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

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

    
    

    For array elements:

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

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

    
    

提交回复
热议问题