Overwrite value of true or false in PHP

Overwrite value of true or false in PHP

am 07.09.2009 21:14:30 von Bpejman

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

Re: Overwrite value of true or false in PHP

am 07.09.2009 21:19:28 von Ashley Sheridan

On Mon, 2009-09-07 at 19:14 +0000, 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

Re: Overwrite value of true or false in PHP

am 07.09.2009 21:43:59 von Martin Scotta

--00c09f90573334d6b704730212c4
Content-Type: text/plain; charset=ISO-8859-1

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

--00c09f90573334d6b704730212c4--

Re: Overwrite value of true or false in PHP

am 07.09.2009 21:46:56 von Martin Scotta

--001485e76f16cc24c40473021c8f
Content-Type: text/plain; charset=ISO-8859-1

On Mon, Sep 7, 2009 at 4:19 PM, Ashley Sheridan wrote:

> On Mon, 2009-09-07 at 19:14 +0000, 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

--001485e76f16cc24c40473021c8f--

Re: Overwrite value of true or false in PHP

am 07.09.2009 22:18:15 von Eddie Drapkin

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 in=
t.  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 strin=
g.
>
> 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.ph p and the
various things linked therein.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Overwrite value of true or false in PHP

am 07.09.2009 22:56:00 von Bpejman

--part9944-boundary-707624214-473517509
Content-Transfer-Encoding: base64
Content-Type: text/plain; charset="Windows-1252"

SSBzZWUgd2hhdCdzIGdvaW5nIG9uIG5vdy4gIEJlY2F1c2Ugb2YgdGhlIHR5 cGUgY29udmVyc2lv
biwgSSBhbSB3cml0aW5nIG15IGNvZGUgc3VjaCB0aGF0IG15IHJldHVybiBj b2RlcyBhcmUgdHJh
bnNsYXRlZCB0byBhIHN0cmljdCAxIG9yIDAuICBUaGUgaWRlYSBvZiBoYXZp bmcgYW55dGhpbmcg
b3RoZXIgdGhhbiAnJyBvciAwIHRyYW5zbGF0aW5nIHRvIGEgdHJ1ZSBzY2Fy ZXMgbWUgYSBsaXR0
bGUgYnV0IHRoYW5rcyBmb3IgcG9pbnRpbmcgb3V0IHR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