PHP检查变量既不是空的,也不是字符串


PHP - check variable is neither empty nor a string

我正在编写与数据库链接的php代码。

如果我把值放在php代码上,例如

$pid=某个值。

这个some值将把它传递给下一个php文件。

我想检查一下这个somevalue不是空的或者不是字符串值

if( some condition ){
printf("Invalid input: %s'n", mysqli_connect_error());
echo "<br/><a href='index.php'>Back to previous page</a>";
exit();
}

使用

if (!empty($pid) && is_numeric($pid)) {
    // proceed
} else {
    // error
}
$pid  = (int) $pid;
if ($pid) {
} else {
}

只需将其定义为口语

if ( ! empty($foo) && ! is_string($foo) ) {
}
if(!empty($pid) and !is_string($foo)){
   printf("Invalid input: %s'n", mysqli_connect_error());
   echo "<br/><a href='index.php'>Back to previous page</a>";
   exit();
}