Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Using booleans in PHP

Posted by enquirer (enquirer), 1 August 2002
I'm having a problem with setting/testing boolean variables.

I have:

$addressnext = false ;

if etc...
elseif ($addressnext) {
       $address = strip_tags($buffer) ;
       $addressnext = false ;
       }
etc...

The elseif line returns a parse error.

http://uk.php.net/manual/en/language.types.boolean.php

does not realy explain but does have a comment in the third user contributed notes, " True and false are no constants since PHP4 but values of the type boolean."

Obviously there are work-arounds but how would you use true/false flags?

Posted by admin (Graham Ellis), 1 August 2002
I rather suspect that the syntax error is on the line prior to the "elseif", in the code you have replaced by "etc...", but the error wasn't apparent to PHP until it hit "elseif".   Suggestion - check that you have a } at the end of the if block (and that the } that you though ended the if block actually ends a "while"), that your double quotes are balanced, etc.

In any case, I have written a demonstration that uses booleans that you might find useful to check you syntax (and perhaps learn one or two other things from, too!) .... the example generates a couple of random numbers, and sets a boolean to true if the first number is less that 50, and to false if the second number is greater than 50.   Since that doesn't catch all cases, I've used the === test to see if $bdemo is equal to false and has the same data type too;   that way, if my boolean has never been set I can distinguish it from a value of false.

#!/usr/bin/php -q
A demonstration of booleans in PHP ....
<?php
function make_seed() {
      list($usec, $sec) = explode(' ', microtime());
      return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());

$randval1 = rand(0,100);
$randval2 = rand(0,100);

if ($randval1 < 50) $bdemo = true;
if ($randval2 > 50) $bdemo = false;

if ($bdemo) {
       print ("First value is less than 50\n");
} elseif ($bdemo === false) {
       print ("second value is greater than 50\n");
} else {
       print ("First value is 50 or more, and second is 50 or less\n");
       }

print "Values were $randval1 and $randval2\n";
?>


Here's the result of that program running;  the first line lets this run as a stand alone program, and the -q option on that line ensures that the usual HTTP headers are supressed.

$ ./booly
A demonstration of booleans in PHP ....
second value is greater than 50
Values were 20 and 100
$ ./booly
A demonstration of booleans in PHP ....
second value is greater than 50
Values were 95 and 57
$ ./booly
A demonstration of booleans in PHP ....
First value is 50 or more, and second is 50 or less
Values were 54 and 32
$ ./booly
A demonstration of booleans in PHP ....
First value is less than 50
Values were 40 and 36
$




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho