php.netphp
<?php $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; //在EOD后必需要包含一个换行
<?php $str = <<<'EOD' Example of string spanning multiple lines using nowdoc syntax. EOD;
关于字符串的编码
PHP 中的 string 的实现方式是一个由字节组成的数组再加上一个整数指明缓冲区长度。如何将字节转换成字符的信息,由程序员来决定。字符串由什么值来组成并没有限制;特别的,其值为 0(“NUL bytes”)的字节能够处于字符串任何位置(不过有几个函数,在本手册中被称为非“二进制安全”的,也许会把 NUL 字节以后的数据全都忽略)。
若是一个脚本的编码是 ISO-8859-1,则其中的字符串也会被编码为 ISO-8859-1,以此类推。不过这并不适用于激活了 Zend Multibyte 时。程序员
将一个对象转换成对象,它将不会有任何变化。若是其它任何类型的值被转换成对象,将会建立一个内置类 stdClass 的实例。若是该值为 NULL,则新的实例为空。数组转换成对象将使键名成为属性名并具备相对应的值。对于任何其它的值,名为 scalar 的成员变量将包含该值shell
自 PHP 5.4 起可用 callable 类型指定回调类型 callback。数组
在 PHP 的函数和类的方法中,超全局变量不能用做可变变量。$this 变量也是一个特殊变量,不能被动态引用。安全
__LINE__ 文件中的当前行号。 __FILE__ 文件的完整路径和文件名。(若是是符号链接,则是解析后的绝对路径) __DIR__ 文件所在的目录。它等价于 dirname(__FILE__) __FUNCTION__ 返回该函数被定义时的名字(区分大小写) __CLASS__ 返回该类被定义时的名字(区分大小写) __METHOD__ 类的方法名。返回该方法被定义时的名字(区分大小写) __NAMESPACE__ 当前命名空间的名称(区分大小写)
$a ** $b
a的b次方(php5.6+)在模板中使用真是棒棒的。
PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch。替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,endwhile;,endfor;,endforeach; 以及 endswitch;。php7
<?php if ($a == 5): ?> A is equal to 5 <?php endif; ?>
任何有效的 PHP 代码都有可能出如今函数内部,甚至包括其它函数和类定义。
函数无需在调用以前被定义,除非是下面两个例子中函数是有条件被定义时。
当一个函数是有条件被定义时,必须在调用函数以前定义。闭包
<?php $makefoo = true; /* 不能在此处调用foo()函数, 由于它还不存在,但能够调用bar()函数。*/ bar(); if ($makefoo) { function foo() { echo "I don't exist until program execution reaches me.\n"; } } /* 如今能够安全调用函数 foo()了, 由于 $makefoo 值为真 */ if ($makefoo) foo(); function bar() { echo "I exist immediately upon program start.\n"; } ?>
PHP 中的全部函数和类都具备全局做用域,能够定义在一个函数以内而在以外调用,反之亦然。app
<?php function foo() { function bar() { echo "I don't exist until foo() is called.\n"; } } foo(); /* 如今能够调用bar()函数了,由于foo()函数 的执行使得bar()函数变为已定义的函数 */ bar(); ?>
若是想要函数的一个参数老是经过引用传递,能够在函数定义中该参数的前面加上符号 &。传引用的参数也能够有默认值。函数
the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.ui
Class/interface name The parameter must be an instanceof the given class or interface name. PHP 5.0.0 array The parameter must be an array. PHP 5.1.0 callable The parameter must be a valid callable. PHP 5.4.0 bool The parameter must be a boolean value. PHP 7.0.0 float The parameter must be a floating point number. PHP 7.0.0 int The parameter must be an integer. PHP 7.0.0 string The parameter must be a string. PHP 7.0.0
默认状况下若是能够转换,php会将不符合声明的传入参数转换,可使用strict_type。
note
declare(strict_types=1){XXXXX}
结构设置declare做用域,只能是declare(strict_types=1) ;
Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file.
function sum($a, $b): float { return $a + $b; }
在 PHP 5.6 及以上的版本中,由 ... 语法实现;在 PHP 5.5 及更早版本中(php7也可以使用),使用函数 func_num_args(),func_get_arg(),和 func_get_args() 。
//...前边能够带有普通参数,也能够有类型提示(必须所有知足) function sum(...$numbers) { $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc; }
You can also use ... when calling functions to unpack an array or Traversable variable or literal into the argument list:
function add($a, $b) { return $a + $b; } $a = [1, 2]; echo add(...$a);
可变函数不能用于例如 echo,print,unset(),isset(),empty(),include,require 以及相似的语言结构。须要使用本身的包装函数来将这些结构用做可变。
$foo->$funcname(); Foo::$variable();
匿名函数(Anonymous functions),也叫闭包函数(closures)。
$greet = function($name) { printf("Hello %s\r\n", $name); }; $greet('World'); $greet('PHP');
闭包能够从父做用域中继承变量。 任何此类变量都应该用 use 语言结构传递进去。闭包的父做用域是定义该闭包的函数(不必定是调用它的函数)
$message = 'hello'; // 继承 $message,不用use会报错 $example = function () use ($message) { var_dump($message); }; echo $example();