奇怪的 PHP 错误:'不能在写上下文中使用函数返回值'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1532693/
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
Weird PHP error: 'Can't use function return value in write context'
提问by cfischer
I'm getting this error and I can't make head or tail of it.
我收到这个错误,我无法理解它。
The exact error message is:
确切的错误消息是:
Fatal error: Can't use function return value in write context in /home/curricle/public_html/descarga/index.php on line 48
致命错误:无法在第 48 行的 /home/curricle/public_html/descarga/index.php 中的写入上下文中使用函数返回值
Line 48 is:
第 48 行是:
if (isset($_POST('sms_code') == TRUE ) {
What could be going on here?
这里会发生什么?
Here's the full function:
这是完整的功能:
function validate_sms_code() {
$state = NOTHING_SUBMITED;
if (isset($_POST('sms_code') == TRUE ) {
$sms_code = clean_up($_POST('sms_code'));
$return_code = get_sepomo_code($sms_code);
switch($return_code) {
case 1:
//no error
$state = CORRECT_CODE;
break;
case 2:
// code already used
$state = CODE_ALREADY_USED;
break;
case 3:
// wrong code
$state = WRONG_CODE;
break;
case 4:
// generic error
$state = UNKNOWN_SEPOMO_CODE;
break;
default:
// unknown error
$state = UNKNOWN_SEPOMO_CODE;
throw new Exception('Unknown sepomo code: ' . $return_code);
break;
}
} else {
$state = NOTHING_SUBMITED;
}
dispatch_on_state($state);
}
采纳答案by chaos
You mean
你的意思是
if (isset($_POST['sms_code']) == TRUE ) {
though incidentally you really mean
虽然顺便说一句,你的意思是
if (isset($_POST['sms_code'])) {
回答by rolfen
This also happens when using empty on a function return:
在函数返回时使用 empty 也会发生这种情况:
!empty(trim($someText)) and doSomething()
because empty is not a function but a language construct (not sure), and it only takes variables:
因为 empty 不是函数而是语言结构(不确定),它只接受变量:
Right:
对:
empty($someVar)
Wrong:
错误的:
empty(someFunc())
Since PHP 5.5, it supports more than variables. But if you need it before 5.5, use trim($name) == false. From empty documentation.
自 PHP 5.5 起,它支持的不仅仅是变量。但是如果您在 5.5 之前需要它,请使用trim($name) == false. 来自空文档。
回答by TigerTiger
if (isset($_POST('sms_code') == TRUE ) {
change this line to
将此行更改为
if (isset($_POST['sms_code']) == TRUE ) {
You are using parentheseis () for $_POSTbut you wanted square brackets []
您正在使用括号 ()$_POST但您想要方括号 []
:)
:)
OR
或者
if (isset($_POST['sms_code']) && $_POST['sms_code']) {
//this lets in this block only if $_POST['sms_code'] has some value
回答by T.Todua
for WORDPRESS:
对于WORDPRESS:
instead of:
代替:
if (empty(get_option('smth')))
should be:
应该:
if (!get_option('smth'))
回答by middus
Correct syntax (you had a missing parentheses in the end):
正确的语法(最后缺少括号):
if (isset($_POST['sms_code']) == TRUE ) {
^
p.s. you dont need == TRUEpart, because BOOLEAN (true/false) is returned already.
ps 你不需要 == TRUE部分,因为 BOOLEAN (true/false) 已经返回。
回答by Mehdi Karamosly
This can happen in more than one scenario, below is a list of well known scenarios :
这可能发生在多个场景中,以下是众所周知的场景列表:
// calling empty on a function
empty(myFunction($myVariable)); // the return value of myFunction should be saved into a variable
// then you can use empty on your variable
// using parenthesis to access an element of an array, parenthesis are used to call a function
// 使用括号访问数组元素,括号用于调用函数
if (isset($_POST('sms_code') == TRUE ) { ...
// that should be if(isset($_POST['sms_code']) == TRUE)
This also could be triggered when we try to increment the result of a function like below:
当我们尝试增加如下函数的结果时,这也可能被触发:
$myCounter = '356';
$myCounter = intVal($myCounter)++; // we try to increment the result of the intVal...
// like the first case, the ++ needs to be called on a variable, a variable should hold the the return of the function then we can call ++ operator on it.
回答by Diego
The problem is in the ()you have to go []
问题在于()你必须去[]
if (isset($_POST('sms_code') == TRUE)
by
经过
if (isset($_POST['sms_code'] == TRUE)
回答by husnixs
I also had a similar problem like yours. The problem is that you are using an old php version. I have upgraded to PHP 5.6 and the problem no longer exist.
我也有和你一样的问题。问题是您使用的是旧的 php 版本。我已经升级到 PHP 5.6,问题不再存在。
回答by tomelin5
Another scenario where this error is trigered due syntax error:
由于语法错误触发此错误的另一种情况:
ucwords($variable) = $string;
回答by user12845085
This error is quite right and highlights a contextual syntax issue. Can be reproduced by performing any kind "non-assignable" syntax. For instance:
这个错误是非常正确的,并突出了上下文语法问题。可以通过执行任何类型的“不可分配”语法来复制。例如:
function Syntax($hello) { .... then attempt to call the function as though a property and assign a value.... $this->Syntax('Hello') = 'World';
function Syntax($hello) { .... 然后尝试像调用一个属性一样调用该函数并分配一个值.... $this->Syntax('Hello') = 'World';
The above error will be thrown because syntactially the statement is wrong. The right assignment of 'World' cannot be written in the context you have used (i.e. syntactically incorrect for this context). 'Cannot use function return value' or it could read 'Cannot assign the right-hand value to the function because its read-only'
上面的错误会被抛出,因为语句在语法上是错误的。'World' 的正确赋值不能写在您使用的上下文中(即在此上下文中语法不正确)。'不能使用函数返回值' 或者它可以读取'不能将右手值分配给函数,因为它是只读的'
The specific error in the OPs code is as highlighted, using brackets instead of square brackets.
OPs 代码中的具体错误已突出显示,使用方括号而不是方括号。

