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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:12:54+00:00 2026-05-28T06:12:54+00:00

$var4 = 123; function fn1($p1) { return array(‘p1’ => 1, ‘p2’ => 2); }

  • 0
$var4 = 123;

function fn1($p1)
{
    return array('p1' => 1, 'p2' => 2);
}

if ($var1 = fn1(1) AND $var4 == 123)
{
    print_r($var1);
}

if ($var2 = fn1(1) && $var4 == 123)
{
    print_r($var2);
}

if (($var3 = fn1(1)) && $var4 == 123)
{
    print_r($var3);
}
  1. If you run this simple script it will output strange results, at
    least for me!! First output from first if expression will result in
    an array returned from the function & assigned to the $var1
    variable, which is what I’m expecting, well?
  2. Second output from second if expression will result in an integer
    ‘1’ assigned to the $var2 variable, which is NOT expected at all!!
    Please note that the only changed thing is the logical operator,
    I’ve used ‘&&’ rather than ‘AND’, that’s all!!
  3. Third output from third if expression will result again the expected
    array returned from the function & assigned to the $var3 variable,
    exactly as the first if expression, but wait: I’ve just embraced the
    assignment statement in the if expression within brackets, while
    still using the second if expression code!!

Can anyone explain technically -in details- why this strange behavior? php.net reference links will be appreciated.

I know that ‘&&’ has higher precedence than ‘AND’ but that doesn’t explains it to me!!

  • 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-28T06:12:55+00:00Added an answer on May 28, 2026 at 6:12 am

    PHP: Operator Precendence

    && has a higher precedence than =, so in the second if, you are assigning the value of fn1(1) && $var4 == 123 (true or false) to $var2.

    In the first if, AND has a lower precedence than =, so the assignment happens first, then the result is compared.

    In the third if, the assignment happens first again because everything in parens gets processed first.

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

Sidebar

Related Questions

I have something like : print \n,|,id,|,var1,|,var2,|,var3,|,var4,| It prints with spaces for each variable.
here is my simplified data set: foo <- data.frame(var1= c(1:10), var2=rep(1:5,2),var3=rep(1:2,5),var4=rep(3:7,2) ) all together
I have a list of tuples like so: ListOfTuples = [(var1,var2,var3,var4,var5), (var6,var7,var8,var9,var10), (var11,var12,var13,var14,var15), (var16,var17,var18,var19,var20)]
this is my function; function ButtonClickAction3 (zEvent) { var var4 = prompt (text); var
I want to have something like var1 = ('a','b','c') var2 = ('a1','b1','c1') var3 =
to become more specific, here is an example: > expand.grid(5, 5, c(1:4,6),c(1:4,6)) Var1 Var2
Here's my code: function mouseOver(variable) { return function() { $(variable).fadeIn(100); }; } function mouseOut(variable)
Suppose I have a string such as this in a text file: (((var1 AND
I am debugging the following lines of code if (var.getvar2() != var3) { var4.add(var);
I have a table like: id NameColumn ------------- 1 var 2 var2 3 var3

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.