if condition evaluation in php [closed]
$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";
}
精彩评论