Gayner Posted July 29, 2009 Share Posted July 29, 2009 This is my simple code. FIrst of all Hi everyone I am a little lost of some php if function here: if (!$ibforums->member['view_img']) { echo "OH HELLO!"; } So My Variable $ibforums->member['view_img'] in my database means = 1 So If I replace $ibforums->member['view_img'] to 1 it would be: if (!1) { echo "HELLO!"; } I dont get it? What is the ! for ? Thanks in advance. so if it's !0 it means dont show? Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/ Share on other sites More sharing options...
Zyx Posted July 29, 2009 Share Posted July 29, 2009 It's one of the logical operators: http://docs.php.net/manual/en/language.operators.logical.php It simply negates the logical value of an expression. PS. "if" is not a function . Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/#findComment-885696 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 the "!" in front very simply means NOT... so for if(!1), or any other non-zero number, the if statement will always evaluate to false... with if(!0), it will evaluate to true... so i guess in this sense it kinda just means the opposite, where 0 = false, and any non-zero number true, and the ! just evaluates to the opposite of that... hope this helps... and i wrote that right, lol... Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/#findComment-885699 Share on other sites More sharing options...
Gayner Posted July 29, 2009 Author Share Posted July 29, 2009 the "!" in front very simply means NOT... so for if(!1), or any other non-zero number, the if statement will always evaluate to false... with if(!0), it will evaluate to true... so i guess in this sense it kinda just means the opposite, where 0 = false, and any non-zero number true, and the ! just evaluates to the opposite of that... hope this helps... and i wrote that right, lol... What if I put a !2 what would that do? Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/#findComment-885701 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 that would be a non-zero number, so it would be the same as 1 or 5 or 23421 Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/#findComment-885703 Share on other sites More sharing options...
Gayner Posted July 29, 2009 Author Share Posted July 29, 2009 that would be a non-zero number, so it would be the same as 1 or 5 or 23421 So these ! only works with 1 and 0 ? lol but this is a very easy way to do if else statements if they want value 1 or 0 right ? lol Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/#findComment-885713 Share on other sites More sharing options...
lynxus Posted July 29, 2009 Share Posted July 29, 2009 <?php I use it for things like , rather than,. if ($foo == "0") { }else{ do stuff; } i use: if ($foo != "0") { // if $foo does not equal 0 do stuff. Rather than if foo equals 0 do nothing else do stuff. do stuff; } ?> Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/#findComment-885761 Share on other sites More sharing options...
Zyx Posted July 29, 2009 Share Posted July 29, 2009 if statement requires a boolean value: true or false. 0 and 1 are integers and are automatically converted, as well as all other types you'll try to use here. The manual shows the conversion rules: what is converted to false and what - to true: Boolean type. We can see that "0" is converted to false and any other number - to "true". Link to comment https://forums.phpfreaks.com/topic/167924-if-0-what-does-it-do/#findComment-885764 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.