Why this condition execute?

Hi guys,

Why the if condition below the $record['photo_url] = 0; //assigned to zero
the condition below keeps printing the image?
I have these codes below,


        <?php
	if($record['photo_url'] != '' && $record['photo_url'] != 0)
	{
	?>
		<a class="image-popup-vertical-fit" href="<?php echo base_url().'images/'.$record['photo_url']; ?>" rel="">
			<img src="<?php echo base_url() . 'icons/' . 'camera.jpg'; ?>" alt="<?php echo $record['title']; ?>" />
		</a>
	<?php
	}
        ?>

It’s not suppose to print the image when the condition is blank or zero.
But unfortunately it’s not filtering properly the condition.
Anyone would like to help please.

What is wrong with my if() condition?

Thanks in advnace.


if($record['photo_url'] != '' || $record['photo_url'] != 0)

This will evaluate the rest of the block (between “{” and “}”), and display the image, only if the field IS NOT EQUAL to nothing OR it IS NOT EQUAL to Zero

Realize there is a difference between ZERO and ‘0’.

You can include this code to view the actual value of $record[‘photo_url’]


var_dump($record['photo_url']);

@ParkinT ;
Thanks dude for your effort.

But I have solved the problem.

The solution:


if($record['photo_url'] != '' && $record['photo_url'] != '0')

Just for info, you could also rewrite your IF check like this:

if( ! empty($record['photo_url']) )

as [fphp]empty[/fphp] returns true for empty strings and 0 values (among other things). You could also write:

if( $record['photo_url'] )

as any non-empty/null, non-zero value will be evaluated to TRUE.

I am happy to see you solved the problem.
Thank you, also, for taking the time to post your resolution here. That is often overlooked/neglected and it can be an immense help to someone who - some time in the future - discovers this thread while struggling with a similar problem.

Good point @fretburner;. Would that apply here, though? Where there is also a value of ‘0’ (ASCII character 48) that represents null.

Yeah that would be considered empty too. According to the docs, all these values are considered to be empty:

  • “” (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • “0” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)