By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

if (!0) means what?

P: n/a
Recently I made this mistake:

if (!0){
echo "I am true";
}
else {
echo "I am false";
}

It always evaluated to "true". What does (!0) equate to in plain language?
Why is it always true.

Al

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Adams-Blake Company" <at************@adams-blaketakeout.com> wrote in
message news:vs************@news20.forteinc.com...
Recently I made this mistake:

if (!0){
echo "I am true";
}
else {
echo "I am false";
}

It always evaluated to "true". What does (!0) equate to in plain language?
Why is it always true.

Al

Ones & Zeros, On or Off, True or False

!0 equates to not zero - which must then be 1 - or true

so, if (true) - is always true


Jul 17 '05 #2

P: n/a
Adams-Blake Company wrote:
Recently I made this mistake:

if (!0){
echo "I am true";
}
else {
echo "I am false";
}

It always evaluated to "true". What does (!0) equate to in plain language?
Why is it always true.

Al


Have a look at: http://www.blueshoes.org/en/developer/php_cheat_sheet/

Looks like '0' always evaluates to false, so essentially you're just
returning 'not false' which is always true ;)

Jul 17 '05 #3

P: n/a
<xyzzy> wrote:

"Adams-Blake Company" <at************@adams-blaketakeout.com> wrote in
message news:vs************@news20.forteinc.com...
Recently I made this mistake:

if (!0){
echo "I am true";
}
else {
echo "I am false";
}

It always evaluated to "true". What does (!0) equate to in plain
language? Why is it always true.

Al

Ones & Zeros, On or Off, True or False

!0 equates to not zero - which must then be 1 - or true

so, if (true) - is always true


I had not thought of it in that way.

Thanks. And thanks to the person who posted the URL to the Cheat Sheet. Very
helpful.

Al

Jul 17 '05 #4

P: n/a
Not quite true. Boolean is a distinct type in PHP. ! is a boolean operator,
so 0 first get convert into boolean (to false), then the expression is
evaluated. The difference between 0 and false is illustrated by the
following examples:

// Example A
if (!"Bush") {
echo "I am true";
}
else {
echo "I am false";
}

// Example B
if ("Bush" == 0) {
echo "I am zero";
}
else {
echo "I am not zero";
}

Example A prints "I am false", because a non-empty string converts to true.
Example B on the other hand, prints "I am zero" because "Bush" converts to
0. Thus, we get the following logic:

false == 0
0 == "Bush"
false != "Bush"

In linear algebra I think you would say "PHP variables do not form a vector
space."

Uzytkownik <xyzzy> napisal w wiadomosci
news:Na********************@comcast.com...

"Adams-Blake Company" <at************@adams-blaketakeout.com> wrote in
message news:vs************@news20.forteinc.com...
Recently I made this mistake:

if (!0){
echo "I am true";
}
else {
echo "I am false";
}

It always evaluated to "true". What does (!0) equate to in plain language? Why is it always true.

Al

Ones & Zeros, On or Off, True or False

!0 equates to not zero - which must then be 1 - or true

so, if (true) - is always true

Jul 17 '05 #5

P: n/a

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:Cs********************@comcast.com...
Not quite true. Boolean is a distinct type in PHP. ! is a boolean operator, so 0 first get convert into boolean (to false), then the expression is
evaluated. The difference between 0 and false is illustrated by the
following examples:

// Example A
if (!"Bush") {
echo "I am true";
}
else {
echo "I am false";
}

// Example B
if ("Bush" == 0) {
echo "I am zero";
}
else {
echo "I am not zero";
}

Example A prints "I am false", because a non-empty string converts to true. Example B on the other hand, prints "I am zero" because "Bush" converts to
0. Thus, we get the following logic:

false == 0
0 == "Bush"
false != "Bush"

In linear algebra I think you would say "PHP variables do not form a vector space."

Uzytkownik <xyzzy> napisal w wiadomosci
news:Na********************@comcast.com...

"Adams-Blake Company" <at************@adams-blaketakeout.com> wrote in
message news:vs************@news20.forteinc.com...
Recently I made this mistake:

if (!0){
echo "I am true";
}
else {
echo "I am false";
}

It always evaluated to "true". What does (!0) equate to in plain language? Why is it always true.

Al

Ones & Zeros, On or Off, True or False

!0 equates to not zero - which must then be 1 - or true

so, if (true) - is always true


I disagree.
Yes, strings can evaluate to true and false if they are null or !null
However, zero is numeric until you convert it to Boolean by using the !

<?
$a = 0;

echo "a = $a\n";
echo "!a = " . !$a . "\n";
echo "is_bool(a) ".($a ? "true":"false") . "\n";
echo "is_bool(!a) ".(!$a ? "true":"false") . "\n";
?>

*** produces ***

a = 0
!a = 1
is_bool(a) =false
is_bool(!a) =true



Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.