php 不能在写上下文中使用方法返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6730216/
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
Can't use method return value in write context
提问by Roman
The following if condition is causing an error:
以下 if 条件导致错误:
if( !empty ($this -> session -> userdata( 'user_id') ) )
{
$user_id = $this -> session -> userdata( 'user_id' );
}
else
{
$error = "ID doesn't exist";
}
I get following error:
我收到以下错误:
Fatal error: Can't use method return value in write context in (line number)
How can I fix it?
我该如何解决?
回答by Francois Deschenes
Replace the first if
statement with if( ! $this->session->userdata('user_id') )
.
用 替换第一个if
语句if( ! $this->session->userdata('user_id') )
。
The empty
function check if a variable is empty but userdata
is a function. Additionally, for your information, according to the CodeIgniter documentation, the userdata
function returns FALSE
if the item doesn't exist, which means that if user_id
doesn't exist in your session, the code in the else
will be executed.
该empty
函数检查变量是否为空但它userdata
是一个函数。此外,对于您的信息,根据CodeIgniter 文档,如果该项目不存在,该userdata
函数将返回FALSE
,这意味着如果user_id
您的会话中不存在,else
则将执行 中的代码。
回答by Sajjad Ashraf
If you are using the sessions library of codeigniter then you don't need to check your data
codeigniter already does that for you.
if $this -> session -> userdata( 'user_id')
is empty or not set then it will be returned as false. So just a code something like that
如果您使用的是 codeigniter 的会话库,那么您不需要检查您的数据 codeigniter 已经为您完成了。如果$this -> session -> userdata( 'user_id')
为空或未设置,则它将返回为 false。所以只是一个类似的代码
$user_id = $this->session->userdata('user_id');
if(!$user_id){ }else{ $error = 'ID doesn't exist'; }
回答by Devang Hingu
if( ($this -> session -> userdata( 'user_id') > 0)
if(($this -> session -> userdata('user_id') > 0)
it is more better instead of if( !$this -> session -> userdata( 'user_id') )
它比if( !$this -> session -> userdata( 'user_id') )更好
because sometimes it will make error
因为有时它会出错
回答by Rolice
Function empty() cannot be used in this way. It expects reference argument, but not a direct function return. A function return is not considered a variable, which can be passed by reference.
函数 empty() 不能以这种方式使用。它需要引用参数,而不是直接函数返回。函数返回不被视为变量,它可以通过引用传递。
$test = $this->session->userdata('user_id');
if(!empty($test))
$user_id = $test;
else
$error = "ID doesn't exist";
回答by Suraj Bhatt
Sometimes this error caused due to PHP version. Latest Codeigniter version support above PHP 7.0. You just need to update PHP version from cpanel to 7.0 or above. Hope this help others those who are using latest codeigniter. I had same issue on below line.
有时这个错误是由 PHP 版本引起的。最新的 Codeigniter 版本支持 PHP 7.0 以上。您只需要将 PHP 版本从 cpanel 更新到 7.0 或更高版本。希望这可以帮助其他使用最新 codeigniter 的人。我在下面有同样的问题。
if($ci->session->userdata('front_logged_in') && !$ci->session->userdata('front_logged_in')){