Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Get rid of notices

    Hi,

    I guess my php.ini has error reporting to all. Therefore I get many notices such as Notice: undefined variable or Notice: undefined index etc. I know I could use the ini_set command and not get these errors or I could add

    PHP Code:
    if(isset($variable)) {
        if(
    $variable != "") { echo "$variable"; } ?>

    to each $variable related line but I don't know if this is necessary. My question: Why do I get such notices? Second, if I use ini_set, are the notices generated anyway but not printed on screen? Does that bring performance loss? Should I stop notices in my code?

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Using error reporting (http://us2.php.net/error_reporting) or ini_set at the top of your script file will nix the notices.

    Do you have access to the php.ini....I would try to change the setting in there, so you don;t have to do the above for all of your files (http://us2.php.net/manual/en/ref.err...rror-reporting)...

    you can also do a php_info() and see your server configuration....you can see what error_reporting is set to...

    If you're on a shared server, ask your hosting provider...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks for the answer. My error reporting is set to 2039 (says phpinfo()). I know I can easily turn off error reporting whether with PHP or with .htaccess (php_flag display_errors off). I wonder if there is a way to stop notices IN the code. For example:

    $my_id = $_SESSION['id'];

    generates a:

    Notice: Undefined index id

    notice. I can't understand that. It is obvious that it is undefined. How to define id?

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Even if error reporting is set so that the notices and warnings are not output to the browser, they are still logged. This will cause a minor speed penalty when the page is parsed and severed.

    Proper programming and testing will eliminate the notices and warnings. For your example -
    PHP Code:
    session_start();
    $my_id $_SESSION['id'];
    // if the session variable does not exist, $my_id will be null at this point
    // and any following code that uses it will fail, such as a mysql query... 
    If this session variable does not exist (but your site does make use of this elsewhere) that would indicate that someone reached the page that has this code without properly visiting the page that creates this session variable. Doing something like the following would be appropriate -
    PHP Code:
    session_start();
    if(!isset(
    $_SESSION['id'])) { 
        
    // note the ! (not) in the above line
        
    die('You are not permitted to access this page directly');

    // normal page code here...
    $my_id $_SESSION['id'];
    // there will be a value in the variable at this point and code that depends
    // on it being set from a previous page will not fail due to it being a null value 
    If you have one page that both sets a variable if it does not exist and then tests it, the same concept will work -
    PHP Code:
    session_start();
    if(!isset(
    $_SESSION['id'])) { 
        
    // note the ! (not) in the above line
        
    $_SESSION['id'] = "your id value";

    // normal page code here...
    $my_id $_SESSION['id'];
    // there will be a value in the variable at this point and code that depends
    // on it being set will not fail due to it being a null value 
    Last edited by CFMaBiSmAd; 11-25-2006 at 09:24 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •