Php 检查变量是数字还是数组
我想检查变量的内容是数字还是数组<代码>是数组(),Php 检查变量是数字还是数组,php,arrays,numbers,Php,Arrays,Numbers,我想检查变量的内容是数字还是数组是数组(),是int(),是数值()实际上不起作用。目前我正在使用myArray[1],它似乎可以工作。但是我想知道为什么这个函数中的一个不能为我做这个 编辑: 似乎我有类似于myArray['id']的内容,这始终是一个数组。您可以使用函数 $type = gettype($variable); if ( $type == 'array' ) { // it's an array } else if ( $type == 'integer' ) { //
是int()
,是数值()
实际上不起作用。目前我正在使用myArray[1],它似乎可以工作。但是我想知道为什么这个函数中的一个不能为我做这个
编辑:
似乎我有类似于myArray['id']
的内容,这始终是一个数组。您可以使用函数
$type = gettype($variable);
if ( $type == 'array' ) {
// it's an array
} else if ( $type == 'integer' ) {
// it's an integer
} else {
// it's a trap !
}
你确定吗
$myNumber = 13;
$myArray = array("test" => "data");
if(is_array($myNumber)) {
echo "myNumber is an array!";
}else{
if(is_numeric($myNumber)) {
echo "myNumber is not an array, but it is a number!";
}
}
我得到我的号码不是数组,但它是一个数字 这不是一个真正的问题。
is_array()
对于编号13
有什么真正的问题吗
虽然PHP可以让您使用访问数组成员所用的相同语法从包含number13
的变量中访问数字1和3,但它不能用整数生成数组。它只是一种“语法糖”
在开始写问题之前,你必须验证你的印象
$array = is_array(13) ? "yes" : "no";
$int = is_int(13) ? "yes" : "no";
$numeric = is_numeric(13) ? "yes" : "no";
echo $array."\n", $int."\n", $numeric."\n";
答复
no
yes
yes
正如预期的那样,所以我不太确定这里的问题是什么
也许值得注意的是,如果你跑步:
$array = is_array("13") ? "yes" : "no";
$int = is_int("13") ? "yes" : "no";
$numeric = is_numeric("13") ? "yes" : "no";
echo $array."\n", $int."\n", $numeric."\n";
答复是:
no
no
yes
这也是您所期望的-字符串和数字不表示为数组
像这样运行gettype:
echo gettype(13);
显示它是一个
整数
…数字13是一个数组吗?您的意思是德语gettype文档(松散翻译)中的is_array(13)==true
:警告永远不要使用gettype检查特定类型,因为返回的字符串将来可能会更改。此外,此方法速度较慢,因为它涉及字符串比较。改用is_*-函数。