Commonly used syntactic sugars for PHP 5.0 to 7.1
In computer science, syntactic sugar refers to the syntax in a programming language that can more easily express an operation. It can make it easier for programmers to use the language: operations can become clearer and more convenient. , or more in line with programmers' programming habits.
Type
Boolean
-
Empty objects are considered
The internal structure oftrue
after
4.0
String
-
string
is similar toarray
. You can use subscripts to access characters like python. String$str = '012345'; echo $str[1]; //1 echo $str{2}; //2
Copy after login
Array
##5.4
In the future, you can define arrays like JS
$arr = ['one', 'two', 'three']; //感觉方便了很多
Copy after login
时间长不用总会忘记重新整理一下加深下印象
Copy after login
$_SERVER时间长不用总会忘记重新整理一下加深下印象
SERVER_ADDR
IP address 127.0.0.1
SERVER_NAME
Host name localhost
SERVER_SOFTWARE
Server type nginx
- ##REMOTE_ADDR
Client IP. 127.0.0.1
s
$_FILES
- $_FILES['file'][ 'name']
Original name of the picture
- $_FILES['file']['type']
Picture MIME type
- $_FILES['file']['size']
Picture size
- $_FILES['file']['tmp_name ']
Server-side temporary name
##Constant
- can be used later
- const
To define constants
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>const DEBUG = true;</pre><div class="contentsignin">Copy after login</div></div>
Operator
##<=>
Comparison operator,- 7.0
- Later support
echo $a <=> $b;/* 当 $a < $b 时, 表达式返回 -1 当 $a = $b 时, 表达是返回 0 当 $a > $b 时, 表达式返回 1 */
Copy after login##??
Null coalescing operator
- feature.
?:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>$name = $_POST[&#39;name&#39;] ?? &#39;&#39;; //如果前面的值不为null,则返回本身,否则返回后面的值</pre><div class="contentsignin">Copy after login</div></div>
Ternary operator
- Can be used in the future
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>$name = $_POST[&#39;name&#39;] ?: &#39;&#39;; ////如果前面的值不为null,则返回本身,否则返回后面的值</pre><div class="contentsignin">Copy after login</div></div>
Process control
5.3
- The above is valid
操作符可以用来跳转到程序中的另一位置。该目标位置可以用目标名称加上冒号来标记,而跳转指令是 goto 之后接上目标位置的标记。PHP 中的 goto 有一定限制,目标位置只能位于同一个文件和作用域,也就是说无法跳出一个函数或类方法,也无法跳入到另一个函数。也无法跳入到任何循环或者 switch 结构中。可以跳出循环或者 switch,通常的用法是用 goto 代替多层的 break。
goto a;echo 'Foo'; a:echo 'Bar';//输出 Bar
函数
变长参数
...
,5.6
以后可用
function dosum(...$arr){ $sum = 0; foreach($arr as $v){ $sum += $v; } return $sum; }$arr = [1, 2, 3, 4, 5];echo dosum(...$arr); // 输出15echo dosum(1,2,3,4,5,6); //输出21//TODO/** 这个语法,我最近总在用。感觉还比较简单。不过要注意服务器版本。。最近入了一个坑。 */
匿名函数(Anonymous functions)
5.3
也叫闭包函数,在JS中很常见。为了防止污染全局作用域。5.3 以后PHP也支持了这种写法
$test = function($name='Li'){ echo 'My name is '.$name; };$test();
如果想要从父作用域中继承变量怎么办
//这里定义一个默认的输出名字的方式$tpl = 'My name is ';//使用 use() 来引用父级的变量,最后输出结果与上边一致 $test = function($name='Li') use($tpl) { echo $tpl.$name; };$test();
需要注意的是,闭包函数的父作用域,是定义它的作用域,不是调用的作用域
类和对象
::class
类的静态方法,用于获取类的完全限定名称,(包含命名空间)
namespace Foo{ class test{ } echo test::class; // 输出 FOO\test, 在使用命名空间的情况非常有用}
5.4
新增加的一个多继承实现方式trait
。下面总结了一下基本概念
1.trait 和 class 是相似的概念,但不能被实例化
2.一个类可以使用多个trait,优先级是 class
> trait
> 父类继承的方法
3.使用insteadof
来解决 tarit 冲突
4.使用as
,来修改方法的访问控制
5.在trait
中也可以使用tarit
。和在class
中用法一致
trait A { public function say(){ echo 'trait A'; } }trait B { public function say(){ echo 'trait B'; } public function walk(){ echo 'walk B'; } }class Person { use A, B{ B :: say insteadof A; // 使用B的say方法代替了A的say方法 walk as protected; // 将walk 设置为受保护的 } }$obj = new Person;$obj->say(); // echo trait A;$obj->walk(); // 提示不能访问一个受保护的方法
6.在trait
中使用, 属性、静态属性、静态方法、抽象类都是被允许的。
trait Test { public static $obj; public $name = 1; static function createObj(){ return empty(self::$obj) ? new self : self::$obj; } }class son { use Test; }$obj = son::createObj();echo $obj->name; // echo 1echo $obj === $obj1 ? 0 : 1; // echo 1
5.3
类的后期静态绑定
官方的解释是:
该功能从语言内部角度考虑被命名为”后期静态绑定”。”后期绑定”的意思是说,static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为”静态绑定”,因为它可以用于(但不限于)静态方法的调用
乍一看,好像什么也没看懂。看看具体的代码吧。
class A { public static function who() { echo __CLASS__; } public static function test() { self::who(); } }class B extends A { public static function who() { echo __CLASS__; } } B::test(); // echo A;// 上面是一个正常的调用, 输出了 A// 当我们把 class A 的 test 方法修改一下。 将 self 改成 static 后class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); } }class B extends A { public static function who() { echo __CLASS__; } } B::test(); // echo B;
总结:PHP5.3
新增加了一类关键字,static
可以在调用函数的方法。用这个关键字,来实现了后期静态绑定
。
异常处理
比较简单记录一下
try{ throw new Execption('抛出异常'); } catch (Execption $e){ //获取异常 $error = $e->getMessage(); }echo $error; //抛出异常
相关推荐:
The above is the detailed content of Commonly used syntactic sugars for PHP 5.0 to 7.1. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
