I have the follow:
$sum = 10 + 10;
is the above line executed saved to $sum
as 20 to use it from now on or if everytime i echo $sum
, it will run the 10+10?
It only calculates it once to set the value of $sum.
So from then on out $sum is equal to 20.
So the 10+10 is only calculated the first time.
During the lifetime of the script the value of 10 + 10 will be assigned to the $sum variable - no further 10 + 10 will be calculated when using $sum.
PHP is not inherently lazy nor it has lazy primitives, so value assignation is executed immediately. To simulate some sort of lazy functionality you can declare a function:
funciton sumTen() {
return 10 + 10;
}
sumTen() // will calculate the value every time.
$sum = 0;
echo $sum; // will print 0
$sum = 10 + 10;
echo $sum; // will print 20
$sum = 5;
echo $sum; // will print 5