SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    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,
    Code:
            <?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.
    Quality codes are optimized and tested...
    Click here for inspiration..

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,337
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Code:
    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']
    Code:
    var_dump($record['photo_url']);
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @ParkinT ;
    Thanks dude for your effort.

    But I have solved the problem.

    The solution:
    Code:
    if($record['photo_url'] != '' && $record['photo_url'] != '0')
    Quality codes are optimized and tested...
    Click here for inspiration..

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,410
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Just for info, you could also rewrite your IF check like this:
    PHP Code:
    if( ! empty($record['photo_url']) ) 
    as empty returns true for empty strings and 0 values (among other things). You could also write:
    PHP Code:
    if( $record['photo_url'] ) 
    as any non-empty/null, non-zero value will be evaluated to TRUE.

  5. #5
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,337
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    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.

    Quote Originally Posted by fretburner View Post
    ...as empty returns true for empty strings and 0 values (among other things).
    Good point @fretburner ;. Would that apply here, though? Where there is also a value of '0' (ASCII character 48) that represents null.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,410
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ParkinT View Post
    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)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •