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

后端 未结 28 917
盖世英雄少女心
盖世英雄少女心 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: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";
    }
    

提交回复
热议问题