I have a really weird problem: Below in the code listed 4 echo's are pretty much the same, but only the last ones work properly (First two echoes only print the answer of addition/ subtraction, no text).
Here's the code:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
<?php $sk1 = $_POST["sk1"];
$sk2 = $_POST["sk2"];
$veiksm = $_POST["veiksmas"];
switch($veiksm){
default:
echo "Jus nepasirinkote veiksmo";
break;
case "sud":
echo "Sudeties veiksmo rezultatas: " .$sk1 + $sk2;
break;
case "ati":
echo "Atimties veiksmo rezultatas: " .$sk1 - $sk2;
break;
case "dal":
echo "Dalybos veiksmo rezultatas: " .$sk1 / $sk2;
break;
case "dau":
echo "Daugybos veiksmo rezultatas: " .$sk1 * $sk2;
break;
}
?>
</body>
</html>
You have to put brackets around your calculation like:
echo "Atimties veiksmo rezultatas: " . ($sk1 - $sk2);
//^ See here ^
Otherwise you can imagine your echo statement like this:
"Sudeties veiksmo rezultatas: 17" + 5 // Same as 0 + 5, because the string is casted to int which is 0
Also * and / works because they are getting evaluated first
Put parentheses around the calculation, like this:
echo "Sudeties veiksmo rezultatas: " . ($sk1 + $sk2);
The reason for this is the order in which the expression is processed. Without parentheses, PHP evaluates everything from left to right, so it will first concatinate $sk1 to the string. The combined value will be "Sudeties veiksmo rezultatas: 1" (if $sk1 is 1).
After that, $sk2 is added to that value. Because PHP cannot add up a string, it tries to convert it to a number. This conversion fails, because the string starts with a non-numeric text, and defaults to 0 which is added to the value of $sk2.
Multiplication and division operators have higher precedence, so they are evaluated first, overriding the left-to-right order. That's why it works for the last two cases.
Related
I'm learning basics in php, and I was trying to add multiple arguments in echo command. But the variable after <br> is not showing.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".$number1+$number2;
and the output should be:
number 1 is: 10
number 2 is: 20
30
But the output is:
number 1 is: 10
number 2 is: 2020
So what's the error?
used this code
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
$total= $number1+$number2;
echo "<br> ".$total;
?>
The output will be:
number 1 is: 10
number 2 is: 20
30
The other answers just state a solution, this answer explains whats happening and how to prevent the unexpected behavior in 2 ways.
The dot operator has the same precedence as + and -.
Considering
$number1 = 10;
$number2 = 20;
echo "<br> ".$number1+$number2;
The dot you've used is a string operator, not a numeric operator.
What's happening:
"<br>" and 10 are concatenated with the dot operator to "<br>10".
"<br>10" is added to $number2 (20) with the numeric + operator.
Non-empty, non-numeric strings are converted to 0. Meaning "<br>10" = 0.
0+20 results in 20 which makes line 3: echo 20;
This can be solved by changing the precedences by using brackets echo "<br> ". ($number1 + $number2); or the less seen option, by passing more arguments to the echo language construct: echo "<br> ", $number1 + $number2; (Note the comma instead of a dot). Each argument will be evaluated first before outputting them all together.
Personally I use the second option (multiple arguments) in cases like this.
Just add the braces to the sum operation.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".($number1+$number2);
The output will be:
number 1 is: 10
number 2 is: 20
30
You should revise code within bracket
echo "<br> ". ($number1 + $number2);
to get the result you want.
Reason: each operation has precendence level
Get reference: http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html
This question already has answers here:
Concatenation with addition in it doesn't work as expected
(2 answers)
Closed 7 years ago.
Please explain how echo understand the dot(.) with mathematical expressions and binary comma(,).
<?php
echo "The Sum: " . 2+3;
?>
//Output
3
Why 3 as output?
. and + are left-associative, so your statement is interpreted as
echo ("The Sum: " . 2) + 3;
This is equivalent to
echo "The Sum: 2" + 3;
When you add a string and a number, it converts the string to a number, which tries to find a number at the beginning of the string. Since "The Sum: 2" doesn't begin with a number, it converts to 0. So that makes the statement equivalent to
echo 0 + 3;
which simplifies to
echo 3;
and that's the result you see.
there is two operator dot(.) and plus(+) and dot has high priority so . try this
<?php
echo ("The Sum: " . 2) + 3;
?>
I was experimenting with weak/dynamic typing properties of PHP in preparation for a test and was completely baffled by the output of this string concatenation. Can someone explain how this is even possible?
<?php echo 1 . "/n" . '1' + 1 ?><br />
output:
2
Analysis:
echo 1 . "/n" . '1' + 1;
is equivalent to
//joined first 3 items as string
echo "1/n1"+1;
is equivalent to
//php faces '+' operator, it parses '1/n1' as number
//it stops parsing at '/n' because a number doesn't
//contain this character
echo "1"+1;
is equivalent to
echo 1+1;
Hi I have some simple code here that as far as I can tell should be working but it doesn't and I am puzzled as to why
//VARIABLES
$rowcount=15;
$colcount=15;
$path= array( array());
$startrow=8;
$startcol=7;
$row=$startrow;
$col=$startcol;
for ($a=0;$a<$rowcount;$a++) {
for ($b=0;$b<$colcount;$b++) {
$path[$a][$b]=0;
}
}
echo"In case one ";
echo"<BR>";
echo " Col= ".$col;
echo " Row= ".$row-1;
echo " Col= ".$col;
echo " Cell= ".$path[$row-1][$col];
echo " Cellx= ".$path[7][7];
if($path[$row-1][$col]=="o") {
echo " F1 = ".$flagOne;
$flagOne="1";
echo " cell equals ==o==";
echo " F1 = ".$flagOne;
} else {
echo " cell not equal ==o==";
}
echo"<BR>";
This is what I get as a output
In case one
Col= 7-1 Col= 7 Cell= 0 Cellx= 0 F1 = 0 cell equals ==o== F1 = 1
what puzzles me is the 7th spot "-1"
I would expect to see "Row= 7" not "-1" since $row=8
I have been programming in java and C and lately just barely touching PHP lately but I have been doing PHP for years, I think this should be working
what am i doing wrong
As another issue
looking at the if statement the value of the array/cell = 0 so I would also expect the evaluation to be false and echo this
cell not equal ==o==
but the condition is evaluating true why is this
my one thought is the way I am setting up the $path variable
I simply want it to be a simple two dimensional array $path[$a][$b] is it setup correctly when I output the array I see what I am expecting an two dimensional array.
Never had to do these much in the past
$rowcount=15;
$colcount=15;
$path= array( array());
$startrow=8;
$startcol=7;
$row=$startrow;
$col=$startcol;
for ($a=0;$a<$rowcount;$a++) {
for ($b=0;$b<$colcount;$b++) {
$path[$a][$b]=0;
}
}
echo"In case one ";
echo"<BR>";
echo " Col= ".$col;
echo " Row= ".($row-1);
echo " Col= ".$col;
echo " Cell= ".$path[$row-1][$col];
echo " Cellx= ".$path[7][7];
if($path[$row-1][$col]==0) {
echo " F1 = ".$flagOne;
$flagOne="1";
echo " cell equals ==0==";
echo " F1 = ".$flagOne;
} else {
echo " cell not equal ==0==";
}
echo"<BR>";
At first, dont use double-quotes all over the script. There is a special meaning to double and single quotes in PHP, read the manual about this. Second - character "o" is not zero. You can clearly see, that You are trying to match zero against lower-case "o". Also, integers does not need to be quoted in PHP just as in about every language out there. Check Your font settings to differ one from another. Third - If You are doing both evaluation and concatenation in PHP, You should surround evaluation statement with parentheses, although it is a bad practice. It is better to pre-calculate values and assign them to values before sending them to output or before concatenating them with another values as strings.
Here is a simple php program which gives a strange output. Can anyone explain why it is coming like this and how to get the expected output?
<?php
$a=2;$b=3;
echo "<br> ADD:".$a+$b;
echo "<br> SUB:".$a-$b;
echo "<br> MUL:".$a*$b;
echo "<br> DIV:".$a/$b;
?>
Output:
3-3
MUL:6
DIV:0.66666666666667
Expected Output:
ADD:5
SUB:-1
MUL:6
DIV:0.66666666666667
It is because the string concatenation operator . has the same precedence as the add/sub operators, and all of them are left-associative. This means that evaluation proceeds left-to-right, so "<br> ADD:".$a is evaluated first and the result is added to 3. This particular string converts to zero and 0 + 3 = 3. Similar for the subtraction.
Solution: put the arithmetic in parentheses.
echo "<br> ADD:".($a+$b);
echo "<br> SUB:".($a-$b);
On the other hand, mul/div have higher precedence than concatenation so they produce the expected result.