PHP5.5新特性及与php5.4的区别总结
现在,让我们来看看PHP5.5 可能会新增的功能:
1、放弃对Windows XP和2003 的支持
2.弃用e修饰符
e修饰符是指示preg_replace函数用来评估替换字符串作为PHP代码,而不只是仅仅做一个简单的字符串替换。不出所料,这种行为会源源不断的出现安全问题。这就是为什么在PHP5.5 中使用这个修饰符将抛出一个弃用警告。作为替代,你应该使用preg_replace_callback函数。你可以从RFC找到更多关于这个变化相应的信息。
3.新增函数和类
boolval()
PHP已经实现了strval、intval和floatval的函数。为了达到一致性将添加boolval函数。它完全可以作为一个布尔值计算,也可以作为一个回调函数。
hash_pbkdf2()
PBKDF2全称“Password-Based Key Derivation Function 2”,正如它的名字一样,是一种从密码派生出加密密钥的算法。这就需要加密算法,也可以用于对密码哈希。更广泛的说明和用法示例
array_column()
$userNames = array_column($users, 'name'); // is the same as $userNames = []; foreach ($users as $user) { $userNames[] = $user['name']; }
intl 扩展
将有许多改进 intl的扩展。例如,将会有新的IntlCalendar,IntlGregorianCalendar,IntlTimeZone,IntlBreakIterator,IntlRuleBasedBreakIterator,IntlCodePointBreakIterator类。之前,我竟然不知道有这么多关于intl扩展,如果你想知道更多,我建议你去最新公告里找 Calendar和 BreakIterator。
4.一个简单的密码散列API
$password = "foo"; // creating the hash $hash = password_hash($password, PASSWORD_BCRYPT); // verifying a password if (password_verify($password, $hash)) { // password correct! } else { // password wrong! }
5.新的语言特性和增强功能。
常量引用
“常量引用”意味着数组可以直接操作字符串和数组字面值。举两个例子:
function randomHexString($length) { $str = ''; for ($i = 0; $i < $length; ++$i) { $str .= "0123456789abcdef"[mt_rand(0, 15)]; // direct dereference of string } } function randomBool() { return [false, true][mt_rand(0, 1)]; // direct dereference of array }
我不认为在实践中会使用此功能,但它使语言更加一致。请参阅 RFC。
6.调用empty()函数(和其他表达式)一起工作
目前,empty()语言构造只能用在变量,而不能在其他表达式。
在特定的代码像empty($this->getFriends())将会抛出一个错误。作为PHP5.5 这将成为有效的代码
7.获取完整类别名称
PHP5.3 中引入命名空间的别名类和命名空间短版本的功能。虽然这并不适用于字符串类名称
use Some\Deeply\Nested\Namespace\FooBar; // does not work, because this will try to use the global `FooBar` class $reflection = new ReflectionClass('FooBar'); echo FooBar::class;
为了解决这个问题采用新的FooBar::class语法,它返回类的完整类别名称
8.参数跳跃
如果你有一个函数接受多个可选的参数,目前没有办法只改变最后一个参数,而让其他所有参数为默认值。
RFC上的例子,如果你有一个函数如下:
function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true) { ... }
那么有没有办法设置$report_errors=false,而其他两个为默认值。为了解决这个跳跃参数的问题而提出:
create_query("deleted=0", "name", default, default, false);
我个人不是特别喜欢这个提议。在我的眼睛里,代码需要这个功能,只是设计不当。函数不应该有12个可选参数。
9.标量类型提示
标量类型提示原本计划进入5.4,但由于缺乏共识而没有做。获取更多关于为什么标量类型提示没有做进PHP的信息,请参阅: 标量类型提示比你认为的更难。
对于PHP5.5 而言,针对标量类型提示讨论又一次出现,我认为这是一个相当不错的 提议。
它需要通过输入值来指定类型。例如:123,123.0,“123”都是一个有效的int参数输入,但“hello world”就不是。这与内部函数的行为一致。
function foo(int $i) { ... } foo(1); // $i = 1 foo(1.0); // $i = 1 foo("1"); // $i = 1 foo("1abc"); // not yet clear, maybe $i = 1 with notice foo(1.5); // not yet clear, maybe $i = 1 with notice foo([]); // error foo("abc"); // error
10.Getter 和 Setter
如果你从不喜欢写这些getXYZ()和setXYZ($value)方法,那么这应该是你最受欢迎的改变。提议添加一个新的语法来定义一个属性的设置/读取:
<?php class TimePeriod { public $seconds; public $hours { get { return $this->seconds / 3600; } set { $this->seconds = $value * 3600; } } } $timePeriod = new TimePeriod; $timePeriod->hours = 10; var_dump($timePeriod->seconds); // int(36000) var_dump($timePeriod->hours); // int(10)
当然还有更多的功能,比如只读属性。如果你想要知道更多,请参阅 RFC。
11.生成器
目前,自定义迭代器很少使用,因为它们的实现,需要大量的样板代码。生成器解决这个问题,并提供了一种简单的样板代码来创建迭代器。
例如,你可以定义一个范围函数作为迭代器:
<?php function *xrange($start, $end, $step = 1) { for ($i = $start; $i < $end; $i += $step) { yield $i; } } foreach (xrange(10, 20) as $i) { // ... }
上述xrange函数具有与内建函数相同的行为,但有一点区别:不是返回一个数组的所有值,而是返回一个迭代器动态生成的值。
12.列表解析和生成器表达式
列表解析提供一个简单的方法对数组进行小规模操作:
$firstNames = [foreach ($users as $user) yield $user->firstName];
上述列表解析相等于下面的代码:
$firstNames = []; foreach ($users as $user) { $firstNames[] = $user->firstName; }
也可以这样过滤数组:
$underageUsers = [foreach ($users as $user) if ($user->age < 18) yield $user];
生成器表达式也很类似,但是返回一个迭代器(用于动态生成值)而不是一个数组。
13.finally关键字
这个和java中的finally一样,经典的try ... catch ... finally 三段式异常处理。
14.foreach 支持list()
对于“数组的数组”进行迭代,之前需要使用两个foreach,现在只需要使用foreach + list了,但是这个数组的数组中的每个数组的个数需要一样。看文档的例子一看就明白了。
$array = [ [1, 2], [3, 4], ]; foreach ($array as list($a, $b)) { echo "A: $a; B: $b\n"; }
15.增加了opcache扩展
使用opcache会提高php的性能,你可以和其他扩展一样静态编译(--enable-opcache)或者动态扩展(zend_extension)加入这个优化项。
16.非变量array和string也能支持下标获取了
echo array(1, 2, 3)[0];
echo [1, 2, 3][0];
echo "foobar"[2];

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.