473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

if (!0) means what?

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: badz | last post by:
Hi friends I'm still exploring in PHP programming with little knowledge on C .. the mother of programming language.. Just wonder .. some times we will see some pepole use :: singn in their...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
2
by: GregM | last post by:
Hi First off I'm not using anything from Twisted. I just liked the subject line :) The folks of this list have been most helpful before and I'm hoping that you'll take pity on a the dazed and...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
6
by: Just Me | last post by:
What does "Set not supported at runtime" in the Task List mean? Thanks in advance for any info
8
by: Narf the Mouse | last post by:
I'm currently working on a roguelike as an excercise in learning c++. Anyway, I've set up a script parser to check a text file to find out how many races there are, then dynamically alocate an...
9
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <=...
2
by: Garg | last post by:
if we assign array.length = 0 then what does it mean? Are we setting the length of the array equal to 0 or are we selecting the first element of the array? Please help me on this..
3
by: Revathi Balakrishnan | last post by:
Hi All I am very new to javascript world. But i need to debug a javascript code. i wanted to know what does the below code mean? if(this.opener) { } Please help me. i am in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.