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

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
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

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

Post 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
Last edited by hairyjim on Fri Dec 17, 2004 9:15 am, edited 1 time in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

I tend to like this method:

Code: Select all

<?php
function isEmpty($var)
{
    return !isset($var) || strlen(trim($var)) == 0;
}
?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

the difference between [php_man]empty[/php_man] and [php_man]isset[/php_man] is small, but important.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Ahhhh! Cheers PatriK!

Indeed it is a most important piece of info you have just armed me with!
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

:)
Post Reply