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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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