“Notice: Undefined variable”, “Notice: Undefined i

2020-04-30 02:49发布

I'm running a PHP script and continue to receive errors like:

Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10

Notice: Undefined index: my_index C:\wamp\www\mypath\index.php on line 11

Line 10 and 11 looks like this:

echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];

What is the meaning of these error messages?

Why do they appear all of a sudden? I used to use this script for years and I've never had any problem.

How do I fix them?


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.

Related Meta discussion:

28条回答
老娘就宠你
2楼-- · 2020-04-30 03:02

Try these

Q1: this notice means $varname is not defined at current scope of the script.

Q2: Use of isset(), empty() conditions before using any suspicious variable works well.

// recommended solution for recent PHP versions
$user_name = $_SESSION['user_name'] ?? '';

// pre-7 PHP versions
$user_name = '';
if (!empty($_SESSION['user_name'])) {
     $user_name = $_SESSION['user_name'];
}

Or, as a quick and dirty solution:

// not the best solution, but works
// in your php setting use, it helps hiding site wide notices
error_reporting(E_ALL ^ E_NOTICE);

Note about sessions:

查看更多
成全新的幸福
3楼-- · 2020-04-30 03:04

Those notices are because you don't have the used variable defined and my_index key was not present into $my_array variable.

Those notices were triggered every time, because your code is not correct, but probably you didn't have the reporting of notices on.

Solve the bugs:

$my_variable_name = "Variable name"; // defining variable
echo "My variable value is: " . $my_variable_name;

if(isset($my_array["my_index"])){
    echo "My index value is: " . $my_array["my_index"]; // check if my_index is set 
}

Another way to get this out:

ini_set("error_reporting", false)
查看更多
贪生不怕死
4楼-- · 2020-04-30 03:05

In PHP you need fist to define the variable after that you can use it.
We can check variable is defined or not in very efficient way!.

//If you only want to check variable has value and value has true and false value.
//But variable must be defined first.

if($my_variable_name){

}

//If you want to check variable is define or undefine
//Isset() does not check that variable has true or false value
//But it check null value of variable
if(isset($my_variable_name)){

}

Simple Explanation

//It will work with :- true,false,NULL
$defineVarialbe = false;
if($defineVarialbe){
    echo "true";
}else{
    echo "false";
}

//It will check variable is define or not and variable has null value.
if(isset($unDefineVarialbe)){
    echo "true";
}else{
    echo "false";
}
查看更多
再贱就再见
5楼-- · 2020-04-30 03:06

Generally because of "bad programming", and a possibility for mistakes now or later.

  1. If it's a mistake, make a proper assignment to the variable first: $varname=0;
  2. If it really is only defined sometimes, test for it: if (isset($varname)), before using it
  3. If it's because you spelled it wrong, just correct that
  4. Maybe even turn of the warnings in you PHP-settings
查看更多
我只想做你的唯一
6楼-- · 2020-04-30 03:06

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.

查看更多
爷、活的狠高调
7楼-- · 2020-04-30 03:06

I used to curse this error, but it can be helpful to remind you to escape user input.

For instance, if you thought this was clever, shorthand code:

// Echo whatever the hell this is
<?=$_POST['something']?>

...Think again! A better solution is:

// If this is set, echo a filtered version
<?=isset($_POST['something']) ? html($_POST['something']) : ''?>

(I use a custom html() function to escape characters, your mileage may vary)

查看更多
登录 后发表回答