开发者

if condition evaluation in php [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
$karthik=$_POST['myarray1'];

if($karthik=="karthik"){
echo "correct";
}
else{
echo "incorrect";
}

Here myarray1 is开发者_运维百科 a array variable has got value "karthik" it is showing result as incorrect


In the example you have given there is no way that the variable $karthik has the value 'karthik'. Do this before your if statement to confirm.

echo '$karthik.... ';
var_dump($karthik);
echo '$_POST["myarray1"]....';
var_dump($_POST['myarray1']);

You say

Here myarray1 is a array variable

This is slightly ambiguous, are you posting this from your page or is this variable coming from somewhere else?


You're suggesting that $_POST['myarray1'] is an array, this will evaluate to "Array" when used in string context. So, it's obvious that the condition $karthik=="karthix" is false, because "Array" does not equal "karthix".

If you've a field like:

<input name="myarray1[]" value="karthix">

you can check whether the field contains the value "karthix" by using the in_array function for checking whether a string is contained in the array or not:

$karthix = $_POST['myarray1'];
if (in_array("karthix", $karthix)) {
    echo "correct";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜