[SOLVED] ! (NOT) Operator not printing 0 !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] ! (NOT) Operator not printing 0 !

Post by anjanesh »

Code: Select all

$var = 0;
echo !var; // Ouputs 1
$var = 1;
echo !var; // Does not Ouput 0 - I want it to
I guess 0 is not printed because its value becomes false ?
Any way to output 0 without using conditional statements ?
Thanks
Last edited by anjanesh on Wed Mar 30, 2005 1:30 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo ((int) !1);
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Thanks. This is solved.
BTW, why doesnt type casting work the other way round as in C/C++ ?
int(!1)
PHP is trying the search for function int instead of typecasting it.
All other languages seem to work with int()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

C can do either way. The major reason would probably be that PHP doesn't have typing like they do.
Post Reply