- 四种标量类型:
- 1. boolean(布尔型)
- 2. integer(整型)
- 3. float(浮点型,也称作 double)
- 4. string(字符串)
- 三种复合类型:
- 1. array(数组)
- 2. object(对象)
- 3. callable(可调用)
- 最后是两种特殊类型:
- 1. resource(资源)
- 2. NULL(无类型)
子句中指定要导入的外部环境变量
- use
- function getClosure($n) {
- $a = 100;
- return function($m) use($n, &$a) {
- $a += $n + $m;
- echo $a."\n";
- };
- }
- $fn = getClosure(1);
- $fn(1); //102
- $fn(2); //105
- $fn(3); //109
- echo $a; //Notice: Undefined variable
- class Dog {
- private $_name;
- protected $_color;
- public
- function __construct($name, $color) {
- $this - >_name = $name;
- $this - >_color = $color;
- }
- public
- function greet($greeting) {
- return function() use($greeting) {
- //类中闭包可通过 $this 变量导入对象
- echo "$greeting, I am a {$this->_color} dog named {$this->_name}.\n";
- };
- }
- public
- function swim() {
- return static
- function() {
- //类中静态闭包不可通过 $this 变量导入对象,由于无需将对象导入闭包中,
- //因此可以节省大量内存,尤其是在拥有许多不需要此功能的闭包时。
- echo "swimming....\n";
- };
- }
- private
- function privateMethod() {
- echo "You have accessed to {$this->_name}'s privateMethod().\n";
- }
- public
- function __invoke() {
- //此方法允许对象本身被调用为闭包
- echo "I am a dog!\n";
- }
- }
- $dog = new Dog("Rover", "red");
- $dog - >greet("Hello")();
- $dog - >swim()();
- $dog();
- //通过ReflectionClass、ReflectionMethod来动态创建闭包,并实现直接调用非公开方法。
- $class = new ReflectionClass('Dog');
- $closure = $class - >getMethod('privateMethod') - >getClosure($dog);
- $closure();
- . addslashes函数转义风险:对于URL参数arg = %df\'在经过addslashes转义后在GBK编码下arg = 運'
- . urldecode函数解码风险:对于URL参数uid = 1%27在调用urldecode函数解码(二次解码)后将变成uid = 1'
- $str = preg_replace_callback(
- '/([a-z]*)([A-Z]*)/',
- function($matchs){
- return strtoupper($matchs[1]).strtolower($matchs[2]);
- },
- $str
- );
- // 文件路径:src/sds.h
- struct sdshdr {
- // 记录buf数组中已使用字节的数量
- int len;
- // 记录buf数组中未使用字节的数量
- int free;
- // 字节数组,用于保存字符串
- char buf[];
- };
- 5 / 3;//1.6666666666667
- 5.7 % 3;//2
- 5 % 3;//2
- 2 ** 3;//8
结合方向 | 运算符 | 附加信息 |
---|---|---|
无 | clone new | clone 和 new |
左 | [ | array() |
右 | ** | 算术运算符 |
右 | ++ -- ~ (int) (float) (string) (array) (object) (bool) @ | 类型和递增/递减 |
无 | instanceof | 类型 |
右 | ! | 逻辑运算符 |
左 | * / % | 算术运算符 |
左 | + - . | 算术运算符和字符串运算符 |
左 | << >> | 位运算符 |
无 | < <= > >= | 比较运算符 |
无 | == != === !== <> <=> | 比较运算符 |
左 | & | 位运算符和引用 |
左 | ^ | 位运算符 |
左 | | | 位运算符 |
左 | && | 逻辑运算符 |
左 | || | 逻辑运算符 |
左 | ?? | 比较运算符 |
左 | ? : | ternary |
右 | = += -= *= **= /= .= %= &= |= ^= <<= >>= | 赋值运算符 |
左 | and | 逻辑运算符 |
左 | xor | 逻辑运算符 |
左 | or | 逻辑运算符 |
- //示例一:函数内销毁全局变量$foo是无效的
- function destroy_foo() {
- global $foo;
- unset($foo);
- echo $foo; //Notice: Undefined variable: foo
- }
- $foo = 'bar';
- destroy_foo();
- echo $foo; //bar
- //示例二:要在函数中 unset 一个全局变量,应使用 $GLOBALS 数组来实现
- function foo() {
- unset($GLOBALS['bar']);
- }
- $bar = "something";
- foo();
- echo $bar; //Notice: Undefined variable: bar
- $binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);//Pack data into binary string
- $array = unpack("c4chars/nint", $binarydata);//Unpack data from binary string
- print_r($array);//Array ( [chars1] => 19 [chars2] => 52 [chars3] => 24 [chars4] => 22 [int] => 16706 )
- // Proposed group use syntax:
- use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo };
- // Compared to current use syntax:
- use FooLibrary\Bar\Baz\ClassA;
- use FooLibrary\Bar\Baz\ClassB;
- use FooLibrary\Bar\Baz\ClassC;
- use FooLibrary\Bar\Baz\ClassD as Fizbo;
- // Fetches the request parameter user and results in 'nobody' if it doesn't exist
- $username = $_GET['user'] ?? 'nobody';
- // equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
operator | equivalent |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
- 1. random_bytes(int length):Generates cryptographically secure pseudo-random bytes, such as when generating salts, keys or initialization vectors.
- 2. random_int(int min, int max):Generates cryptographically secure pseudo-random integers, such as when shuffling a deck of cards for a poker game.
- $bytes = random_bytes(5);
- var_dump(bin2hex($bytes));//string(10) "385e33f741"
- var_dump(random_int(100, 999));//int(248)
来源: http://www.cnblogs.com/XiongMaoMengNan/p/7205312.html