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 6375023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:33:39+00:00 2026-05-25T01:33:39+00:00

This is a practice I’ve seen before, but not very often: A variable is

  • 0

This is a practice I’ve seen before, but not very often: A variable is assigned to a value at the same time the value itself is evaluated (or is it the expression itself that is evaluated?). Example:

// Outputs "The value is 1"
$value = 1;
if ($var = $value) {
    echo "The value is $var";
}

Seems to be the same as:

$value = 1;
$var = $value;
if ($var) {
    echo "The value is $var";
}

Another example:

// Outputs "The value is 1"
$value = 1;
echo "The value is ".$var = $value;

I’ve been using this a little bit to shorten up my code, mainly the first example: for evaluating the first variable or expression while assigning it to another within the same expression. Something like this:

if ($status = User::save($data)) {
    echo "User saved.";
}
// do something else with $status

This seems so basic, but I can’t actually find any documentation on this, maybe I’m not sure where to look. I’ve only recently figured out how this works after seeing it for years, and I really like using it, but I don’t want to use it haphazardly.

It makes code shorter, maybe not quite as clear to some, but definitely less repetitive. Are there any caveats with this method? Is this perfectly safe or are there any cases where it might fail or cause unexpected behavior? This doesn’t seem to be a very common practice, so I was hoping to find an explanation before I start “going nuts” with it. If it is documented, links to the correct page will be much appreciated.

  • 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-05-25T01:33:40+00:00Added an answer on May 25, 2026 at 1:33 am

    From https://www.php.net/manual/en/language.expressions.php:

    PHP takes expressions much further, in the same way many other languages do. PHP is an expression-oriented language, in the sense that almost everything is an expression. Consider the example we’ve already dealt with, $a = 5. It’s easy to see that there are two values involved here, the value of the integer constant 5, and the value of $a which is being updated to 5 as well. But the truth is that there’s one additional value involved here, and that’s the value of the assignment itself. The assignment itself evaluates to the assigned value, in this case 5. In practice, it means that $a = 5, regardless of what it does, is an expression with the value 5. Thus, writing something like $b = ($a = 5) is like writing $a = 5; $b = 5; (a semicolon marks the end of a statement). Since assignments are parsed in a right to left order, you can also write $b = $a = 5.

    Many people would argue that you shouldn’t use this behaviour very often. For instance, distinguishing between:

    if ($a == 5) { ... }
    

    and

    if ($a = 5) { ... }
    

    is tricky! The two common idiomatic ways of writing the above to distinguish them:

    if (5 == $a) { ... }
    if (($a = 5)) { ... }
    

    The first is called a yoda condition and causes a syntax error if a equals character is left out. The latter won’t behave any differently when run, but some code checkers will output warnings when the expression doesn’t have the extra parentheses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure what this practice is actually called, so perhaps someone can edit
I have read Java Concurrency in Practice and this is a great reference, but
(Similar in spirit to but different in practice from this question .) Is there
So basically, this is what I have. But is this a good practice? I
I am stumped by this practice problem (not for marks): {w is an element
This question may seem pedantic or just silly, but what is your practice for
I'm not sure if this has been asked or not yet, but how much
I've been trying to self-teach myself how to code but this practice problem is
Is this an okay practice or an acceptable way to use PHP's error suppressing?
What is the general best practice for this type of functionality. I have a

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.