How to fix PHP Notice: Undefined Variable

Summary:

The undefined variable error happens when you've forgotten to add a variable that hasn't been instantiated. You're most likely not including a file that you need or haven't passed it into the function. See below for details.

PHP Notice: Undefined Variable

PHP Notice: Undefined variable arises when trying to access an undeclared or uninitialized variable. Here’s one of the recommended fixes from this article.

<?php
 
$name = "Edward";
 
 
$name = isset($name) ? $name : 'N/A';
$percentage = isset($percentage) ? $percentage : 'N/A';
 
echo $name. ' has scored '.$percentage.'%';  
 
?>

Stay tuned to the article to learn more about undefined variable in PHP and how to fix it.

Introduction

A variable represents a value in memory. Before using a variable in PHP, it is recommended to declare the variable and initialize it to some value. However, PHP doesn’t absolutely require doing so. PHP adds an undeclared variable to the scope and assigns it a default value based on the context.

<?php
 
$name = 'Edward';
 
echo $name. ' has scored '.$percentage.'%';   //$percentage is coerced to empty string
 
//OUTPUT
//Edward has scored %
?>

The example demonstrates what has been stated above. The $percentage is not declared, but PHP infers its type and value, assigning it an empty string. It does throw a low-level warning of undefined variable in PHP.

PHP Notice: Undefined Variable

As seen already, PHP issues a warning when accessing an undeclared or uninitialized variable. The following snippets result in the warning.

<?php
 
$name = 'Edward';
 
echo $name. ' has scored '.$percentage.'%';   //$percentage is not declared
 
?>
<?php
 
$name = 'Edward';
$percentage; //declared but uninitialized
 
echo $name. ' has scored '.$percentage.'%';  
 
?>
PHP Warning:  Undefined variable $percentage …

It is more commonplace in scenarios where a variable tries to fetch data from a source, for instance, an array.

Fix#1 – PHP Notice: Undefined Variable using isset() [Recommended]

PHP isset function determines if a variable is declared and not set to null. It is a recommended way of tackling the undefined variable notice. This function call also helps avoid any unintended bugs resulting from a variable mishap. Here’s how it works.

<?php
 
$name = "Edward";
 
if(!isset($name))
{
    $name = "N/A";
}
 
if(!isset($percentage))
{
    $percentage = "N/A";
}
 
echo $name. ' has scored '.$percentage.'%';  
 
?>

The code checks if a variable is not set. If true, it assigns it ‘N/A’. This way the variable undeclared issue doesn’t occur at all. The same code with the ternary operator is shorter and cleaner, here’s how.

<?php
 
$name = "Edward";
 
 
$name = isset($name) ? $name : 'N/A';
$percentage = isset($percentage) ? $percentage : 'N/A';
 
echo $name. ' has scored '.$percentage.'%';  
 
?>

So, the ternary operator checks if a variable is set. If false, the variable gets the value ‘N/A’.

Fix#2 – PHP Notice: Undefined Variable using null coalesce operator

PHP 7.0 features a null coalesce operator which simplifies things further. This operator takes two operands and returns the one that is set.

<?php
 
$name = "Edward";
 
 
$name = $name ?? 'N/A';
$percentage = $percentage ?? 'N/A';
 
echo $name. ' has scored '.$percentage.'%';  
 
?>

Voila! It is more compact than the ternary operator, but it won’t work with PHP version < 7.0.

Fix#3 – PHP Notice: Undefined Variable by disabling E_Notice

Disabling error reporting is not a recommended practice. It does suppress the error messages but could fire back in the long run. So, stick to Fix#1 as it is the best way out. So to disable E_Notice, add this line of code to the top of your PHP file.

error_reporting( error_reporting() & ~E_NOTICE );

That’s it for this article. Now is the time for the wrap up.

Wrap Up

PHP undefined variable is a notice/warning that PHP issues when trying to access a variable that is either undeclared or uninitialized. The article suggests ways to fix this issue and recommends Fix#1.

Hopefully, you’ve enjoyed the article. Stay tuned for more articles like these at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


Article Categories

Article Tags

© 2022 Confident.Systems