I have following code:
var_dump($attr['value']);
if ($attr['value'] != 0 && $attr['value'] != null) {
echo $attr['value'];
$i++;
}
The problem is that dump tells me the $attr['value'] is 8 characters long string but the if condition fails and the code doesn't show me the value of $attr['value'] inside the if block. Is the condition wrong? I don't want to check the length of the string cause sometimes it can be also 1 character long string.
Note that any non-numeric string will be converted to 0
when compared to integer unless you use the ===
operator.
Try this:
var_dump('a' == 0); // true
var_dump('a' === 0); // false
The solution you are searching for is:
if(isset($attr['value']) && !empty($attr['value'])) ...
try
if(!empty($attr['value'])){
This condition :
if ($attr['value'] != 0)
Verifies if $attr['value'] CONTAINS 0, this is NOT the length of the string.
If you want to verify the string length, modify your code to this :
var_dump($attr['value']);
if (strlen($attr['value']) != 0 && $attr['value'] != null)
{
echo $attr['value'];
$i++;
}
strlen($variable) will verify the length of the string :
http://php.net/manual/fr/function.strlen.php
It seems that you are simply checking if the variable is set, in that case, you could simplify your code to this :
var_dump($attr['value']);
if ($attr['value'])
{
echo $attr['value'];
$i++;
}