PHP - Fatal error: Unsupported operand types

前端 未结 5 1436
别跟我提以往
别跟我提以往 2020-12-19 07:59

I keep getting the following error and I was wondering on how to fix?

Fatal error: Unsupported operand types in C:\\wamp\\www\\tuto\\core\\Controller.php on          


        
5条回答
  •  情深已故
    2020-12-19 08:39

    + can be used in different ways but to avoid complications, only use it on numeric values.

    When you didnt initially set $this->vars to be an array, it won't work (thx to deceze); see http://codepad.viper-7.com/A24zds

    Instead try init the array and use array_merge:

    public function set($key,$value=null){
        if (!is_array($this->vars)) {
            $this->vars = array();
        }
    
        if(is_array($key)){
            $this->vars = array_merge($this->vars, $key);
        }else{
            $this->vars[$key] = $value;
        }
    }
    

    Examples:

     one'
    print_r($test);
    
    
    $test = array_merge($test, $t);
    
    // will print both elements
    print_r($test);
    

提交回复
热议问题