Re: [PHP] Overwrite value of true or false in PHP
I see what's going on now. Because of the type conversion, I am writing my code such that my return codes are translated to a strict 1 or 0. The idea of having anything other than '' or 0 translating to a true scares me a little but thanks for pointing out the === operator. I had to rewrite a lot of code after discovering it. Good times... Thanks for clarifying everyone. -Original Message- From: Martin Scotta Date: Mon, 7 Sep 2009 16:43:59 To: Cc: Subject: Re: [PHP] Overwrite value of true or false in PHP On Mon, Sep 7, 2009 at 4:14 PM, Bobby Pejman wrote: > Hi, > > I noticed that the following returns a 1. > > echo (1<2) ? True : False > > I was under the impression that true/false are of type boolean and not int. > But in php anything other than 0 translates to true, meaning a 1. What I > am trying to achieve is for a 1 to be a 1 and a true or false to be a true > and false respectively and I do not want to put quotes around the word > true/false either because then it is no longer a boolean but string. > > Is it possible to overwrite php's true/false and declare them as boolean? > You often see in C++, some use Define(). > > Thanks. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Yes *true* and *false* are booleans. PHP convert natively different types to boolean in order to help write sentences like this if( $object ) { $object->something(); } while( $data = mysql_fetch_assoc( $resource ) ) { print_r( $data ); } But we you try to print a boolean variable PHP convert it to string: true becomes '1' and false becomes ''. That's why you can't just echo a boolean variable to see the value, but you can rely on var_dump to show this. $bool = true; echo $bool; var_dump( $bool ); $bool =! $bool; echo $bool; var_dump( $bool ); As other languages PHP as an special operator for checking types while comparing values. $a = 'false'; $b = true; var_dump( $a == $b, # <-- true $a === $b # <-- false ); This happens because values are converted before comparison so, 'false' becomes true. PHP converts everything different to an empty string as *true* This also affect any type of variable. -- Martin Scotta
Re: [PHP] Overwrite value of true or false in PHP
On Mon, Sep 7, 2009 at 3:14 PM, Bobby Pejman wrote: > Hi, > > I noticed that the following returns a 1. > > echo (1<2) ? True : False > > I was under the impression that true/false are of type boolean and not int. > But in php anything other than 0 translates to true, meaning a 1. What I am > trying to achieve is for a 1 to be a 1 and a true or false to be a true and > false respectively and I do not want to put quotes around the word true/false > either because then it is no longer a boolean but string. > > Is it possible to overwrite php's true/false and declare them as boolean? > You often see in C++, some use Define(). > > Thanks. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > It might be pertinent for you to read the section of the manual dealing with type juggling: http://us3.php.net/manual/en/language.types.type-juggling.php and the various things linked therein. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Overwrite value of true or false in PHP
On Mon, Sep 7, 2009 at 4:19 PM, Ashley Sheridan wrote: > On Mon, 2009-09-07 at 19:14 +, Bobby Pejman wrote: > > Hi, > > > > I noticed that the following returns a 1. > > > > echo (1<2) ? True : False > > > > I was under the impression that true/false are of type boolean and not > int. But in php anything other than 0 translates to true, meaning a 1. > What I am trying to achieve is for a 1 to be a 1 and a true or false to be > a true and false respectively and I do not want to put quotes around the > word true/false either because then it is no longer a boolean but string. > > > > Is it possible to overwrite php's true/false and declare them as boolean? > You often see in C++, some use Define(). > > > > Thanks. > > > The statement you gave does return a boolean value, but what you are > doing is printing the resulting value out. PHP converts the boolean to > the nearest printable value, which will be a 1 or 0. If you want it to > output the value 'True' or 'False' then you need to output a string > value. > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > I think php convert the boolean variable to string before print its content. That's why... echo true; # prints '1' echo false; # does not prints anything, or prints '' $bool = false; var_dump( (string) $bool, (int) $bool ); -- Martin Scotta
Re: [PHP] Overwrite value of true or false in PHP
On Mon, Sep 7, 2009 at 4:14 PM, Bobby Pejman wrote: > Hi, > > I noticed that the following returns a 1. > > echo (1<2) ? True : False > > I was under the impression that true/false are of type boolean and not int. > But in php anything other than 0 translates to true, meaning a 1. What I > am trying to achieve is for a 1 to be a 1 and a true or false to be a true > and false respectively and I do not want to put quotes around the word > true/false either because then it is no longer a boolean but string. > > Is it possible to overwrite php's true/false and declare them as boolean? > You often see in C++, some use Define(). > > Thanks. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Yes *true* and *false* are booleans. PHP convert natively different types to boolean in order to help write sentences like this if( $object ) { $object->something(); } while( $data = mysql_fetch_assoc( $resource ) ) { print_r( $data ); } But we you try to print a boolean variable PHP convert it to string: true becomes '1' and false becomes ''. That's why you can't just echo a boolean variable to see the value, but you can rely on var_dump to show this. $bool = true; echo $bool; var_dump( $bool ); $bool =! $bool; echo $bool; var_dump( $bool ); As other languages PHP as an special operator for checking types while comparing values. $a = 'false'; $b = true; var_dump( $a == $b, # <-- true $a === $b # <-- false ); This happens because values are converted before comparison so, 'false' becomes true. PHP converts everything different to an empty string as *true* This also affect any type of variable. -- Martin Scotta
Re: [PHP] Overwrite value of true or false in PHP
On Mon, 2009-09-07 at 19:14 +, Bobby Pejman wrote: > Hi, > > I noticed that the following returns a 1. > > echo (1<2) ? True : False > > I was under the impression that true/false are of type boolean and not int. > But in php anything other than 0 translates to true, meaning a 1. What I am > trying to achieve is for a 1 to be a 1 and a true or false to be a true and > false respectively and I do not want to put quotes around the word true/false > either because then it is no longer a boolean but string. > > Is it possible to overwrite php's true/false and declare them as boolean? > You often see in C++, some use Define(). > > Thanks. > The statement you gave does return a boolean value, but what you are doing is printing the resulting value out. PHP converts the boolean to the nearest printable value, which will be a 1 or 0. If you want it to output the value 'True' or 'False' then you need to output a string value. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php