I am running a PHP script, and keep getting errors like:
Undefined variable: user_location in C:wampwwwmypathindex.php on line 12
Line 12 looks like this:
$greeting = "Hello, ".$user_name." from ".$user_location;
What do these errors mean?
Why do they appear all of a sudden? I used to use this script for years and I've never had any problem.
What do I need to do to fix them?
Is there a quick fix?
This is a General Reference question for people to link to as duplicate, instead of having to explain the issue over and over again. I feel this is necessary because most real-world answers on this issue are very specific.
Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals tued on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.
Some explanations:
Although PHP does not require variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that he will use later in the script. What PHP does in the case of undeclared variables is issue a very low level error, E_NOTICE, one that is not even reported by default, but the Manual advises to allow during development.
Ways to deal with the issue:
Recommended: Declare your variables. Or use isset() to check if they are declared before referencing them, as in: $value = isset($_POST['value']) ? $_POST['value'] : '';.
Use empty(), so no waing is generated if the variable does not exist. It's equivalent to !isset($var) || $var == false. E.g.
echo "Hello " . (!empty($user) ? $user : "");
Set a custom error handler for E_NOTICE and redirect the messages away from the standard output (maybe to a log file). set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT).
Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is error_reporting( error_reporting() & ~E_NOTICE ).
Q1: this notice means $vaame is not defined at current scope of the script.
Q2: Use of isset(), empty() conditions before using any suspicious variable works well.
// recommended solution
$user_name = $_SESSION['user_name'];
if (empty($user_name)) $user_name = '';
OR
// just define at the top of the script index.php
$user_name = '';
$user_name = $_SESSION['user_name'];
OR
$user_name = $_SESSION['user_name'];
if (!isset($user_name)) $user_name = '';
QUICK Solution:
// not the best solution, but works
// in your php setting use, it helps hiding site wide notices
error_reporting(E_ALL ^ E_NOTICE);
A (often discouraged) alteative is the error suppression operator@. It is a specific language construct to shut down undesired notices and waings, but should be used with care.
First, it incurs a microperformance penalty over using isset. That's not measurable in real world applications, but should be considered in data heavy iterations. Secondly it might obstruct debugging, but at the same time suppressed errors are in fact passed on to custom error handlers (unlike isset decorated expressions).
It means you are testing, evaluating, or printing a variable that you have not yet assigned anything to. It means you either have a typo, or you need to check that the variable was initialized to something first. Check your logic paths, it may be set in one path but not in another.
Its because the variable '$user_location' is not getting defined. If you are using any if loop inside which you are declaring the '$user_location' variable then you must also have an else loop and define the same. For example:
The above code will create error as The if loop is not satisfied and in the else loop '$user_location' was not defined. Still PHP was asked to echo out the variable. So to modify the code you must do the following:
I use all time own useful function exst() which automatically declare variables.
Your code will be -
$greeting = "Hello, ".exst($user_name, 'Visitor')." from ".exst($user_location);
/** * Function exst() - Checks if the variable has been set * (copy/paste it in any place of your code) * * If the variable is set and not empty retus the variable (no transformation) * If the variable is not set or empty, retus the $default value * * @param mixed $var * @param mixed $default * * @retu mixed */
function exst( & $var, $default = "")
{ $t = ""; if ( !isset($var) || !$var ) { if (isset($default) && $default != "") $t = $default; } else { $t = $var; } if (is_string($t)) $t = trim($t); retu $t;
}
In a very Simple Language. The mistake is you are using a variable $user_location which is not defined by you earlier and it doesn't have any value So I recommend you to please declare this variable beforeusing it, For Example:
$user_location = ''; Or $user_location = 'Los Angles';This is a very common error you can face.So don't worry just declare the variable and Enjoy Coding.
<?php
error_reporting(E_ALL); // making sure all notices are on
function idxVal(&$var, $default = null) { retu empty($var) ? $var = $default : $var; }
echo idxVal($arr['test']); // retus null without any notice
echo idxVal($arr['hey ho'], 'yo'); // retus yo and assigns it to array index, nice
?>