LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2010, 06:35 PM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
PHP Booleans:


Hello All,

I have this function that is supposed to print draw the first image from a function then hide the rest of the images using 'display: none' however for some reason the boolean test does not seem to work on one of my servers however it works on another, is there some php.ini setting I am missing or can you see any issues with the following code:

Code:
        $FIRST = TRUE;          
    
        for($i = 0; $i<count($imageID); $i++)
          {                             
          $id = $imageID[$i];           
                                            
          echo "<a";   
// The following IF statement fails, even when it is $FIRST==FALSE                 
            if(!$FIRST)                         
              {
    // THIS CODE IS ALWAYS EXECUTED.                                         
              echo  " style = 'display: none;'";        
              }                                         
          echo "                        
            rel='example_group'
            title='Custom title'
            href='getimage.php?id=$id'>
            
            <img
              src = 'getimage.php?id=$id'
              style   = 'border:      none'
              width   = '60px'
              height  = '60px'
              />";
                                            
          $FIRST = FALSE;               
          }

Please can you tell me why that statement is always executed on one server and works correctly on another?

Many thanks in advance.
trscookie.
 
Old 07-29-2010, 07:01 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
PHP implementation of booleans is flawed in my opinion, but that is because of the non-typedness of the language. Therefore the '===' operator has been created. Which doesn't work for me half of the time either.

A variable is not false when it has a non-zero value. So if $FIRST = FALSE; , $FIRST is a non-zero value and therefor true!

I would not rely on this mechanism. A few alternatives:
PHP Code:
$FIRST=TRUE;
if (
$FIRST === TRUE
Not recommended because I have seen in failing.

PHP Code:
$FIRST=1
if ($FIRST
Will work, but relies on the implementation of boolean evaluation.

PHP Code:
$FIRST=1
if ($FIRST == 1
Best. Unambiguous. Works always.

jlinkels
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Installing Nginx With PHP 5.3 And PHP-FPM On Ubuntu Lucid Lynx (10.04) LXer Syndicated Linux News 0 06-14-2010 11:42 PM
booleans mintoo4linux Linux - Newbie 4 06-20-2008 08:39 AM
problem with booleans yadava Linux - Newbie 5 04-18-2008 09:16 AM
Load Booleans?? maestro52 Linux - Newbie 11 08-27-2005 10:18 PM
php apache or php cgi - php learner rblampain Linux - Security 3 12-17-2004 11:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration