PHP error Can't use method return > value in write context

10,820

Solution 1

You can't use isset for the result of a function. Consider the following code instead:

if( $this->session->get('user_id') ){
    //run code
}

Solution 2

isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.

From the PHP Manual.

Solution 3

You can't use isset on a function. However, since false, 0, and '' all equate to a falsey statement, write your test this way:

if( $id = $this->sessions->get('user_id') ){
   // Will only run if $id does not equal '', False, or 0
}

That way you have run your test and assigned the variable in one step.

Share:
10,820
JasonDavis
Author by

JasonDavis

PHP/MySQL is my flavor of choice however more recently JavaScript is really becoming something I enjoy developing with! Writing code since 2000' Currently working heavily with SugarCRM + Launching my Web Dev company ApolloWebStudio.com "Premature optimization is not the root of all evil, lack of proper planning is the root of all evil." Twitter: @JasonDavisFL Work: Apollo Web Studio - https://www.apollowebstudio.com Some of my Web Dev skills, self rated... +------------+--------+------+--------------+ | Skill | Expert | Good | Intermediate | +------------+--------+------+--------------+ | PHP | X | | | +------------+--------+------+--------------+ | MySQL | X | | | +------------+--------+------+--------------+ | Javascript | X | | +------------+--------+------+--------------+ | jQuery | X | | +------------+--------+------+--------------+ | CSS+CSS3 | X | | +------------+--------+------+--------------+ | HTML+HTML5 | X | | | +------------+--------+------+--------------+ | Photoshop | | X | | +------------+--------+------+--------------+ | Web Dev | X | | | +------------+--------+------+--------------+ | SugarCRM | X | | | +------------+--------+------+--------------+ | Magento | | X | | +------------+--------+------+--------------+ | WordPress | X | | | +------------+--------+------+--------------+ | SEO | X | | | +------------+--------+------+--------------+ | Marketing | X | | | +------------+--------+------+--------------+ |Social Media| X | | | +------------+--------+------+--------------+

Updated on June 09, 2022

Comments

  • JasonDavis
    JasonDavis almost 2 years

    I am getting this error in a PHP class...

    Fatal error: Can't use method return value in write context in C:\webserver\htdocs\friendproject2\includes\classes\User.class.php on line 35

    Here is the troubled part.

    if(isset($this->session->get('user_id')) && $this->session->get('user_id') != ''){
        //run code
    }
    

    This code is in my contrustor, is a value is not already set for $this->session->get('user_id') then it will return false instead of a Number. So as you can see I was hoping to check if this value is a number or not or not even set.

    Any help with fixing appreciated.