...

View Full Version : checking whether a variable contains value or not



mrjameer
03-12-2007, 05:54 PM
hi,


how to check whether a variable contains some value or not.if the variable contains some value then print.example
<?php

$a="abcd";

echo "the value of a is".$a;

$b;

echo "the value of b is".$b;
?>
here the $b contains no value.so i want to hide the echo statement.how i can check that the $b contains some value or not.any of your help will be appreciated

thanks
mrjameer

the-dream
03-12-2007, 05:59 PM
try something like:



<?php

$a = "abcd";

if ($a == "abcd") {
echo "The Statement Is True!";
}

else {
echo "The Statement Is False!";
}

?>

Fumigator
03-12-2007, 06:05 PM
Use isset() (http://us2.php.net/manual/en/function.isset.php).

mrjameer
03-12-2007, 07:01 PM
hi,

i have tested the isset function.working good.
if i have 100 variables how i can test it.i have to write 100 isset function..

thanks
mrjameer

Fumigator
03-12-2007, 08:00 PM
Sounds like you could benefit from the powers of the Array (and looping) in that situation.

aedrin
03-12-2007, 08:00 PM
The function empty() is more effective for this.

Fumigator
03-12-2007, 08:07 PM
empty() has the distinct disadvantage of returning TRUE if the value is 0 or "0" or FALSE, which can burn you if you use it to check to see if a variable exists (especially booleans and numeric types).

aedrin
03-12-2007, 09:42 PM
The question asked was.


how to check whether a variable contains some value or not

For which empty() is the best function to use. isset() will return true even when a variable is empty.

Fumigator
03-12-2007, 09:59 PM
I'm not sure if you're trying to "win", or help the guy out-- empty() has a major caveat that in the OP's case will possibly burn him down the road, which is worth mentioning.

I never said "don't use empty()".

(I've got 5 bucks that says aedrin is going to argue some more with me about how his post is right and everyone else is wrong :D)

Inigoesdr
03-13-2007, 05:55 AM
There isn't even an argument. Unless the "value" is a zero, then empty() is the right function to use. As aedrin said isset() will return true if the variable is set, even if the value is blank. ie. $var = "";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum