Home Backend Development PHP Tutorial PHP 的八卦两则_PHP

PHP 的八卦两则_PHP

Jun 01, 2016 pm 12:21 PM

PHP5.3 的正式发布,又重新关注起其语言本身。细心的朋友可能发现,PHP5.3 增加了 goto 语句,这在结构化语言今天似乎是有点不可思议的事情。

按照官方的说法,其实这和我们传统理解的 goto 还是有所差别。PHP 5.3 中的 goto 语句只能在脚本文件以及上下文中跳转,因此它无法从某函数或方法跳到其他的函数或者方法 -- 这不得不让我怀疑是否是“妥协”的结果。

看来 PHP 语言的设计者对 goto 语句应用是方便从从多重循环体中跳出(在其文档中的例子也说明了这点)。但其实这功能的本身也引发了不少的争议,个人也隐约的闻到了潘多拉身上的香水味。

其实“好事者”早在 2007 年就开始“期待”这项“新功能”,相信他现在会很开心。不过有趣的是这篇文章的留言给原作者泼了盆冷水。的确,goto 语句所能完成的功能,其实善用 switch 也能做到,而且更有可读性。

不过不管怎么样,语言本身仅仅是工具而已。从程序的本身角度考虑(不仅仅是 PHP),如果有过多的循环等的语块嵌套,那就说明这段代码必须需要优化了。到底应不应该使用 goto 语句,其实本人和其他的 PHP 人员也有过讨论。

最终,大家较为统一的观点就是,避免使用。

“PHP 的 10 宗罪”

老外较起真来真的非常让人受不了,这不又有好事者总结了 PHP 语言本身语法的 “10 宗罪”。其实,我个人这与其称为“Mistake”,还说是 PHP 提供的“美丽的陷阱”。

例如,文中提到的有关单引号和双引号的变量转义问题。其实这一争论的声音从学 PHP 起就在耳畔充斥。甚至记得当年还有道经典的面试题,就是考单引号和双引号的的速度孰快 -- 回过头来看,这论点就犹如此道面试题一样,是没有任何的意义的。

但这不代表文中的些“Mistake”都可以当作笑谈,有些是的确需要注意的。例如

以下为引用的内容:

$i = 0;
while($i     //some code here
    $i++;
}

这样的写法。对应的其实还有 for 语句,我们可能“手残”多写了个分号(不要不承认),那么就只能祈祷 set_time_limit 的数字小些了。

文中指出的其他些问题,也是我们需要纳入思考的范围中,例如数据库存取的缓存问题。为何 PHP 为何至今没有数据池这样的概念,其实这需要从 PHP 这门语言本身的设计哲学出发了。从此问题其实可以引申争为何 PHP 没有走向 Java 的套路,好吧这问题又可以争论一番了。

文中还有其他类似的条目,都非常的具有争议(也许搞不好这就是作者的初衷),但无论怎么说,其提出的几个问题都是值得我们去思考的。

深夜杂谈随想,乱语之处众位见笑了,欢迎发表您的看法。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles