Jump to content

'Proper' Coding


completeamateur

Recommended Posts

I just turned on all error reporting in php and its spuing out loads of errors all over my local site.  The most common being trying to GET or POST a non-existant variable.  What should I do? Ignore it or add something like this:

 

if(isset($_POST["username"])) { $username = $_POST["username"]); }

 

Regards.

Link to comment
https://forums.phpfreaks.com/topic/111153-proper-coding/
Share on other sites

Thats because it reports warnings when you use fields that you havent declared, it works fine but it gives warnings

 

You can remove the warnings using:

 

error_reporting(E_ERROR | E_WARNING | E_PARSE);

 

or by editing the ini file again.

 

Or you could put the isset checks in, thats the proper way that php should be coded

Link to comment
https://forums.phpfreaks.com/topic/111153-proper-coding/#findComment-570458
Share on other sites

Write a wrapper function and use the wrapper, something like:

<?php

function GetVariable( $name )
{

    if ( isset( $_GET[ $name ] ) )
   {

          return $_GET[ $name ];

   }
   else
   {
           // whatever to do if it doesn't exist
   }

}

$name = GetVariable("username");

?>

 

Obviously code in your $_POST/$_SERVER stuff etc. where required.

Link to comment
https://forums.phpfreaks.com/topic/111153-proper-coding/#findComment-570462
Share on other sites

completeamateur,

 

While developing, it is good to see those messages...

 

suppose you were expecting to see $_GET[school_name]; back from a form,

 

but you get:

 

Informational message: '$_GET[skool_name] is uninitialized ' ... Pretty good clue that you have something misspelled somewhere...

 

Scot Diddle, Richmond VA

Link to comment
https://forums.phpfreaks.com/topic/111153-proper-coding/#findComment-570472
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.