[Solved]: Notice: Undefined variable in PHP
  John Mwaniki /   10 Dec 2021

[Solved]: Notice: Undefined variable in PHP

This error, as it suggests, occurs when you try to use a variable that has not been defined in PHP.

Example 1

<?php
echo $name;
?>

Output

Notice: Undefined variable: name in /path/to/file/file.php on line 2.

Example 2

<?php
$num1 = 27;
$answer = $num1 + $num2;
echo $answer;
?>

Output

Notice: Undefined variable: num2 in /path/to/file/file.php on line 3.

In our above two examples, we have used a total of 4 variables which include $name, $num1, $num2, and $answer.

But out of all, only two ($name and $num2) have resulted in the "undefined variable" error. This is because we are trying to use them before defining them (ie. assigning values to them).

In example 1, we are trying to print/display the value of the variable $name, but we had not yet assigned any value to it.

In example 2, we are trying to add the value of $num1 to the value of $num2 and assign their sum to $answer. However, we have not set any value for $num2.

To check whether a variable has been set (ie. assigned a value), we use the in-built isset() PHP function.

Syntax

isset($variable)

We pass the variable name as the only argument to the function, where it returns true if the variable has been set, or false if the variable has not been set.

Example

<?php
//Example 1
$name = "Raju Rastogi";
if(isset($name)){
  $result = "The name is $name";
}
else{
  $result = "The name has not been set";
}
echo $result;
//Output: The name is Raju Rastogi
echo "<br>"


//Example 2
if(isset($profession)){
  $result = "My profession is $profession";
}
else{
  $result = "The profession has not been set";
}
echo $result;
//Output: The profession has not been set
?>

In our first example above, we have defined a variable ($name) by creating it and assigning it a value (Raju Rastogi) and thus the isset() function returns true.

In our second example, we had not defined our variable ($profession) before passing to the isset() function and thus the response is false.

The Fix for Undefined variable error

Here are some ways in which you can get rid of this error in your PHP program.

1. Define your variables before using them

Since the error originates from using a variable that you have not defined (assigned a value to), the best solution is to assign a value to the variable before using it anywhere in your program.

For instance, in our first example, the solution is to assign a value to the variable $name before printing it.

<?php
$name = "Farhan Qureshi";
echo $name;
//Output: Farhan Qureshi
?>

The above code works without any error.

2. Validating variables with isset() function

Another way to go about this is to validate whether the variables have been set before using them.

<?php
$num1 = 27;
if(isset($num1) && isset($num2)){
$answer = $num1 + $num2;
echo $answer;
}
?>

The addition operation will not take place because one of the required variables ($num2) has not been set.

<?php
$num1 = 27;
$num2 = 8;
if(isset($num1) && isset($num2)){
$answer = $num1 + $num2;
echo $answer;
}
//Oputput: 35
?>

The addition this time will take place because the two variables required have been set.

3. Setting the undefined variable to a default value

You can also check whether the variables have been defined using the isset() function and if not, assign them a default value.

For instance, you can set a blank "" value for variables expected to hold a string value, and a 0 for those values expect to hold a numeric value.

Example

<?php
$name = "John Doe";
$name = isset($name) ? $name : '';
$age= isset($age) ? $age: 0;
echo "My name is $name and I am $age yrs old.";
//Output: My name is John Doe and I am 0 yrs old.
?>

4. Disabling Notice error reporting

This is always a good practice to hide errors from the website visitor. In case these errors are visible on the website to the web viewers, then this solution will be very useful in hiding these errors.

This option does not prevent the error from happening, it just hides it.

Open the php.ini file in a text editor, and find the line below:

error_reporting = E_ALL

Replace it with:

error_reporting = E_ALL & ~E_NOTICE

Now the ‘NOTICE’ type of errors won't be shown again. However, the other types of errors will be shown.

Another way of disabling these errors is to add the line of code below on the top of your PHP code.

error_reporting (E_ALL ^ E_NOTICE);

That's all for this article. It's my hope that it has helped you solve the error.