判断php变量是否定义,是否为空

isset() 【1】

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

输入可以是多个变量,只有所有的变量为真的时候,返回真

 

empty()【2】

 

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

 

输入只能是一个变量

 

is_null() 【3】

Returns TRUE if var is null , FALSE otherwise.

 

 

 

   A variable is considered to be null if:

  • it has been assigned the constant NULL.

  • it has not been set to any value yet.

  • it has been unset().    

使用 PHP 函数对变量  $x 进行比较
表达式gettype()empty()is_null()isset()if($x) Boolean
$x = "";stringTRUEFALSETRUEFALSE
$x = null;NULLTRUETRUEFALSEFALSE
var $x;NULLTRUETRUEFALSEFALSE
$x is undefinedNULLTRUETRUEFALSEFALSE
$x = array();arrayTRUEFALSETRUEFALSE
$x = false;booleanTRUEFALSETRUEFALSE
$x = true;booleanFALSEFALSETRUETRUE
$x = 1;integerFALSEFALSETRUETRUE
$x = 42;integerFALSEFALSETRUETRUE
$x = 0;integerTRUEFALSETRUEFALSE
$x = -1;integerFALSEFALSETRUETRUE
$x = "1";stringFALSEFALSETRUETRUE
$x = "0";stringTRUEFALSETRUEFALSE
$x = "-1";stringFALSEFALSETRUETRUE
$x = "php";stringFALSEFALSETRUETRUE
$x = "true";stringFALSEFALSETRUETRUE
$x = "false";stringFALSEFALSETRUETRUE

 

(上表没有找到原始来源,谁知道请告诉我)

 

如果变量是一个object该如何呢?

表达式gettype()empty()is_null()isset()if($x) Boolean
$x = new object() objectFALSEFALSETRUETRUE

 

参考:

【1】http://php.net/manual/en/function.isset.php

【2】http://www.php.net/manual/en/function.empty.php

【3】http://www.php.net/manual/en/function.is-null.php

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 JavaScript 中,可以使用 AJAX 技术向服务器发送请求,并获取服务器返回的数据,从而判断 PHP 变量是否存在。下面是一个示例代码: ```javascript function checkVariable(variableName) { var xhr = new XMLHttpRequest(); xhr.open('GET', 'check_variable.php?variableName=' + variableName, true); xhr.onload = function() { if (xhr.status === 200) { if (xhr.responseText === 'true') { console.log(variableName + ' exists.'); } else { console.log(variableName + ' does not exist.'); } } }; xhr.send(); } ``` 在上面的代码中,我们定义了一个名为 `checkVariable` 的函数,它接受一个参数 `variableName`,表示要检查PHP 变量名。函数通过 AJAX 向服务器发送 GET 请求,请求的 URL 中包含要检查PHP 变量名。服务器收到请求后,根据变量名进行判断,并返回一个字符串 `'true'` 或 `'false'`。如果返回 `'true'`,则表示 PHP 变量存在,否则表示 PHP 变量不存在。函数根据服务器返回的字符串进行判断,并在控制台输出相应的信息。 在服务器端,可以使用 PHP 的 `isset()` 函数来判断一个变量是否存在。例如,在 `check_variable.php` 文件中可以这样编写代码: ```php <?php if (isset($_GET['variableName'])) { echo 'true'; } else { echo 'false'; } ?> ``` 在上面的 PHP 代码中,我们通过 `isset()` 函数判断 `$_GET['variableName']` 是否存在。如果存在,则返回字符串 `'true'`,否则返回字符串 `'false'`。注意,为了安全起见,需要对传递的参数进行过滤和验证,以防止恶意攻击。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值