PHP 可用的函数

Variable handling and type-checking functions reference.

Intermediate
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       非标量
FunctionTrue when
is_int整数
is_float浮点
is_string字符串
is_bool布尔
is_array数组
is_object对象
is_nullnull
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
张三
42
unserialize 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)

Friends & Partners