p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: if(!empty($varible) excludes zeros. help?


Message #1 by danostrowski@h... on Fri, 22 Feb 2002 20:25:44
Ok.. When I take user input from a form and then pass it to another part 

of my script or another script, the number 0 is considered empty.  



This poses a problem since some of my scripts, for instance, will take 

numbers to modify, say, standings.





So far, I have been passing variables by one script to another, but some 

times some of the information is OPTIONAL.  Therefore, I merely check to 

see if it is empty to determine whether or not I should enter it into the 

database.



However, what If I need to enter zero?  It's a number, but it counts as 

being empty, so prolbems ensue when I try to pass a 0 to my script.  It 

sees it is empty and acts accordingly.



Is there a workaround for this??



Thanks, 



Dan  



P.S. - The email I have listed is old and doesn't work anymore.  My new 

one is dan@t...
Message #2 by tonsil@s... on Fri, 22 Feb 2002 16:05:03 -0800
What about using isset()?



On 22 Feb 2002, at 20:25, danostrowski@h... wrote:



> Ok.. When I take user input from a form and then pass it to another part 

> of my script or another script, the number 0 is considered empty.  

> 

> This poses a problem since some of my scripts, for instance, will take 

> numbers to modify, say, standings.

> 

> 

> So far, I have been passing variables by one script to another, but some 

> times some of the information is OPTIONAL.  Therefore, I merely check to 

> see if it is empty to determine whether or not I should enter it into the 

> database.

> 

> However, what If I need to enter zero?  It's a number, but it counts as 

> being empty, so prolbems ensue when I try to pass a 0 to my script.  It 

> sees it is empty and acts accordingly.

> 

> Is there a workaround for this??

> 

> Thanks, 

> 

> Dan  

> 

> P.S. - The email I have listed is old and doesn't work anymore.  My new 

> one is dan@t...




> 





Message #3 by danostrowski@h... on Fri, 22 Feb 2002 21:39:13
> What about using isset()?





isset() checks to see whether or not a variable is present, unless I am 

incorrect.  if you use a HTML form and pass a varible without modification 

(ie. - you just leave a form field blank) then a variable is STILL PASSED. 

it just contains a NULL value or "".  





Correct or incorrect?? I will try with isset() but I don't think it works 

that way..



/boggle



Dan
Message #4 by "sneax" <sneaker@p...> on Fri, 22 Feb 2002 22:34:41 +0100
Is

empty()

a build-in function?

Or did you make it yourself?

I'd use

isset()

, that's much better i guess ...



Greetz, sneax





----- Original Message -----

From: <tonsil@s...>

To: "beginning php" <beginning_php@p...>

Sent: Saturday, February 23, 2002 1:05 AM

Subject: [beginning_php] Re: if(!empty($varible) excludes zeros. help?





> What about using isset()?

>

> On 22 Feb 2002, at 20:25, danostrowski@h... wrote:

>

> > Ok.. When I take user input from a form and then pass it to another part

> > of my script or another script, the number 0 is considered empty.

> >

> > This poses a problem since some of my scripts, for instance, will take

> > numbers to modify, say, standings.

> >

> >

> > So far, I have been passing variables by one script to another, but some

> > times some of the information is OPTIONAL.  Therefore, I merely check to

> > see if it is empty to determine whether or not I should enter it into

the

> > database.

> >

> > However, what If I need to enter zero?  It's a number, but it counts as

> > being empty, so prolbems ensue when I try to pass a 0 to my script.  It

> > sees it is empty and acts accordingly.

> >

> > Is there a workaround for this??

> >

> > Thanks,

> >

> > Dan

> >

> > P.S. - The email I have listed is old and doesn't work anymore.  My new

> > one is dan@t...




$subst('Email.Unsub').

> >

>

>

>




$subst('Email.Unsub').

>



Message #5 by "sneax" <sneaker@p...> on Fri, 22 Feb 2002 23:26:09 +0100
I think that just checking if a variable has something in it is just

checking if its content is "" or not (when its "", it's empty)



That's how i check my vars for any content!



Greetz, sneax





----- Original Message -----

From: <danostrowski@h...>

To: "beginning php" <beginning_php@p...>

Sent: Friday, February 22, 2002 9:39 PM

Subject: [beginning_php] Re: if(!empty($varible) excludes zeros. help?





> > What about using isset()?

>

>

> isset() checks to see whether or not a variable is present, unless I am

> incorrect.  if you use a HTML form and pass a varible without modification

> (ie. - you just leave a form field blank) then a variable is STILL PASSED.

> it just contains a NULL value or "".

>

>

> Correct or incorrect?? I will try with isset() but I don't think it works

> that way..

>

> /boggle

>

> Dan




$subst('Email.Unsub').

>



Message #6 by "Nikolai Devereaux" <yomama@u...> on Fri, 22 Feb 2002 14:58:11 -0800

The problem you're having is because empty is traditionally used to test

strings or arrays, not integers.



Sadly, I think that this returns true:



  $var = "0";

  empty($var);





> isset() checks to see whether or not a variable is present, unless I am

> incorrect.  if you use a HTML form and pass a varible without

> modification (ie. - you just leave a form field blank) then a

> variable is STILL PASSED.  it just contains a NULL value or "".



I dunno about this...



<testing> . . . .</testing>



Okay -- radio buttons and checkboxes don't get passed with no value, they're

simply not passed.  Inputs which prompt the user for text are still sent.

This is true for type = text, textfield, and textarea.  (textfield and

textarea are older input types, but I figured what the heck)



Since we can't count on empty() returning us a useful answer, try this:



if( isset($var) && ($var != "") )

{

    //valid

}



now if you wanted all the INVALID items, it's just a quick romp through

DeMorgan's law for negating the above boolean expression:



if( !isset($var) || ($var == "") )

{

    // invalid.

}



bingoriffic.  hope this helps,



nik







> -----Original Message-----

> From: danostrowski@h... [mailto:danostrowski@h...]

> Sent: Friday, February 22, 2002 8:26 PM

> To: beginning php

> Subject: [beginning_php] if(!empty($varible) excludes zeros. help?

>

>

> Ok.. When I take user input from a form and then pass it to another part

> of my script or another script, the number 0 is considered empty.

>

> This poses a problem since some of my scripts, for instance, will take

> numbers to modify, say, standings.

>

>

> So far, I have been passing variables by one script to another, but some

> times some of the information is OPTIONAL.  Therefore, I merely check to

> see if it is empty to determine whether or not I should enter it into the

> database.

>

> However, what If I need to enter zero?  It's a number, but it counts as

> being empty, so prolbems ensue when I try to pass a 0 to my script.  It

> sees it is empty and acts accordingly.

>

> Is there a workaround for this??

>

> Thanks,

>

> Dan

>

> P.S. - The email I have listed is old and doesn't work anymore.  My new

> one is dan@t...




> $subst('Email.Unsub').



Message #7 by danostrowski@h... on Fri, 22 Feb 2002 23:19:37
w00terific!  I will give it a go!



Thanks!



Dan

dan@t...
Message #8 by "Lawrence" <lkrubner@g...> on Sat, 23 Feb 2002 17:18:46 -0500
From: "Nikolai Devereaux" <yomama@u...>

> Since we can't count on empty() returning us a useful answer, try this:

>

> if( isset($var) && ($var != "") )

> {

>     file://valid

> }

>

> now if you wanted all the INVALID items, it's just a quick romp through

> DeMorgan's law for negating the above boolean expression:

>

> if( !isset($var) || ($var == "") )

> {

>     // invalid.

> }





Thank god for Google. For everyone out there who long ago forgot what DeMorgan's law was (I'm not

sure I ever knew in the first place, but if I did, I'm sure I forgot it ten years ago) I tracked

down this:



http://www.cs.utah.edu/classes/cs3100/handouts-Spring-2001/lec2/notes.txt



1.2.2 Reduction to definitions



This idea is being illustrated in the context of sets and proofs

about sets.



A very important set of ideas is getting introduced right here,

namely that relating to "IF-THEN". Let me explain this a great deal.



ALL THESE are equivalent facts:

   + If A then B

   ? A only if B

   . B if A

   ++ A implies B

   ?? Whenever A holds, B follows



Logically, we say "A => B" or "A implies B"



       This means "not(A) or B"



Thus, our quiz assertion (x /\ y) => y simplies thus:



      (x /\ y) => y



 =     !(x /\ y) \/ y   -- definition of =>

 =     (!x \/ !y) \/ y  -- demorgan's law



 =     !x \/ !y \/ y   -- bool alg



 =     !x \/ true     -- bool alg



 = true.       -- bool alg.



In other words, (x /\ y) => y is a tautology - i.e. true regardless

of x and y.



Well, if so, we should have the case that



      (false /\ false) => false



is true. Well, grind thru and you will see this.



"IF moon is made of cheese THEN horses can fly" is true because

both 'moon is made of cheese' and 'horses can fly' are false.


















  Return to Index