[英]PHP Checking if Array is empty logic not working
I have a page that loads and creates an empty array: 我有一个页面加载并创建一个空数组:
$_SESSION['selectedItemIDs'] = array();
If the user hasn't added any selections that get stored in the array I then check the array and branch accordingly, but there appears to be some error in my logic/syntax
that is failing here. 如果用户尚未添加存储在数组中的任何选择,则应检查数组并进行相应的分支,但是我的
logic/syntax
似乎有一些错误,导致此处失败。
Here's where I test if the $_SESSION['selectedItemIDs']
is set but empty: 这是我测试
$_SESSION['selectedItemIDs']
是否设置为空的地方:
if (isset($_SESSION['selectedItemIDs']) && $_SESSION['selectedItemIDs'] !== '') {
// update the selections in a database
} else {
// nothing selection so just record this in a variable
$selectedItems = 'no selected items were made';
}
When testing this with no selections if I print the $_SESSION['selectedItemIDs']
array I get this: 如果不打印选择内容进行测试,如果我打印
$_SESSION['selectedItemIDs']
数组,则会得到以下信息:
[selectedItemIDs] => Array
(
)
however the $selectedItems
variable is not being set - it's evaluating the if test as true when I would expect it to be false, but I'm obviously misunderstanding something here. 但是,没有设置
$selectedItems
变量-当我期望它为false时,它将if测试评估为true,但显然我在这里误解了一些东西。
Use empty()
function. 使用
empty()
函数。
empty() - it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.
empty() -如果变量为空字符串,false,array(),NULL,“ 0?”,0和未设置的变量,则它将返回true。
Syntax 句法
empty(var_name)
Return value 返回值
FALSE if var_name has a non-empty and non-zero value.
Value Type : 值类型 :
Boolean
List of empty things : 空物清单:
Code 码
if (!empty($_SESSION['selectedItemIDs']) ) {
// update the selections in a database
} else {
// nothing selection so just record this in a variable
$selectedItems = 'no selected items were made';
}
$_SESSION['selectedItemIDs'] = array();
this variable is already set, but is empty. 此变量已设置,但为空。
isset($_SESSION['selectedItemIDs'])
check if variable exists, even if it's nothing in. isset($_SESSION['selectedItemIDs'])
检查变量是否存在,即使变量不存在也是如此。
To check if there is anything just use 要检查是否有任何东西,只需使用
empty($_SESSION['selectedItemIDs']);
if (!empty($_SESSION['selectedItemIDs'])) {
// update the selections in a database
} else {
// nothing selection so just record this in a variable
$selectedItems = 'no selected items were made';
}
isset($_SESSION['selectedItemIDs'])
=> it will check is this parameter is set or not which is correct. isset($_SESSION['selectedItemIDs'])
=>它将检查是否设置了此参数。
$_SESSION['selectedItemIDs'] !== ''
=> this will exactly compare that your parameter type is not '' but here i guess your selectedItemIDs is array so it let you go inside $_SESSION['selectedItemIDs'] !== ''
=>这将完全比较您的参数类型不是”,但在这里我猜您的selectedItemIDs是数组,因此它可以让您进入
You can check the count of this array. 您可以检查此数组的计数。
if (isset($_SESSION['selectedItemIDs']) && count($_SESSION['selectedItemIDs']) > 0) {
// update the selections in a database
} else {
// nothing selection so just record this in a variable
$selectedItems = 'no selected items were made';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.