This question already has answers here:
Not getting the specific output without braces [closed]
(3 answers)
Closed 8 years ago.
This code only prints out '17' instead of "10 + 7 = 17". Why is this? And how can i solve this.
<?
$x = 10;
$y = 7;
echo $x . '+' . $y . '=' . $x+$y;
?>
What you should understand is that echo won't do anything until the result of the whole expression given to it is evaluated. This expression contains both . and + operators - the first one concatenates its operands (glues two strings together), the second adds numbers.
Now, you might think that + operator has a higher precedence than of . one - in other words, that result of $x + $y will be calculated before (eventually) glued to the rest of the string. But it's not. So we can say this statement is actually treated as...
echo ($x . '+' . $y . '=' . $x) + $y;
In other words, the result of all the string concatenation is added (converted to number) to $y, and only the result of this operation is printed.
The result of $x . '+' . $y . '=' . $x is '10+7=10', and it doesn't look like something that might be added. But guess what, PHP wants to be nice to you - and it assumes that you actually wanted to extract the first digits of a string when you tried to convert it to a number. So the whole line is treated as number 10. When added to 7, it's just 17 - that's why you got 17 echoed.
One possible workaround is getting rid of ., using , operator instead (as its precedence is lower - in fact, it's the lowest among operators):
echo $x, '+', $y, '=', $x + $y;
It could be simplified if one remember about such a handy feature of PHP as string interpolation:
echo "$x + $y = ", $x + $y;
<?php
$x = 10;
$y = 7;
function sum($a,$b){
return $a+$b;
}
echo $x.'+'.$y.'='.sum($x,$y);
?>
Related
This question already has answers here:
Adding 2 to a variable each time
(5 answers)
Closed 1 year ago.
So I'm trying to make it so the computer counts up to ten by two and making a new line each time. When I tested this my entire computer crashed. What's wrong with it?
<?php
$test = 0;
while ($test < 10) {
$test + 2;
echo $test . "/n";
}
?>
Change:
$test + 2;
to:
$test += 2;
Currently, $test is always 0 in your code, since you are not assigning a new value to it, hence the infinite loop.
Additionally, you would also want to change:
echo $test . "/n";
to:
echo $test . "\n";
Since you need to use a backslash to indicate a new line character.
N.B. In PHP you can also use the PHP_EOL constant to indicate the end of a line.
Here you just sum $test with 2 but did not assign it back to $test
$test + 2;
Trying replace it by $test += 2;
Another point is /n is not "making a new line" as you expected, change it to \n instead.
Although you can use while statement, this is a good case to use for instead.
for($i=0; $i < 10; $i += 2)
{
echo $i . PHP_EOL;
}
Usually while is used to evaluate something every loop, some variable that can change inside the while, like a bool variable or an "infinite" loop until break.
When you have a already defined number of loops, for is usually a better approach.
Can anyone elexplain to me why the output of this code is 22 not 21?
$x=10;
$x+=++$x;
echo $x;
$x += ++$x;
The right-hand side of this assignment is evaluated first:
increment $x → $x is now 11, result of expression ++$x is 11
take the value of $x (11) and add the result of step 1 to it → 22
assign the result of step 2 to $x
x is being incremented by the stored incremented value of x.
x += (x = x + 1).
Internally, this is evaluated as:
# op ext return operands
-------------------------------------
1 ASSIGN !0, 10
2 PRE_INC $2 !0
3 ASSIGN_ADD 0 !0, $2
4 ECHO !0
Assign 10 to $x (referred to above as !0)
Pre-increment $x, i.e.
Add 1 to $x
Return the new result (11)
Increase $x (now 11) by the return value from step 2 (also 11)
Echo the result (22)
(Edited the VLD output for readability, see the full version here: https://3v4l.org/mftI4/vld#output)
Its all about order of operations.
$a+=$b is just a shorthand for $a = $a + $b. So now, "unroll" your 2nd line with that knowledge:
$x = $x + (++$x);
To assign value to $x, we must 1st evaluate right side of the assignment. To do that, we need first to perform the ++ operation, only then our variables on the right are ready to be added.
So what is operator ++ in this context? It is in turn a shorthand for a function, that does something similar to this:
function preIncrement(&$variable) {
$variable = $variable + 1;
return $variable;
}
Note that variable is a reference (&$variable, rather than $variable). What that means is that inside that function, if we modify variable, it will modify the variable that was passed to it, OUTSIDE. So when we pass $x, the function increases $x and then returns some number value. That number value is being replaced in the right side of the assignment.
So, when that line really looks like is:
$x = $x + postIncrement($x);
So, to evaluate we need to first execute the function in the assignment and get the functions return value. It happens to be 11. Great, now we know we need to add 11 to $x.
$x = $x + 11;
Great, lets just read current value of $x and we can assign. $x is 11. postIncrement function increased it to 11 when we executed it. So:
$x = 11 + 11;
So now, $x is 22.
Lets compare that to post incrementation:
$x+=$x++;
Unrolling += ...
$x = $x + $x++;
As before, we need to get return value of $x++ before we can evaluate. Post incrementation looks something like this:
function postIncrement(&$variable) {
$oldValue = $variable;
$variable = $variable + 1;
return $oldValue;
}
So it takes our $x, increases its value, but returns the original value. As a result, the $x++ gets evaluated as 10. Now we arrive at:
$x = $x + 10;
Ok, lets evaluate $x. $x is 11. Post increment increased it when we executed it. So:
$x = 11 + 10;
So $x is 21 in that case.
Hope this helps you.
This question already has answers here:
Php for loop with 2 variables?
(7 answers)
Closed 6 years ago.
My PHP code (so far not working as I want it to) -
for ($x=0, $y=0; $x<=163, $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
What I was trying to achieve is - it should echo even if one condition matches. Right now it stops when one condition is fulfilled, i.e. in this case when y=98, it stopped. Also there may be cases when y>=x in contrast to given code where x>y
Edit -
The duplicate marked question, did not solve my problem as in that question, range of both the variables were same, so it could have been achieved by the mentioned answer (by increasing one variable). In my case both variables has different range.
Also I tried this
for ($x=0, $y=0; $x<=163 || $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
But it's also not helping me to achieve my desired output.
Edit 2 - I think I couldn't explain properly earlier, with what I wanted the output to be.
So I am trying to demonstrate by small e.g. with x=3, y=2
x=1, y=1
x=2, y=1
x=3, y=1
x=1, y=2
x=2, y=2
x=3, y=2
I am trying to achieve something like this. I don't know, what this ?matrix is called in maths (so it's possible my question title is wrong).
You can achieve that through a basic nested loop. Basically you loop over y in the outer loop and for each iteration of y you do a another for loop for x. Try this and adjust x and y as necessary.
<?php
for ($y=0; $y<=98 ; $y++) {
for ($x=0; $x<=163; $x++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
}
This question already has answers here:
Shunting Yard implementation in PHP needed, interpret and parse a string perform a mathematical comparison and return a boolean result
(4 answers)
Closed 7 years ago.
Why this 2 code not echo same result ?
On first code echo *57, But i want to echo 35 like on second code.
How can i do that ?
<?PHP
$fomular = "*5";
$x = "7";
$res = $fomular."".$x;
echo $res;
?>
<?PHP
echo 5*7;
?>
There are a couple of issues.
In your example
...
$res = $fomular."".$x;
...
Is the same as saying
...
$res = "*5".""."3";
...
So that explains the "*53" been echoed/
...
$res = $fomular + $x;
// or
$res = $x + $fomular;
...
Will return 12 as it will not evaluate the "*" but rather add the numbers.
You could use eval( ... ) but it is evil and would really advise against it.
...
echo eval( "5*" . "7" . ";");
...
So your best bet, if you really want to echo the result, would be to write your own function to parse the 2 strings and do the operation.
That is because when you use .(dot) operator it appends the two variables.
You have to use code like below to get the desired output
$formular = 5;
$x=7;
$res=$formular * $x;
echo $res;
$formular = 5;
$x = 7;
$res = $formular * $x;
echo $res;
. (dot) in php is a string concatenating operator! if you try to use it with any type of variables, it converts they to string and put together. Sample: $x = 5 . 7 -> $x = '57'; but, if you use any kind of mathematicals operators (*, /, +, -), php will convert both operands to numeric form (float or int), then execute your formula and give a result $x = 5 * 7; -> $x = 35;
In short: use only mathematicals operators for executing formulas, not a dot. (sory for my bad english)
I have a program in php that will based on post and pre increment like
<?php
$x=5;
echo $x+++$x++;
?>
I execute program and output is 11 but i confused why it prints 11.can anyone please explain how this program execute.
The postincrement operator increments the variable, but returns its old value. So $x++ is equivalent to:
($temp = $x, $x = $x + 1, $temp)
When you do it twice in an expression, it's like:
echo ($temp1 = $x, $x = $x + 1, $temp1) + ($temp2 = $x, $x = $x + 1, $temp2);
The first part sets $temp1 = 5 and increments $x to 6. The second part sets $temp2 = 6 and increments $x to 7. Then it does $temp1 + $temp2 and echoes the result, which is 5 + 6 = 11.
x++ is called postincrement, it returns x and then increases x by one. Opposite is ++x ( preincrement ), increase x by one and then return. + in the middle is arithmetic operator for addition.
$x = 5;
echo $x++ + $x++; // 11
echo $x; // 7
This: echo $x+++$x++ is the same as:
echo ( ($x++) + ($x++) )
So, as you can see, its adding $x++ plus $x++
In PHP, when you run $x++, is the same as saying $x = $x + 1 but returning the original value of $x. (As said in the comments)
So for example if you do this:
$x = 5;
echo $x++; //outputs $x, then adds 1
echo $x++; //outputs $x, then adds 1
/*
THIS IS WHAT THE ABOVE DOES:
set x equal to 5
echo x (OUTPUTS 5), then add 1.
echo x (OUTPUTS 6), then add 1.
*/
With that said, in $x+++$x++, the first reference of $x is when its value is still 5 (before it is incremented) and the second reference to $x is then when its value is 6 (before it is again incremented), Therefore: 5 + 6 = 11
Source: http://php.net/manual/en/language.operators.increment.php