if ($xml) {
$emailId = $xml->mail->id;
$mailPassword = $xml->mail->password;
if (!empty($emailId) && !empty($mailPassword)) {
$data['emailIdandPasswordCheck'] = 0;
}
}
this is a condition in which we are checking the values are empty or not but it is not working
in this id is empty and password is not empty.
but everytime xml object is coming in both id and password
because of this both the things are coming as non empty.
what check i can use to check xml object is empty or not?
You can use cast an array and you check the condition
$emailId = (array)$emailId;
$mailPassword = (array)$mailPassword;
if (!empty($emailId) && !empty($mailPassword) ){
}
OR
$emailId = (array)$emailId;
$mailPassword = (array)$mailPassword;
if (count($emailId)==0 && count($mailPassword)==0 ){
}
Use isset()
instead of empty()
if (!isset($emailId) && !isset($mailPassword)) {
$data['emailIdandPasswordCheck'] = 0;
}
OK. Here is what your code is doing.
$emailId = $xml->mail->id;
This sets null in $emailId
if there is no value.
$mailPassword = $xml->mail->password;
And this sets null in $mailPassword
if there is no value.
And your condition says
if emailId is non-empty and mailPassword is non-empty
And using empty
always tells the conditions are true. Because empty
returns true
on null
. So instead using isset
will solve the issue.
You can see the comparison table for further info.
I think you want this:
if ($xml) {
$emailId = $xml->mail->id;
$mailPassword = $xml->mail->password;
if ( $emailId !=null && $emailId !="" && $mailPassword!=null && $mailPassword!="" ) {
$data['emailIdandPasswordCheck'] = 0;
}
}