PHP 可用的函数
Variable handling and type-checking functions reference.
Key Points`isset` is set-and-not-null, `empty` tests falsiness, `array_key_exists` tests existence only.
Type checks
PHP
<?php
$vals = [1, 1.5, "x", true, null, [], new stdClass(), fn() => 1];
foreach ($vals as $v) {
printf("%-12s %s\n", gettype($v), is_scalar($v) ? "标量" : "非标量");
}Output
integer 标量
double 标量
string 标量
boolean 标量
NULL 非标量
array 非标量
object 非标量
object 非标量| Function | True when |
|---|---|
| is_int | 整数 |
| is_float | 浮点 |
| is_string | 字符串 |
| is_bool | 布尔 |
| is_array | 数组 |
| is_object | 对象 |
| is_null | null |
| is_numeric | 数字或数字字符串 |
| is_callable | 可调用(函数名、闭包、[对象, 方法]) |
| is_iterable | 数组或 Traversable |
Existence
PHP
<?php
$arr = ["a" => 1, "b" => null, "c" => 0, "d" => "", "e" => "0"];
foreach (array_keys($arr) as $k) {
printf("%s isset=%d empty=%d key_exists=%d\n",
$k,
isset($arr[$k]) ? 1 : 0,
empty($arr[$k]) ? 1 : 0,
array_key_exists($k, $arr) ? 1 : 0
);
}
var_dump(isset($arr["zzz"]));Output
a isset=1 empty=0 key_exists=1
b isset=0 empty=1 key_exists=1
c isset=1 empty=1 key_exists=1
d isset=1 empty=1 key_exists=1
e isset=1 empty=1 key_exists=1
bool(false)For a
null value isset is 0 while array_key_exists is 1. **Only array_key_exists distinguishes missing from null.**Conversion
PHP
<?php
echo intval("42abc"), " ", intval("0x1A", 16), " ", intval(3.9), "\n";
echo floatval("1.5abc"), " ", (float)"2.5", "\n";
echo strval(42) . strval(1.5), "\n";
$v = "123";
settype($v, "integer");
var_dump($v);
var_dump((bool)"0");
var_dump((bool)"0.0");
var_dump((array)"x");Output
42 26 3
1.5 2.5
421.5
int(123)
bool(false)
bool(true)
array(1) {
[0]=>
string(1) "x"
}intval("42abc") is 42 and intval("abc") is 0. Silently turning garbage into 0 hides errors — validate with filter_var first.Debug output
PHP
<?php
$data = ["a" => 1, "b" => [1, 2]];
echo var_export($data, true), "\n";
echo "---\n";
echo json_encode($data, JSON_UNESCAPED_UNICODE), "\n";
echo "---\n";
print_r($data);Output
array (
'a' => 1,
'b' =>
array (
0 => 1,
1 => 2,
),
)
---
{"a":1,"b":[1,2]}
---
Array
(
[a] => 1
[b] => Array
(
[0] => 1
[1] => 2
)
)var_export($v, true) returns runnable PHP — good for config files. print_r is human-readable; var_dump includes types and lengths.
Serialization
PHP
<?php
$data = ["name" => "张三", "tags" => ["a", "b"]];
$s = serialize($data);
echo $s, "\n";
echo strlen($s), "\n";
$back = unserialize($s);
echo $back["name"], "\n";
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo strlen($json), "\n";Output
a:2:{s:4:"name";s:6:"张三";s:4:"tags";a:2:{i:0;s:1:"a";i:1;s:1:"b";}}
56
张三
42unserialize on user-controlled data is a serious vulnerability — object injection leads to RCE. Use json_decode, or pass ["allowed_classes" => false].PHP
<?php
$s = 'O:8:"stdClass":1:{s:1:"a";i:1;}';
// 危险:允许实例化任意类
var_dump(unserialize($s) instanceof stdClass);
// 安全:禁止实例化类
var_dump(unserialize($s, ["allowed_classes" => false]));Output
bool(true)
bool(false)类型判断
PHP
<?php
$vals = [1, 1.5, "x", true, null, [], new stdClass(), fn() => 1];
foreach ($vals as $v) {
printf("%-12s %s\n", gettype($v), is_scalar($v) ? "标量" : "非标量");
}Output
integer 标量
double 标量
string 标量
boolean 标量
NULL 非标量
array 非标量
object 非标量
object 非标量| 函数 | 为真条件 |
|---|---|
| is_int | 整数 |
| is_float | 浮点 |
| is_string | 字符串 |
| is_bool | 布尔 |
| is_array | 数组 |
| is_object | 对象 |
| is_null | null |
| is_numeric | 数字或数字字符串 |
| is_callable | 可调用(函数名、闭包、[对象, 方法]) |
| is_iterable | 数组或 Traversable |
存在性判断
PHP
<?php
$arr = ["a" => 1, "b" => null, "c" => 0, "d" => "", "e" => "0"];
foreach (array_keys($arr) as $k) {
printf("%s isset=%d empty=%d key_exists=%d\n",
$k,
isset($arr[$k]) ? 1 : 0,
empty($arr[$k]) ? 1 : 0,
array_key_exists($k, $arr) ? 1 : 0
);
}
var_dump(isset($arr["zzz"]));Output
a isset=1 empty=0 key_exists=1
b isset=0 empty=1 key_exists=1
c isset=1 empty=1 key_exists=1
d isset=1 empty=1 key_exists=1
e isset=1 empty=1 key_exists=1
bool(false)b 的值是 null,isset 返回 0 而 array_key_exists 返回 1。**要区分「键不存在」和「键存在但值为 null」,只能用 array_key_exists。**转换
PHP
<?php
echo intval("42abc"), " ", intval("0x1A", 16), " ", intval(3.9), "\n";
echo floatval("1.5abc"), " ", (float)"2.5", "\n";
echo strval(42) . strval(1.5), "\n";
$v = "123";
settype($v, "integer");
var_dump($v);
var_dump((bool)"0");
var_dump((bool)"0.0");
var_dump((array)"x");Output
42 26 3
1.5 2.5
421.5
int(123)
bool(false)
bool(true)
array(1) {
[0]=>
string(1) "x"
}intval("42abc") 得 42(遇到非数字就停),intval("abc") 得 0。用它解析用户输入会把「abc」静默变成 0,掩盖错误。先 filter_var 校验再用。调试输出
PHP
<?php
$data = ["a" => 1, "b" => [1, 2]];
echo var_export($data, true), "\n";
echo "---\n";
echo json_encode($data, JSON_UNESCAPED_UNICODE), "\n";
echo "---\n";
print_r($data);Output
array (
'a' => 1,
'b' =>
array (
0 => 1,
1 => 2,
),
)
---
{"a":1,"b":[1,2]}
---
Array
(
[a] => 1
[b] => Array
(
[0] => 1
[1] => 2
)
)var_export($v, true) 返回可直接当 PHP 代码执行的字符串,适合生成配置文件;print_r 适合人看;var_dump 带类型和长度。
序列化
PHP
<?php
$data = ["name" => "张三", "tags" => ["a", "b"]];
$s = serialize($data);
echo $s, "\n";
echo strlen($s), "\n";
$back = unserialize($s);
echo $back["name"], "\n";
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo strlen($json), "\n";Output
a:2:{s:4:"name";s:6:"张三";s:4:"tags";a:2:{i:0;s:1:"a";i:1;s:1:"b";}}
56
张三
42unserialize 处理用户可控数据是严重漏洞——可以触发对象注入(POP chain),进而执行任意代码。用 json_decode 代替,或者加 ["allowed_classes" => false]。PHP
<?php
$s = 'O:8:"stdClass":1:{s:1:"a";i:1;}';
// 危险:允许实例化任意类
var_dump(unserialize($s) instanceof stdClass);
// 安全:禁止实例化类
var_dump(unserialize($s, ["allowed_classes" => false]));Output
bool(true)
bool(false)