Page 1 of 1

IF statement with text field ?

Posted: Tue Dec 20, 2005 2:37 pm
by michlcamp
When it's a numeric field ($qty, for example), the IF statement works great like this...

Code: Select all

if ($qty > 0)   
{ 
echo "$qty";
}
and if $qty is greater than zero, it echo's $qty.

How would you use the IF statement to do the same thing if the field is a text field?
($address2, for example)



thanks in advance.
(you guys are great!)
mc

Posted: Tue Dec 20, 2005 2:44 pm
by Burrito
well obviously you can't use the ">" operator as there's not a quantity against which to compare.

what do you need to know in the "if" condition to make it pass?

Code: Select all

if($somefield == "some text")
   echo "it passed";

Posted: Tue Dec 20, 2005 4:22 pm
by michlcamp
I want to echo the text if there's ANY text, do nothing if there's NO text...

Posted: Tue Dec 20, 2005 4:27 pm
by hawleyjr
The Empty Function is what your looking for.

Posted: Tue Dec 20, 2005 4:28 pm
by Burrito
you could do this:

Code: Select all

if($string != "")
    echo $string;
or this:

Code: Select all

if(strlen($string) > 0)
    echo $string;
or you could just echo the string and if there's nothing there it won't show anything :wink:

Code: Select all

echo $string;

Posted: Tue Dec 20, 2005 4:29 pm
by josh
I see this is redundancy, if there is no text contained in the variable, echo'ing it will do nothing essentially.. for your purposes at least. That is unless you are going to have more then

Code: Select all

echo $var;
in your if block/..

Posted: Tue Dec 20, 2005 5:15 pm
by michlcamp
I ended up using this...worked fine, does what I want it to.

Code: Select all

if ($shipaddr2)   { echo "$shipaddr2<br>";}

Posted: Tue Dec 20, 2005 7:08 pm
by josh
true but that doesn't cover everything

Code: Select all

<?
$var = '0';
if ($var) {
	// will not output it
}
if ($var!=NULL) {
	//will output it
}
?>