2011-04-28 71 views
0

嗨,大家好,我的if语句不起作用。我想基本上什么都不做,数组是空的,或者如果它有值,就做些什么。所有这一切发生的是“没有选择的额外”即使我选择演员php空数组if语句

if (empty($extras)) { 
    $message .="<br /> No extras selected <br />"; 
    } else { 
    foreach($_POST['extras'] as $extra) 
    { 
     $message .="<br />Extras: ".$extra."<br />"; 

    } 
    } 
+0

是的,它是复选框的数组 – Simon 2011-04-28 12:51:23

回答

4

我假设这是一个错字,作为根据您的代码$extras从未设置。

您应该使用

if (empty($_POST['extras'])) { 
0

我没有测试过这显示,但是你可以尝试if(!isset($extras[0]))

+0

没了,仍然dosent工作的帮助队友 – Simon 2011-04-28 12:47:26

1

可能$extras变量是空的,但你通过$_POST['extras']迭代这是不一样的。

+0

由于是存储在$ _ POST – Simon 2011-04-28 13:00:52

0

什么

if (empty($_POST['extras'])) { ... } 

您以前是否声明过$extras变量?

+0

$演员 – Simon 2011-04-28 12:48:54

0
if(!empty($array)){ 
//do something 
}else{ 
//donothing 

} 

请试试这个,......

0

您可以用is_array()功能也试试。文档here

0

如果您分配一个空数组到$extras变量在你的代码(例如$extras = array(),那么你基本上检查$extras分配,而不是分配给它的数组的长度。

这应该为你工作:

if(count($extras) == 0) { 
    // Your code here... 
} 
0

我觉得你的代码应该是这样的:

if (array_key_exists('extras', $_POST) and // check that the element exists in the $_POST array 
    !empty($_POST['extras'])) { // check that it is not empty 
    foreach ($_POST['extras'] as $extra) { 
     $message .="<br />Extras: " . $extra . "<br />"; 
    } 
} else { 
    $message .="<br /> No extras selected <br />"; 
} 

当前代码中的逻辑是向后的。

+0

干杯堆的帮助复选框数组 – Simon 2011-04-28 13:01:16