Forum Moderators: coopster

Message Too Old, No Replies

IF Statements and Variables

         

branmh

2:44 pm on Jan 4, 2004 (gmt 0)

10+ Year Member



How would I go about creating an IF statement that uses a variable in order to use that IF statement?

I have tryed a few but doesn't work

if (($_GET["from"])) {
PRINT 'hey it works';
}

Romeo

3:20 pm on Jan 4, 2004 (gmt 0)

10+ Year Member



you can check if a variable is empty or set at all, before using it:
if (!empty($variable)) { echo $variable; }
if (isset($variable)) ...

Regards,
R.

travelbuff

3:53 pm on Jan 4, 2004 (gmt 0)

10+ Year Member



you can also check it by seeing if the variable exists:
if (isset($var)) { or
if ($var) {

or to see if it is empty you can also use
if ($var == "" OR NULL) {

the reason that the example you posted didn't work is you had an extra set of (), this would have worked:
if ($_GET["from"]) {

ergophobe

10:28 pm on Jan 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




the reason that the example you posted didn't work is you had an extra set of (), this would have worked:
if ($_GET["from"]) {

Sorry, but that isn't true. Parens should not effect things.

if ($x==$y && $a==$b)

is the same as

if (($x==$y) && ($a==$b))

As another example,


[pre]
$var['index'] = TRUE;
if (($var['index'])) {
echo "((TRUE)) evaluates as TRUE\n";
} elseif((!$var['index'])) {
echo "((TRUE)) evaluates as FALSE\n";
} else {
echo "((TRUE)) has problems\n";
}
var_dump($var['index']);
[/pre]

Yields:
((TRUE)) evaluates as TRUE
bool(true)

Tom

travelbuff

4:37 am on Jan 5, 2004 (gmt 0)

10+ Year Member



My bad! Guess I should limit my advice to things I actually know ;-)

Then why wouldn't his original way of doing it work?

coopster

3:11 pm on Jan 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Could be a number of reasons but the first I would check would be PHP version to make sure you are checking the correct Superglobal [php.net]:

HTTP GET variables: $_GET
Note: Introduced in 4.1.0. In earlier versions, use $HTTP_GET_VARS.

...and second, use the advice to check if the variable is actually being populated with the empty [php.net] or isset [php.net] functions. For example, if you aren't doing something like this:

h**p://www.mysite.com/myphpscript.php?from=myvalue

you aren't going to have an index key of
from
in the GET superglobal.