什么是 Javascript 未定义的 PHP 等价物? [英] What is the PHP equivalent of Javascript's undefined?

查看:78
本文介绍了什么是 Javascript 未定义的 PHP 等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的假设后跟基于假设的问题:

My assumption followed by question based on the assumption:

Javascript 有 nullundefined.你可以将一个变量设置为 null,暗示它没有值,或者你可以将它设置为 undefined,暗示它不知道它是否有值或不是 - 它根本没有设置.

Javascript has null and undefined. You can set a variable to be null, implying it has no value, or you can set it to undefined, implying that it isn't known whether it has a value or not - it's just not set at all.

PHP 有 null,到目前为止,我使用它的方式与在 Javascript 中使用 null 的方式相同.PHP 是否有等效的 undefined?

PHP has null, which so far I've used in the same way I'd use null in Javascript. Does PHP have an equivalent of undefined?

推荐答案

不是真的,undefined 在 PHP 中没有对应物.比较 JS 的 undefined 和 PHPs null 并没有真正叠加,但它与您将要获得的一样接近.在两种语言中,您都可以指定这些值/非值:

Not really, undefined has no counterpart in PHP. Comparing JS's undefined to PHP s null doesn't really stack up, but it's as close as you're going to get. In both languages you can assign these values/non-values:

var nothingness = undefined;
$nothing = null;

而且,如果你声明了一个变量,但没有给它赋值,看起来 JS 将 var 解析为 undefined,而 PHP 解析(或赋值)该变量 null:

and, if you declare a variable, but don't assign it anything, it would appear that JS resolves the var to undefined, and PHP resolves (or assigns) that variable null:

var nothingness;
console.log(nothingness === undefined);//true

$foo;
echo is_null($foo) ? 'is null' : '?';//is null

使用 isset 时要小心:

$foo = null;
//clearly, foo is set:
if (isset($foo))
{//will never echo
    echo 'foo is set';
}

知道变量是否存在的唯一真正方法,AFAIK,是使用get_defined_vars:

The only real way to know if the variable exists, AFAIK, is by using get_defined_vars:

$foo;
$bar = null;
$defined = get_defined_vars();
if (array_key_exists($defined, 'foo'))
{
    echo '$foo was defined, and assigned: '.$foo;
}

这篇关于什么是 Javascript 未定义的 PHP 等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆