[UPHPU] Setting a variable with an if statement?

Randy Moller zoomerz at comcast.net
Thu Mar 26 13:45:05 MDT 2009


if your description field is set to not null, then it should accept an 
empty string (mysql?), like this:

$description = !empty($_POST['description']) ? $_POST['description'] : '';

Otherwise, if you "must" have a value of a length > 0, then do something 
like this:

$description = !empty($_POST['description']) ? $_POST['description'] : 
'blank';

The empty function accounts for all boolean false and null values, like 
0,null,false, or "".
The construct () ? : ; is the ternary operator, and is very handy for 
writing concise code
when your conditional is an "if/else".

Hope that helps!

Randy

Caleb Call wrote:
> I'm trying to figure a way to set a variable with an if statement, however
> it's not working.  Here's my code:
>
> if ($_POST['description'] == ""){
>     $description='blank';
>     } else {
>     $description=$_POST['description'];
> }
>
> Description isn't a required field, but the stored procedure needs to have
> something passed for $description when it runs.  So I'm trying to set it so
> that if it's not entered then it will be passed as "blank".  Right now it's
> always being assigned as "blank" even if I enter something in the
> Description.
>
> What am I doing wrong in my if statement to cause it to always be set as
> "blank".
>
> Also, if I set $description=$_POST['description']; then everything passes
> correctly, unless it's left blank.
>
> Thanks,
> Caleb
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 8.0.238 / Virus Database: 270.11.29/2024 - Release Date: 03/26/09 07:12:00
>
>   



More information about the UPHPU mailing list