Jump to content

[SOLVED] Undefined Index


supanoob

Recommended Posts

When i use $_GET['step'] to find out what the user is doing and include the page required (using the folowing code) i get errors even though the $_GET['step'] is defined. Anyway to stop this happening?

 

PHP Code:

<?php
if (!$_GET['step'])
{
include_once('homepage.php');
}

if ($_GET['step'] == 'register')
{
include_once('register.php');
}
?>

 

Errors:

Notice: Undefined index: step in C:\wamp\www\index.php on line 75

Hello World!

Notice: Undefined index: step in C:\wamp\www\index.php on line 80

 

The hello world is what i want echoing when on the hompage (as a test obv) the errors disappear when i click register and my resgistration page shows up.

 

 

Link to comment
Share on other sites

Notices aren't important are they? You could just display errors...

 

<?php
error_reporting(E_ALL ^ E_NOTICE); //error reporting is everything but notices.
?>

 

ive tried that and it doesnt change anything, just been reading up on it and apparently i cant use global variables in the php version im using? 5.2.9?

 

however when when put $step = $_GET['step']; in the top of my code it throws out the same error, and i was mistaken my register doesn't show up at all.

Link to comment
Share on other sites

Notices aren't important are they? You could just display errors...

 

Err. Yes. They're pretty important. They're generally the single most helpful thing in finding out why a script's not running the way you expected it to. It's damn difficult finding a typo without them.

 

With regard to the error, you should check to make sure that $_GET['step'] is set before trying to use it:

 

if(isset($_GET['step']) && $_GET['step'] == 'register'){
    include_once('register.php');
}else{
    include_once('homepage.php');
}

 

You might wonder why you don't still have problems with this if statement. After all, you're still making a test against $_GET['step'] so if it doesn't exist you might think you'd still get a warning. But no. PHP (like most languages) uses short-cut evaluation of logical expressions. That is, whilst evaluating the expression, if it has already worked out the result, it won't check the rest of the comparisons. So, in this case, the isset() test will be evaluated first. If it is false, then the second part of the statement wont be tested since false & expression is false regardless of the result of expression.

Link to comment
Share on other sites

Notices aren't important are they? You could just display errors...

 

Err. Yes. They're pretty important. They're generally the single most helpful thing in finding out why a script's not running the way you expected it to. It's damn difficult finding a typo without them.

 

With regard to the error, you should check to make sure that $_GET['step'] is set before trying to use it:

 

if(isset($_GET['step']) && $_GET['step'] == 'register'){
    include_once('register.php');
}else{
    include_once('homepage.php');
}

 

You might wonder why you don't still have problems with this if statement. After all, you're still making a test against $_GET['step'] so if it doesn't exist you might think you'd still get a warning. But no. PHP (like most languages) uses short-cut evaluation of logical expressions. That is, whilst evaluating the expression, if it has already worked out the result, it won't check the rest of the comparisons. So, in this case, the isset() test will be evaluated first. If it is false, then the second part of the statement wont be tested since false & expression is false regardless of the result of expression.

 

so using that method (which fixed it, thanks alot) how would i go about adding more to that list? would something like:

 

if(isset($_GET['step']) && $_GET['step'] == 'register'){
    include_once('register.php');
}else
if(isset($_GET['step']) && $_GET['step'] == 'register_done'){
    include_once('register_done.php');
}
else
if(isset($_GET['step']) && $_GET['step'] == 'newsr'){
    include_once('news.php');
}
else
{
    include_once('homepage.php');
}

 

do the trick?

Link to comment
Share on other sites

I think i'd build an array of pages which you might want to include. Something like this:

 

$pages = array('register','register_done','news');
if(isset($_GET['step']) && in_array($_GET['step'],$pages)){
   include($_GET['step'].'.php');
}else{
   include('homepage.php');
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.