作者:webabcd
介绍
速战速决 之 PHP
示例
1、数据类型: bool, int, float, string, object
basic/type1.php
<span style="color: #000000;">php </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 数据类型: bool, int, float, string, object </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 布尔类型(true, false 不分大小写)</span> <span style="color: #800080;">$b</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$b</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "true"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #800080;">$b</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (!<span style="color: #800080;">$b</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "false"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 整型</span> <span style="color: #800080;">$i</span> = 100<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$i</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 八进制(以 0 开头)</span> <span style="color: #800080;">$i</span> = 0144; <span style="color: #008000;">//</span><span style="color: #008000;"> 等于十进制的 100</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$i</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 十六进制(以 0x 开头)</span> <span style="color: #800080;">$i</span> = 0x64; <span style="color: #008000;">//</span><span style="color: #008000;"> 等于十进制的 100</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$i</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 浮点型</span> <span style="color: #800080;">$f</span> = 3.14<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$f</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">is_nan</span>(<span style="color: #800080;">$f</span>)) <span style="color: #008000;">//</span><span style="color: #008000;"> 通过 is_nan 函数来判断一个变量的值是否不是数字类型</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$f</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$f</span> == 1.11 + 2.03) <span style="color: #008000;">//</span><span style="color: #008000;"> false</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "注意不要用 == 来比较两个浮点型变量"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">bccomp</span>(<span style="color: #800080;">$f</span>, 1.11 + 2.03, 2) == 0) <span style="color: #008000;">//</span><span style="color: #008000;"> true</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "请用 bccomp 函数来比较两个浮点型变量"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 字符串类型</span> <span style="color: #800080;">$s</span> = 'abc$i\$'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$s</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: abc$i\$</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #800080;">$s</span> = "abc<span style="color: #800080;">$i</span>\$"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$s</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: abc100$</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_string</span>(<span style="color: #800080;">$s</span>)) <span style="color: #008000;">//</span><span style="color: #008000;"> 通过 is_string 函数来判断一个变量的值是否是字符串类型</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$s</span> 是字符串"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 字符串的格式化输出</span> <span style="color: #800080;">$s</span> = <span style="color: #008080;">sprintf</span>("string: %s, int: %d", "xyz", 100<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$s</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> heredoc 结构(通过“ <span style="color: #800080;">$h</span> = XXX </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$s</span><span style="color: #000000;">; \\ XXX; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$h</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 上面 echo $h; 输出的内容如下: echo abc100$; \ </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;"> nowdoc 结构(通过“ <span style="color: #800080;">$n</span> = echo</span> <span style="color: #800080;">$s</span><span style="color: #000000;">; \\ YYY; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$n</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 上面 echo $n; 输出的内容如下: echo $s; \\ </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;"> object 类型</span> <span style="color: #800080;">$obj</span> = (<span style="color: #0000ff;">object</span>)"sss"; <span style="color: #008000;">//</span><span style="color: #008000;"> 将一个变量转换为对象时,系统会创建一个内置类,该类的 scalar 属性即为原变量的值</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj</span>->scalar; <span style="color: #008000;">//</span><span style="color: #008000;"> output: sss</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> NULL 类型(NULL 不分大小写)</span> <span style="color: #800080;">$n1</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 未赋值则为 null</span> <span style="color: #800080;">$n2</span> = <span style="color: #0000ff;">NULL</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$n1</span> == <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "null"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: null</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$n2</span> == <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "null"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: null</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;"> * 数据类型间的显示转换 * (int) * (bool) * (float) * (string) * (array) * (object) </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 数据类型间的隐式转换</span> <span style="color: #800080;">$v</span> = "1"; <span style="color: #008000;">//</span><span style="color: #008000;"> 字符串类型</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$v</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: 1</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #800080;">$v</span> = 2 + <span style="color: #800080;">$v</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 隐式转换为整型</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$v</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: 3</span> <span style="color: #0000ff;">echo</span> "<br>";
2、数据类型: array
basic/type2.php
<span style="color: #000000;">php </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 数据类型: array </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 定义数组(数组中的 key 索引为从 0 开始的递增的整型)</span> <span style="color: #800080;">$array0</span> = <span style="color: #0000ff;">array</span>(1, 2, 3, 4<span style="color: #000000;">); </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array0</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 定义数组</span> <span style="color: #800080;">$array1</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;"> ( </span>"a" => "abx", "x" => "xyz",<span style="color: #000000;"> ); </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array1</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 在 php 5.4 之后可以通过 [] 定义数组</span> <span style="color: #800080;">$array2</span> =<span style="color: #000000;"> [ </span>"a" => "abx", "x" => "xyz",<span style="color: #000000;"> ]; </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array2</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> key 是 integer 类型或 string 类型,如果是其他类型的话会被自动转换为 integer 类型或 string 类型 // key 可以 integer 和 string 两种类型混合存在 // key 为可选项,如果未指定的话,则 PHP 将自动使用之前用过的最大 integer 键名加上 1 作为新的键名,如果没有则从 0 开始</span> <span style="color: #800080;">$array3</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;"> ( </span>"a" => "abc", "x" => "xyz", 100 => "111", 100.99 => "222", 1 => "333", <span style="color: #0000ff;">true</span> => "444", "a", "b", "c"<span style="color: #000000;"> ); </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array3</span><span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> * 上面数组定义的结果如下: * 'a' => string 'abc' * 'x' => string 'xyz' * 100 => string '222' * 1 => string '444' * 101 => string 'a' * 102 => string 'b' * 103 => string 'c' </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 通过 [] 指定数组字符串类型的 key 并获取其值</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>["a"]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: abc</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 {} 指定数组字符串类型的 key 并获取其值</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>{"a"}; <span style="color: #008000;">//</span><span style="color: #008000;"> output: abc</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 [] 指定数组整型类型的 key 并获取其值(注:这里不是指位置 100 的元素,而是指 key 为 100 的元素)</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>[100]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: 222</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 {} 指定数组整型类型的 key 并获取其值(注:这里不是指位置 100 的元素,而是指 key 为 100 的元素)</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>{100}; <span style="color: #008000;">//</span><span style="color: #008000;"> output: 222</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 修改数组中指定 key 的值</span> <span style="color: #800080;">$array3</span>["a"] = "aaa"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>["a"]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: aaa</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 key 和 value 为数组添加新元素(在数据最后一个元素的后面添加)</span> <span style="color: #800080;">$array3</span>[50] = "d"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>[50]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: d</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 仅通过 value 为数组添加新元素(在数据最后一个元素的后面添加),key 值将自动使用之前用过的最大 integer 键名加上 1 作为新的键名,如果没有则从 0 开始</span> <span style="color: #800080;">$array3</span>[] = "e"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>[104]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: e</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历数组</span> <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array3</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$i</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 销毁数组中的指定元素</span> <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$array3</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]); } </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array3</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 之前通过 unset 销毁了每个数组元素,但是数组内部的 key 索引不会被破坏,如下例,其 key 值会由系统分配为 105</span> <span style="color: #800080;">$array3</span>[] = "xxx"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>[105]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: xxx</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 销毁指定的变量,如果是销毁数组的话,则数组内部的 key 索引也会被破坏</span> <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$array3</span><span style="color: #000000;">); </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array3</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 由于之前数组内部的 key 索引被破坏了,所以下例中,其 key 值会由系统分配为 0</span> <span style="color: #800080;">$array3</span>[] = "yyy"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array3</span>[0]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: yyy</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 将任意变量转换为数组时,数组的第一个元素即为该变量本身</span> <span style="color: #800080;">$s</span> = "sss"<span style="color: #000000;">; </span><span style="color: #800080;">$array4</span> = (<span style="color: #0000ff;">array</span>)<span style="color: #800080;">$s</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$array4</span>[0]; <span style="color: #008000;">//</span><span style="color: #008000;"> output: sss</span> <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 array_values 可以重建数组内部的 key 索引</span> <span style="color: #800080;">$array5</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;"> ( </span>"a" => "abx", "x" => "xyz", 0 => "123",<span style="color: #000000;"> ); </span><span style="color: #800080;">$array5</span> = <span style="color: #008080;">array_values</span>(<span style="color: #800080;">$array5</span>); <span style="color: #008000;">//</span><span style="color: #008000;"> 无论之前的 key 是什么,在 array_values 之后,数组内的 key 索引都将变为从 0 开始递增的整型数据</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array5</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 list 函数将数组中的成员依次赋值给指定的变量 // 注意:array[0] 对应 list 的 1 个参数,array[1] 对应 list 的 2 个参数......(这里的 array[0], array[1] 不是指第 0 个元素, 第 1 个元素,而是 key 为 0 的元素, key 为 1 的元素)</span> <span style="color: #0000ff;">list</span> (<span style="color: #800080;">$r1</span>, <span style="color: #800080;">$r2</span>, <span style="color: #800080;">$r3</span>) = <span style="color: #800080;">$array5</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$r1</span>, <span style="color: #800080;">$r2</span>, <span style="color: #800080;">$r3</span>"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: abx, xyz, 123</span> <span style="color: #0000ff;">echo</span> "<br>";
OK
[源码下载]