471,892 Members | 1,937 Online

# if (!0) means what?

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
5 20068

message news:vs************@news20.forteinc.com...

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

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
<xyzzy> wrote:

message news:vs************@news20.forteinc.com...

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

Al

Jul 17 '05 #4
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."

news:Na********************@comcast.com...

message news:vs************@news20.forteinc.com...

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

"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."

news:Na********************@comcast.com...

message news:vs************@news20.forteinc.com...

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.