Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP - error reporting level

  • 27-12-2007 10:47pm
    #1
    Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭


    What level of error reporting do people use in their PHP sites?

    I only realised this existed when I installed easyphp locally and started getting "undefined variable" error.

    My server level is at 2039

    My local server is 2047.

    ****************
    echo $i; // incorrect
    ****************
    $i = 10;
    echo $i; // correct
    ****************

    Why would you set your code so you have to define variables beforehand. The reason I use PHP is because its not so fussy about this sort of thing.

    Is it just other unnecessary lines of code?


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    eh? - what is the point echoing $i when i doesn't exist :confused:

    The correct way to do it might be:

    [php]
    if (isset($i))
    echo $i;
    [/php]


  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    That is just an example.

    Another probably better example is checking to see if values have been submitted via a form.

    If you have a php file which submits a form to itself, everytime the file is loaded you have to check if anything was submitted
    if ($_POST['Submit'] == "Submit")
    {
              // Do whatever is needed when information is submitted
    } else {
              /*
     Do whatever is needed when nothing is submitted or incorrect information is submitted
              */
    }
    

    With the error reporting level set to the same as my local server I have to write the above code in the following way
    if (isset($_POST['Submit']))
    {
              if ($_POST['Submit'] == "Submit")
              {
                        // Do whatever is needed when information is submitted
              } else {
                        /*
               Do whatever is needed when nothing is submitted or incorrect information is submitted
                        */
              }
    } else {
              /*
               Do whatever is needed when nothing is submitted or incorrect information is submitted
              */
    }
    
    
    

    Hope this is clear.

    Thanks:)


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Surely all you need is:

    [php]
    if isset($_POST))
    {
    //Do stuff when submitted
    }
    else
    {
    //What to do with no submit
    }
    [/php]


  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    Thanks Webmonkey.

    I realise that all I have to do is check does each variable exist before using them.

    If I have a form where I have lots of values get submitted it is quite annoying having to check does each variable actually exist before verifying the variable.

    What I'm really trying to ask is why people use this extra level of error reporting and what is the advantage in it? Is it better memory usage? Is it more object orientated like?


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Its good practice and your program's behaviour is more predictible.
    Object Oriented stuff is a whole different area and isn't really related to this issue.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    Thanks,

    Just haven't implemented it on any of my code to date and now I think I'm just gonna change my error reporting level ahead of changing all of my code.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Might be easier option alright for the older scripts but do start making it a habit for future scripts. These things should be done. Note that in other languages you wouldn't get away with such thing so if you want to move from PHP it's it a good idea to not assume a variable exists before using it. :)


Advertisement