IF statement with text field ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

IF statement with text field ?

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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";
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

I want to echo the text if there's ANY text, do nothing if there's NO text...
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

The Empty Function is what your looking for.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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;
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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/..
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

I ended up using this...worked fine, does what I want it to.

Code: Select all

if ($shipaddr2)   { echo "$shipaddr2<br>";}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
}
?>
Post Reply