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
Array
##5.4
In the future, you can define arrays like JS
$arr = ['one', 'two', 'three']; //感觉方便了很多
时间长不用总会忘记重新整理一下加深下印象
$_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
<pre class='brush:php;toolbar:false;'>const DEBUG = true;</pre>
Operator
##
Comparison operator,- 7.0
- Later support
echo $a <=> $b;/* 当 $a < $b 时, 表达式返回 -1 当 $a = $b 时, 表达是返回 0 当 $a > $b 时, 表达式返回 1 */
##??
Null coalescing operator
- feature.
-
?:<pre class='brush:php;toolbar:false;'>$name = $_POST[&#39;name&#39;] ?? &#39;&#39;; //如果前面的值不为null,则返回本身,否则返回后面的值</pre>
Ternary operator
- Can be used in the future
<pre class='brush:php;toolbar:false;'>$name = $_POST[&#39;name&#39;] ?: &#39;&#39;; ////如果前面的值不为null,则返回本身,否则返回后面的值</pre>
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!

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

HTTPS significantly improves the security of sessions by encrypting data transmission, preventing man-in-the-middle attacks and providing authentication. 1) Encrypted data transmission: HTTPS uses SSL/TLS protocol to encrypt data to ensure that the data is not stolen or tampered during transmission. 2) Prevent man-in-the-middle attacks: Through the SSL/TLS handshake process, the client verifies the server certificate to ensure the connection legitimacy. 3) Provide authentication: HTTPS ensures that the connection is a legitimate server and protects data integrity and confidentiality.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.


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

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

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.

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