Evaluation of assignment in php

Asked
Active3 hr before
Viewed126 times

5 Answers

assignment
90%

Stack Overflow Public questions & answers , Meta Stack Overflow ,In the context of a condition (eg. while or if), (x = y) is both an assignment and an expression. It returns the value that's being assigned., Stack Overflow help chat

So, basically, you could write:

$buffer = fgets($handle, 4096);
while ($buffer !== false) {
   echo $buffer;

   $buffer = fgets($handle, 4096);
}

PS: The most common example for an assignment in a while loop is probably fetching rows from mysql:

while ($row = mysql_fetch_assoc($result)) {
   echo $row['firstname'].
   ' '.$row['lastname'];
}
load more v
88%

An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference. Objects may be explicitly copied via the clone keyword. , The new operator returns a reference automatically, as such assigning the result of new by reference is an error. , The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to"). , Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop.

Parse error: syntax error, unexpected 'new'(T_NEW) in …
72%

Evaluation Of Expressions In PHP (as of 7.4) Sat, 20 Mar 2021 ,PHP Expressions Overview ,Errors During Evaluation,Validation is documentation Fri, 18 May 2018

However, this code will fail because function arguments are evaluated at the moment of the call:

function _if($x, $a, $b) {
   if ($x != 0) {
      return $a;
   }
   return $b;
}

$x = 0;
_if($x, 1 / $x, 1 * $x); // Division by zero
load more v
65%

When evaluating the assignment operator (=), PHP evaluates the expression on the right side first and assigns the result to the variable on the left side. For example:,On the left side of the assignment operator (=) is a variable to which you want to assign a value. And on the right side of the assignment operator (=) is a value or an expression.,Use PHP assignment operator (=) to assign a value to a variable. The assignment expression returns the value assigned.,Use concatenation assignment operator (.=)to concatenate strings and assign the result to a variable in a single statement.

PHP uses the = to represent the assignment operator. The following shows the syntax of the assignment operator:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
$variable_name = expression;
Code language: PHP(php)
load more v
75%

If the -- or ++ operator appears before the variable then the interpreter will first evaluate it and then return the changed variable:,PHP uses the = symbol as an assignment operator. The following line sets the value of $var to 1:,If the -- or ++ operator appears after the variable then the interpreter will return the variable as it was before the statement executed and then increment the variable:,The values and variables that are used with an operator are known as operands.

PHP uses the = symbol as an assignment operator. The following line sets the value of $var to 1:

$var = 1;

Assign by reference:
By default PHP assigns all variables other than objects by value and not by reference.PHP has optimizations to make assignment by value faster than assigning by reference, but if you want to assign by reference you can use the & operator as follows:

$var1 = 1;
$var2 = & $var1; // assign by reference
$var2 += 3;
echo $var1; // 4

Assign a string value to a variable:

$var = "test string";

Concatenate two strings together to produce "test string":

$var = "test".
" string";

Add a string to the end of another to produce "test string":

$var = "test";
$var = $var.
" string";

Here is a shortcut to add a string to the end of another:

$var = "a";
$var. = " test";
//a test

Using an operator, you can manipulate the contents of one or more variables or constants to produce a new value. For example, this code uses the addition operator ( + ) to add the values of $x and $y together to produce a new value:

$x = 4;
$ = 7;
$z = $x + $y;

Sum integers to produce an integer:

$var = 4 + 7;

Subtraction, multiplication, and division that might have a result that is a float or an integer, depending on the initial value of $var:

$var = (($var - 5) * 2) / 3;

Multiply to double a value:

$var = $var * 2;
$var *= 2;

Halve a value:

$var = $var / 2;
$var /= 2;

These work with float types too:

$var = 123.45 * 28.2;

$var = 5 % 4; //1

4 exponent (or power) of 2:

$var = 2 ** 4; //16

These all add 1 to $var:

$var = $var + 1;
$var += 1;
$var++;

And these all subtract 1 from $var:

$var = $var - 1;
$var -= 1;
$var--;

If the -- or ++ operator appears before the variable then the interpreter will first evaluate it and then return the changed variable:

$var = 1;
echo++$var; //2
echo $var; //2

echo--$var; //1
echo $var; //1

If the -- or ++ operator appears after the variable then the interpreter will return the variable as it was before the statement executed and then increment the variable:

$var = 1;
echo $var++; //1
echo $var; //2

echo $var--; //2
echo $var; //1

For example, in the following fragment $variable is assigned a value of 32 because of the precedence of multiplication over addition:

$variable = 2 + 5 * 6;
echo $variable; //32

The result is much clearer if parentheses are used:

$variable = 2 + (5 * 6);
echo $variable; //32

But the following example displays the different result, because parentheses have the highest precedence in evaluation.

$variable = (2 + 5) * 6;
echo $variable; //42

Let’s start with the most fundamental part of any programming language: expressions. An expression in PHP is anything that evaluates to a value; it is a combination of values, variables, operators, and functions that results in a value. Here are some examples of expressions:

$str = 'abcdef';
$x = 7;
$y = 4;
$z = $x + $y;
$x - $y;
$x;
4 + 7;
3;
true;
null;
load more v

Other "assignment-undefined" queries related to "Evaluation of assignment in php"