PHP Notice: Undefined variable fix

Your ads will be inserted here by

Google Adsense.

Please go to the plugin admin page to set up your ad code.

This warning is generated when a variable is not defined in php.
Using the following example i`l generate this notice and i will explain how to avoid this.
Assuming this to be a file named : testfile.php

<?php
$variable1 =$_GET['variable'];
echo $variable1;
?>

Accessing testfile.php from browser will give the following notice :

PHP Notice:  Undefined index: variable in testfile.php on line 2

Your ads will be inserted here by

Google Adsense.

Please go to the plugin admin page to set up your ad code.

Now let’s fix it :

<?php
if (isset($_GET['variable'])){
   $variable1=$_GET['variable'];
   echo $variable1;
}
?>

Isset checks if a variable is defined or not. If is defined will execute it , if not will ignore it but will not generate any notice.

Your ads will be inserted here by

Google Adsense.

Please go to the plugin admin page to set up your ad code.

avatar

Alex Gabriel

Weird php programer , bloger , computer forensics addict . In my free time (if any) i lose few hours google'ing for interesting stuff .

You may also like...

1 Response

  1. avatar RYan says:

    Simple answer thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>