PHP - 致命错误:不支持的操作数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23154508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Fatal error: Unsupported operand types
提问by user3548898
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 line 23
Here is line 23.
这是第 23 行。
$this->vars += $key;
Here is the full code below.
这是下面的完整代码。
public function set($key,$value=null){
if(is_array($key)){
$this->vars += $key;
}else{
$this->vars[$key]= $value;
}
}
回答by Daniel W.
+
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
当您最初没有设置$this->vars
为数组时,它将无法工作(感谢 deceze);见http://codepad.viper-7.com/A24zds
Instead try init the array and use array_merge
:
而是尝试初始化数组并使用 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:
例子:
<?php
$test = null;
$t = array('test');
//$test += $t prints the fatal here
$test = array('one');
$test += $t;
// will only print '0 => one'
print_r($test);
$test = array_merge($test, $t);
// will print both elements
print_r($test);
回答by Hüseyin BABAL
The solution is in the error. You are trying to sum two value that has different types. You are summing array with normal value;
解决方案是在错误中。您正在尝试对具有不同类型的两个值求和。您正在对具有正常值的数组求和;
$this->vars += $key;
$key
shouldnt be an array
$key
不应该是一个数组
Or second option;
或第二种选择;
$this->vars
should be an array
$this->vars
应该是一个数组
回答by Nimes
**PROBLEM YOUR FAILED TO ADDED EQUAL SIGN TO THE VARIABLE WHICH FETCH INFORMATION TO THE DATABASE **
**问题您未能将等号添加到将信息提取到数据库的变量 **
Hell guys i tried to remove PHP - Fatal error: Unsupported operand types this error will occur during us fail to declare the variable correct so you will solve it this error by add the sign "=" Example if you try to type like this:
该死的家伙,我试图删除 PHP - 致命错误:不支持的操作数类型此错误将发生在我们未能正确声明变量的过程中,因此您将通过添加符号“ =”来解决此错误,例如,如果您尝试这样键入:
$sa="SELECT * FROM `anquiz` WHERE user_id = $a";
$af=mysql_query($sa);
while($cv-mysql_fetch_array($af)){
$d1=$cv['id'];
$d2=$cv['answer'];//student answer
$d4=$cv['quiz_id'];// quiz id
$d5=$cv['user_id'];// user id insert his file
$d6=$cv['time'];
you will get error of fatal unsupported operand by sign -on varible that carry the mysql_fetch_array instead of adding =Take that example` by adding that sign you will see an error
您将通过符号获得不支持的致命操作数错误-在携带 mysql_fetch_array 而不是添加=Take that example` 的变量上添加该符号,您将看到错误
回答by Nimes
The problem of fatal error unsupported operand type is cause by failing to add the equal sign to the mysql fetch array after variable.Try this write the statement like that and use these examples like adding - or _ You will see fatal error.
不支持的操作数类型的致命错误问题是由于未能在mysql fetch 数组变量后添加等号引起的。试试这个写这样的语句并使用这些示例,例如添加 - 或 _ 您将看到致命错误。
$query=mysql_query("SELECT * FROM user");
$fetch=mysql_fetch_array($query);
回答by user3548683
- $this->vars,
- this->$vars,
- $this->$vars.
- $this->vars,
- 这个-> $vars,
- $this->$vars。
Only number 1 is correct, 2 & 3 are not
只有数字 1 正确,2 和 3 不正确