Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:44:34+00:00 2026-06-11T20:44:34+00:00

Could I do this in PHP? if ($Var = $this->test1() == true) { var_dump($Var);

  • 0

Could I do this in PHP?

if ($Var = $this->test1() == true) {
    var_dump($Var);
}

public function test1() {
    return true;
}

It echoes true, but I’m not sure if this is the correct way to check such return values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:44:36+00:00Added an answer on June 11, 2026 at 8:44 pm

    You can but there is 2 things you should be aware of :

    When you do something like :

    if ($Var = $this->test1() == true) {
    

    The operators are confusing :

    • do you want to do $this->test1() == true and store result $var
    • do you want to do $var = $this->test1() and compare it with true

    In your case, $this->test1() returns true so it does not matter. But if we change your code a bit :

    if ($Var = $this->test1() == 5) {
        var_dump($Var);
    }
    
    public function test1() {
        return 3;
    }
    

    Someone who read your code will not understand if you want to store $this->test1() in $Var (so, make 3 in var) or if you want to put result of comparison $this->test1 == 5 in $Var (false).

    What remains in $Var at the end may be a very good question at the PHP 5.3 Certification but not in a useful case.

    To avoid mistakes, uses parenthesis :

    if (($var = $this->test1()) == true) {
    

    You should take care of types :

    I give you an example of what could return something castable to true :

    function test1() { return true; }
    function test2() { return 3; }
    function test3() { return 3.42; }
    function test4() { return "x"; }
    function test5() { return array('x'); } // array() == true returns false
    function test6() { return new stdClass(); }
    
    echo test1() == true;
    echo test2() == true;
    echo test3() == true;
    echo test4() == true;
    echo test5() == true;
    echo test6() == true;
    
    // outputs 111111 (1 = true)
    

    To avoid mistakes, you should use === operator. Your final piece of code becomes :

    if (($var = $this->test1()) === true) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if this is a trivial questions but in a PHP class:
I know I could use PHP to do this, but wanted to find out
Probably really easy for a pro, but could someone re-write this from it's PHP
How could i convert this string 03/16/2012 03:22PMusing php to store in datetime format
a while ago i could comment any code in php with netbeans like this:
This could be a very basic Javascript I know but I just can't get
I could spike this to find out, but I'm going to use SO. In
I am not even sure how to ask. I do this all the time
Could this be done with javascript/jQuery : <div class=number>99,123,123</div> <div class=number>123,123,123</div> <div class=number>1,123,123,123</div> <div
I found this interesting behavior. Why could this happen I am using the cmdlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.