“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

后端 未结 28 912
盖世英雄少女心
盖世英雄少女心 2021-01-24 14:18

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

相关标签:
28条回答
  • 2021-01-24 14:43

    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:

    $a=10;
    if($a==5) { $user_location='Paris';} else { }
    echo $user_location;
    

    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:

    $a=10;
    if($a==5) { $user_location='Paris';} else { $user_location='SOMETHING OR BLANK'; }
    echo $user_location;
    
    0 讨论(0)
  • 2021-01-24 14:46

    the quick fix is to assign your variable to null at the top of your code

    $user_location = null;
    
    0 讨论(0)
  • 2021-01-24 14:47

    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";
    }
    
    0 讨论(0)
  • 2021-01-24 14:48

    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
    0 讨论(0)
  • 2021-01-24 14:49

    I asked a question about this and I was referred to this post with the message:

    This question already has an answer here:

    “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

    I am sharing my question and solution here:

    This is the error:

    Line 154 is the problem. This is what I have in line 154:

    153    foreach($cities as $key => $city){
    154        if(($city != 'London') && ($city != 'Madrid') && ($citiesCounterArray[$key] >= 1)){
    

    I think the problem is that I am writing if conditions for the variable $city, which is not the key but the value in $key => $city. First, could you confirm if that is the cause of the warning? Second, if that is the problem, why is it that I cannot write a condition based on the value? Does it have to be with the key that I need to write the condition?

    UPDATE 1: The problem is that when executing $citiesCounterArray[$key], sometimes the $key corresponds to a key that does not exist in the $citiesCounterArray array, but that is not always the case based on the data of my loop. What I need is to set a condition so that if $key exists in the array, then run the code, otherwise, skip it.

    UPDATE 2: This is how I fixed it by using array_key_exists():

    foreach($cities as $key => $city){
        if(array_key_exists($key, $citiesCounterArray)){
            if(($city != 'London') && ($city != 'Madrid') && ($citiesCounterArray[$key] >= 1)){
    
    0 讨论(0)
  • 2021-01-24 14:51

    I didn't want to disable notice because it's helpful, but wanted to avoid too much typing.

    My solution was this function:

    function ifexists($varname)
    {
      return(isset($$varname)?$varname:null);
    }
    

    So if I want to reference to $name and echo if exists, I simply write:

    <?=ifexists('name')?>
    

    For array elements:

    function ifexistsidx($var,$index)
    {
      return(isset($var[$index])?$var[$index]:null);
    }
    

    In page if I want to refer to $_REQUEST['name']:

    <?=ifexistsidx($_REQUEST,'name')?>
    
    0 讨论(0)
提交回复
热议问题