Page 1 of 1

[SOLVED] !isset is not working....!?

Posted: Fri Dec 17, 2004 3:44 am
by hairyjim
Hi all,

For some reason this piece of code does not execute.

Code: Select all

<?php

if (!isset($_SERVER['REQUEST_URI'])) {
		$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].'?'.(isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '');
	}


?>
I placed an echo inside the statement test if the statement was run, and it didn't. So I placed an echo just after the statement and echoed out the $_server['Request_URI'], nothing was output. Which I found odd because then my aboive statement should have run.

Anyway, I changed the statement above to:

Code: Select all

<?php
if ($_SERVER['REQUEST_URI'] == '') {
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].'?'.(isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '');
	}
?>
This then ran! I have no idea why this run and the original did not!?

Could someone enlighten me.

I run IIS on a win 2003 server.

Jim

Posted: Fri Dec 17, 2004 3:45 am
by patrikG
$_SERVER is a superglobal and either empty or not. Isset only checks whether a variable has been initialised. Use !empty instead of !isset in your code and it should work.

Posted: Fri Dec 17, 2004 7:41 am
by protokol
I tend to like this method:

Code: Select all

<?php
function isEmpty($var)
{
    return !isset($var) || strlen(trim($var)) == 0;
}
?>

Posted: Fri Dec 17, 2004 7:46 am
by patrikG
the difference between [php_man]empty[/php_man] and [php_man]isset[/php_man] is small, but important.

Posted: Fri Dec 17, 2004 9:14 am
by hairyjim
Ahhhh! Cheers PatriK!

Indeed it is a most important piece of info you have just armed me with!

Posted: Fri Dec 17, 2004 9:24 am
by patrikG
:)