115 Notice Undefined variable my_variable_name in php - wikitechy.com

Notice Undefined variable my_variable_name in php

Wikitechy | 4510 Views | php | 13 Jun 2016



When I try to execute the PHP script the following error will occur “C:\wamp\www\mypath\index.php on line 10”

echo "Wikitechy variable value is: " . $my_variable_name;


undefined variable will be assigned for the variable name my_variable_name.

Fix 1:

  • Here we Declare variables, for example append a string to an undefined variable. 
  • Are else use isset () / !empty() function to check whether they are declared before referencing them.

Observe the following PHP script:

//Initializing variable
$Wikitechy_value = ""; //Initialization value; Examples
//"" When you want to append stuff later
//0  When you want to add numbers later

isset — Determine if a variable is set and is not NULL
$value = isset($_POST['Wikitechy_value']) ? $_POST['Wikitechy_value'] : '';
Or you can isempty method to validate the variables.
$value = !empty($_POST['Wikitechy_value']) ? $_POST['Wikitechy_value'] : '';

For Example:

    Here we define the variable “var” as empty.

    Here we set the variable “var” value.

    Apart from that we set the variable “a” value as “true” after that unset the variable again , so now the value will be “false”.

    Here we define the value “foo” as “null” and set the variable as “false”.

Sample Output:

What is nl2br in php?

The nl2br is used for Inserting line breaks where newlines (\n) occur in the string

    echo nl2br("One line.\nAnother line.");

The browser output of the code above will be:

One line.

Another line.

What is var_dump in php?

  • The var_dump function is used to display the structured information about variables/expressions including its type and value.
  • Arrays are explored recursively with values indented to show structure.

What is echo statement in php?

PHP echo and print Statements are more or less the same. They both are used to output the data to the screen. 

What is unset statement in php

If a variable is already unset with unset () function, it will no longer be set. 

What is isset in php?

  • The isset () function is used to check whether a variable is set or not. 
  • The isset () function return false if testing variable contains a NULL value.

Fix 2:

Here is the simple way to resolve this issue by Disable E_NOTICE from reporting in the PHP Script. 

error_reporting( error_reporting() & ~E_NOTICE )

Fix 3:  

Here is the another fix to resolve this issue by Checking whether the values of the variable declared (or) not before usage.

For Example:

foreach ($items as $item) {
    // do something with item
    $counter++;    //the variable not defined initially
  • In this above example code, we are not defining the variable “$counter” so it triggers an error notice.
  • This issue can be resolved by setting  the variable before using it, even if it's just an empty string. 
$counter = 0;
foreach ($items as $item) {
    // do something with item

Applies to:

  • PHP 3 and 4
  • PHP 4.3
  • PHP 5
  • PHP 6 and Unicode
  • PHP 7

Related Tags:

  • Undefined PHP 4.3 Variables
  • "Notice: Undefined Variable:" 
  • PHP: "Notice: Undefined variable"
  • Why do I get "Notice: Undefined variable: location”?


Bug Bounty

Join our Community