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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:09:08+00:00 2026-05-19T01:09:08+00:00

I’ve long used assignment inside long if statements to simplify things. But in this

  • 0

I’ve long used assignment inside long if statements to simplify things. But in this case, it’s not working as intended. Here’s what I’ve got:

$Employee = GetEmployeeName($Record['employee_id']);
echo 'First name: '.$Employee['first_name'];
echo 'Last name: '.$Employee['last_name'];
echo 'Record first name: '.$Record['first_name'];
echo 'Record last name: '.$Record['last_name'];

echo 'Comparing first name: ';
var_dump(strcasecmp($Employee['first_name'], $Record['first_name']));

echo 'Comparing last name: ';
var_dump(strcasecmp($Employee['last_name'], $Record['last_name']));

echo 'Comparing both together: ';
var_dump(strcasecmp($Employee['first_name'], $Record['first_name']) 
         || strcasecmp($Employee['last_name'], $Record['last_name']));

echo 'All together now: ';
var_dump($Foo = GetEmployeeName($Record['employee_id']) 
         && (strcasecmp($Foo['first_name'], $Record['first_name']) 
             || strcasecmp($Foo['last_name'], $Record['last_name'])));

Here’s the results:

First name: ZACHARY
Last name: TAYLOR
Record first name: Zachary
Record last name: Taylor
Comparing first name: int(0)
Comparing last name: int(0)
Comparing both together: bool(false)
All together now: bool(true)

This makes no sense. Comparing the first name resolves as 0 (false); comparing the last name resolves as 0 (false); comparing them together resolves as false. Yet when I add the assignment into the if, it suddenly resolves as true. I’ve been assigning variables in ifs for a long time and I’ve never seen this happen. The weird thing is, if I assign outside the if, it works fine. So I could do that, but at this point it’s about the principle of finding out what’s going wrong so I can improve my knowledge of the language.

Assignment in this case gives true, as you can see by the fact that $Employee is populated. So, the final var_dump is essentially “true && false”, which should resolve as false. Right?

  • 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-19T01:09:09+00:00Added an answer on May 19, 2026 at 1:09 am

    The boolean operator && has a higher precedence than the assignment operator =. So this:

    $Foo = GetEmployeeName($Record['employee_id']) 
         && (strcasecmp($Foo['first_name'], $Record['first_name']) 
             || strcasecmp($Foo['last_name'], $Record['last_name']))
    

    Is equivalent to:

    $Foo = (GetEmployeeName($Record['employee_id']) 
         && (strcasecmp($Foo['first_name'], $Record['first_name']) 
             || strcasecmp($Foo['last_name'], $Record['last_name'])))
    

    Put the parentheses around the assignment and it works:

    ($Foo = GetEmployeeName($Record['employee_id'])) 
         && (strcasecmp($Foo['first_name'], $Record['first_name']) 
             || strcasecmp($Foo['last_name'], $Record['last_name']))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.