!$subject or $subject = ""

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-07-2005
WindAndWaves
 
Posts: n/a
Default !$subject or $subject = ""

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


Reply With Quote
  #2 (permalink)  
Old 02-07-2005
Page12
 
Posts: n/a
Default Re: !$subject or $subject = ""

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


Reply With Quote
  #3 (permalink)  
Old 02-08-2005
Senator Jay Billington Bulworth
 
Posts: n/a
Default Re: !$subject or $subject = ""

"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
Reply With Quote
  #4 (permalink)  
Old 02-08-2005
WindAndWaves
 
Posts: n/a
Default Re: !$subject or $subject = ""


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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:34 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0