Expresions

[Solved] Expresions | Basic - Code Explorer | yomemimo.com
Question : expresions

Answered by : carlos-rodolfo-callejas-landa

Note that even though PHP borrows large portions of its syntax from C, the ',' is treated quite differently. It's not possible to create combined expressions in PHP using the comma-operator that C has, except in for() loops.
Example (parse error):
<?php
$a = 2, $b = 4;
echo $a."\n";
echo $b."\n";
?>
Example (works):
<?php
for ($a = 2, $b = 4; $a < 3; $a++)
{
  echo $a."\n";
  echo $b."\n";
}
?>
This is because PHP doesn't actually have a proper comma-operator, it's only supported as syntactic sugar in for() loop headers. In C, it would have been perfectly legitimate to have this:
int f()
{
  int a, b;
  a = 2, b = 4;
  return a;
}
or even this:
int g()
{
  int a, b;
  a = (2, b = 4);
  return a;
}
In f(), a would have been set to 2, and b would have been set to 4.
In g(), (2, b = 4) would be a single expression which evaluates to 4, so both a and b would have been set to 4.

Source : https://www.php.net/manual/es/language.expressions.php | Last Update : Tue, 19 Jul 22

Answers related to expresions

Code Explorer Popular Question For Basic