#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0

    Undefined Index Error


    I've been looking around the forums at old posts and cannot find the answer to my problem.

    I've got a function which will define the extension to the title of the page by checking what the page is actually on.

    PHP Code:
    <?php 

    include './include/conn/connection.php'
    include 
    './include/functions.php';

    $PageToGet $_GET['p'];

    function 
    getPage($p){
        if(
    $p == ''){
            
    $page ' - Index';
        }elseif(
    $p == 'h'){
            
    $page ' - Edit Team';
        }
        
        return 
    $page;
    }

    function 
    checkPageToGet($p){
        if(!empty(
    $p)){
            
    getPage($p);
        }else{
            
    $page ' - Index';
            return 
    $page;
        }
    }

    ?>

    <html>

        <head>
            
            <?php echo "<title>Fantasy Football".checkPageToGet($PageToGet)."</title>"?>
            
        </head>

    </html>
    But what is happening is it's coming up with this error

    Code:
    Notice: Undefined index: p in C:\xampp\htdocs\FantasyFootball\index.php on line 6
    And I don't know how to get rid of it without adding the error_reporting(E_ERROR) line, which I really want to avoid.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,799
    Rep Power
    529
    At the very beginning of your script, add:
    PHP Code:
    echo('<pre>'.print_r($_GET,1).'</pre>'); 
    What do you see? Is $_GET['p'] set? What does your error say?

    If sometimes it is set and other times it is not, try $PageToGet = isset($_GET['p'])?$_GET['p']:NULL;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    Thank you, the print came up with

    Code:
    Array
    (
    )
    But the second bit of script you put,

    PHP Code:
    $PageToGet = isset($_GET['p'])?$_GET['p']:NULL
    worked like a charm. Can you just quickly explain what it is, what it does and how it works?

    Much appreciated.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,799
    Rep Power
    529
    You received your error because you were asking for an index of an array which didn't exist.

    Try entering http://yoursite.com/index.php?p=hello&abc=123 into your browser, and see what the echo says this time. This time you see it was set.

    The two sections of code do the same thing. The first one is just shorthand. Google "php isset" to see what isset() does.
    PHP Code:
    $PageToGet = isset($_GET['p'])?$_GET['p']:NULL
    PHP Code:
    if(isset($_GET['p'])){$PageToGet =$_GET['p'];}
    else{
    $PageToGet =NULL;} 
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    Thanks so much. That's really helped me re grasp PHP just that little bit more. I was quite good at it until I stopped playing about to concentrate on Python for my A-Levels.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    Originally Posted by NotionCommotion
    You received your error because you were asking for an index of an array which didn't exist.

    Try entering http://yoursite.com/index.php?p=hello&abc=123 into your browser, and see what the echo says this time. This time you see it was set.

    The two sections of code do the same thing. The first one is just shorthand. Google "php isset" to see what isset() does.
    PHP Code:
    $PageToGet = isset($_GET['p'])?$_GET['p']:NULL
    PHP Code:
    if(isset($_GET['p'])){$PageToGet =$_GET['p'];}
    else{
    $PageToGet =NULL;} 
    actually the correct term you are looking for is a ternary operatore (the short hand form)

IMN logo majestic logo threadwatch logo seochat tools logo