This is a discussion on !$subject or $subject = "" within the PHP Language forums, part of the PHP Programming Forums category; Hi Gurus Can I be confident that, when I write: !$variable This also equates to $variable == 0 or $variable = "&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi Gurus
Can I be confident that, when I write: !$variable This also equates to $variable == 0 or $variable = "" or should I check for those conditions separately. I know this is a real beginner's question, but I am paranoid that I may get "false positives" or whatever you call that. Right now, I write if(!$s || $s = "") for strings and if(!$s || $s = 0) for numbers. Thank you in advance - Nicolaas |
|
|||
|
Hey,
Well if you are looking to see if they are null (or not set) use if(!isset($var)) i usually use if(!isset($var) && $var == "") Phil WindAndWaves wrote: > Hi Gurus > > Can I be confident that, when I write: > > !$variable > > This also equates to $variable == 0 or $variable = "" or should I check for those conditions separately. > > I know this is a real beginner's question, but I am paranoid that I may get "false positives" or whatever you call that. Right now, > I write if(!$s || $s = "") for strings and if(!$s || $s = 0) for numbers. > > Thank you in advance > > > > - Nicolaas |
|
|||
|
"WindAndWaves" <access@ngaru.com> wrote in
news:0DyNd.15471$mo2.1227248@news.xtra.co.nz: > Hi Gurus > > Can I be confident that, when I write: > > !$variable > > This also equates to $variable == 0 or $variable = "" or should I > check for those conditions separately. > > I know this is a real beginner's question, but I am paranoid that I > may get "false positives" or whatever you call that. Right now, I > write if(!$s || $s = "") for strings and if(!$s || $s = 0) for > numbers. Yes, currently all of those conditions will evaluate the same. Because PHP does not have strict variable typing, a variable will evaluate false if it is unset, set to the null string, set to 0, or set to false. There is nothing wrong with your double-checks, but you could shorten your code to simply "if(!$s)" regardless of whether you're checking for a set variable, a string, a number, or a boolean. You can test using the following code: <?php if(!$var){ echo "No1!\n"; } $var = ''; if(!$var){ echo "No2!\n"; } $var = 0; if(!$var){ echo "No3!\n"; } $var = false; if(!$var){ echo "No4!\n"; } ?> Output: $ php test.php No1! No2! No3! No4! hth -- Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg'); --------------------------|--------------------------------- <http://www.phplabs.com/> | PHP scripts, webmaster resources |
|
|||
|
"Senator Jay Billington Bulworth" <f@fung.arg> wrote in message news:Xns95F72C3F1D5F0CANDLETRUCK@65.24.7.50... > "WindAndWaves" <access@ngaru.com> wrote in > news:0DyNd.15471$mo2.1227248@news.xtra.co.nz: > > > Hi Gurus > > > > Can I be confident that, when I write: > > > > !$variable > > > > This also equates to $variable == 0 or $variable = "" or should I > > check for those conditions separately. > > > > I know this is a real beginner's question, but I am paranoid that I > > may get "false positives" or whatever you call that. Right now, I > > write if(!$s || $s = "") for strings and if(!$s || $s = 0) for > > numbers. > > Yes, currently all of those conditions will evaluate the same. > > Because PHP does not have strict variable typing, a variable will > evaluate false if it is unset, set to the null string, set to 0, or set > to false. There is nothing wrong with your double-checks, but you could > shorten your code to simply "if(!$s)" regardless of whether you're > checking for a set variable, a string, a number, or a boolean. > > You can test using the following code: > > <?php > if(!$var){ > echo "No1!\n"; > } > $var = ''; > if(!$var){ > echo "No2!\n"; > } > $var = 0; > if(!$var){ > echo "No3!\n"; > } > $var = false; > if(!$var){ > echo "No4!\n"; > } > > ?> > > Output: > > $ php test.php > No1! > No2! > No3! > No4! > > hth > Thanks a lot Senator... |