1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Undefined variable?

Discussion in 'PHP' started by baris22, Aug 8, 2010.

  1. #1
    hello,

    I get this error:

    PHP Notice: Undefined variable: url in

    
    
      $server_url = $_SERVER['DOCUMENT_ROOT'];
      $url = $server_url.'/content/';
    
    
    PHP:
    what is the reason of this error?

    regards
     
    baris22, Aug 8, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Basically it is saying that the variable $url was note declared before using.

    This is not much of a problem, you can either declare and set variables before using or turn notices off in your php.ini (error reporting settings).
     
    ThePHPMaster, Aug 8, 2010 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    how can i declare before using it? i get the same error if i do

    $url = "content";


    regards
     
    baris22, Aug 8, 2010 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    To declare strings in PHP, you use the single quote or double quotes. For example:

    $url = '';
    $url = "";
     
    ThePHPMaster, Aug 8, 2010 IP
  5. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #5
    There is nothing wrong with that, I'm not getting any errors. Try the following:
    
    <?php
    echo $_SERVER['DOCUMENT_ROOT'] . "<br />";
    $server_url = $_SERVER['DOCUMENT_ROOT'];
    $url = $server_url.'/content/';
    echo $url;
    ?>
    
    PHP:
    If you still get errors, then there's probably something wrong with your PHP. =/


    There is a big difference between single and double quotes. Single quote doesn't interpret escape sequences, but double quote do. For example:
    
    <?php
    $lol = "what";
    
    echo '$lol'; // this will output "$lol"
    echo "$lol"; // but this will interpret the variable, thus it will output "what"
    ?>
    
    PHP:
    See this for more info. :)
     
    Last edited: Aug 8, 2010
    Rainulf, Aug 8, 2010 IP
    baris22 likes this.
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    i had them in config.php and i was including config.php in index.php

    i just tried using by itself and i didn`t get any error. I do not know why i am getting this error when i include.
     
    baris22, Aug 8, 2010 IP
  7. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #7
    Aha~ Now we're talking.. It would help if you provide more details (source of both config.php and index.php maybe), there could be a number of reasons. At first, I thought it's because you're including it wrong, but the error should be different so you must be including it correctly. Still, hard to tell with very little details.

    But I'm betting that you're trying to call a variable that doesn't exist from the other file. (check your typos). The following will generate similar error:
    
    <?php
    error_reporting(E_ALL);
    $server_urls = $_SERVER['DOCUMENT_ROOT'];
    $url = $server_url.'/content/';   // typo
    
    echo $url;
    
    ?>
    
    PHP:
     
    Rainulf, Aug 8, 2010 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    i am so stupid. ofcourse i will get the error. i am trying to use a variable while including. Surprisingly it was still working, i was only getting notice.

    
    include ($url."inc/config.php");	// $url variable is in config file.
    
    PHP:
     
    baris22, Aug 10, 2010 IP
  9. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #9
    $url in the include is completely unnecessary because it doesn't contain anything nor does it even exist (that's why you're getting a notice), take it off:
    include ("inc/config.php");
    PHP:
    Only use relative path whenever you include a file, absolute path gives you headaches.
     
    Rainulf, Aug 10, 2010 IP
  10. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #10
    I would turn notices off in your php.ini file or just set this code in the top of your script,

    
    error_reporting(E_ALL ^ E_NOTICE);
    
    PHP:
     
    HuggyEssex, Aug 11, 2010 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Just because you're not seeing the errors, it doesn't mean you're doing it right.
     
    nico_swd, Aug 11, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    Its best practice to not have these errors then to conceal them.

    Use isset() before using user submitted data (ie. via $_POST, $_GET, $_REQUEST etc.)
     
    danx10, Aug 11, 2010 IP
  13. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #13
    Only while your developing, a notice isn't a error that even matters.
     
    HuggyEssex, Aug 11, 2010 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    It does matter.

    Every time a notice, a warning, or an error is triggered by PHP, it opens a log file, writes to it, and closes it. It slows down performance, and really is just... unprofessional. All errors should be reported during development, and should be taken care of before going live with the site.
     
    nico_swd, Aug 11, 2010 IP