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;
?>
Related
This question already has answers here:
Adding string with number in php
(6 answers)
Closed 2 years ago.
I'm a beginner in PHP. with my experience something like "number + String + number" should be a string.
But apparently, it's a number in php.
in my case:
echo 14 + ' ' + 12;
// returns 26
// expected: "14 12"
why did this happen?
and how to fix it?
You are adding a '+' for adding two numbers. These are not a string so it will do a addition.
Use this if you want to add these couple of number as a couple of string :-
echo '14'.' '.'12';
// result "14 12";
You can also use with a html code for a blank space :
echo '14 12';
// result "14 12";
Because echo accepts parameters only inside quotes. If you want text being returne use:
echo"14 12";
In you case PHP does 2 things
Takes 14 adds to 12
Concatenates space.
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:
PHP string number concatenation messed up
(5 answers)
Closed 4 years ago.
To keep it short, why does
<?php
$var = 'Calc: ' . 5 - 5 . '!';
echo $var;
?>
output:
-5!
Instead of, let's say:
Calc: 0 . '!'
Or another variation of this problem:
<?php
echo "time is" . time()-2;
?>
Prints:
-2
Notice the corruption, the first "string" with the first int is chopped off!
Though, < $var = 'Calc: ' . (5 - 5) . '!'; > (does work ok), I'm trying to understand what the key concept behind this behavior is.
Good old math comes and saves the day.
Adding () means it will be calculated first and give you a correct string.
$var = 'Calc: ' . (5 - 5) . '!';
echo $var; //Calc: 0!
https://3v4l.org/p8OYf
This question already has answers here:
Compare floats in php
(17 answers)
Closed 4 years ago.
This piece of code:
$total=$o->cart->getTotalSum();
$subTotal=$o->cart->getSubTotal();
if(floatval($r['sum_total']) != floatval($total) ||
floatval($r['sum_sub']) != floatval($subTotal)) {
echo 'Differs on #' . $r['id'];
echo 'Total: ' . $total . ' / ' . $r['sum_total'];
echo 'Sub: ' . $subTotal . ' / ' . $r['sum_sub'];
}
Gives me this output:
Differs on #697
Total: 19.6 / 19.6
Sub: 19.6 / 19.6
Why? How is that even possible?
I make sure that all values compared are of type float, so no strings could have slipped in.
I must be missing something.
My apologies for not providing really reproducible code, but i wouldn't know how in this case.
If you do it like this they should be the same. But note that a characteristic of floating-point values is that calculations which
seem to result in the same value do not need to actually be identical.
So if $a is a literal .17 and $b arrives there through a calculation
it can well be that they are different, albeit both display the same
value.
Usually you never compare floating-point values for equality like
this, you need to use a smallest acceptable difference:
if (abs(($a-$b)/$b) < 0.00001) { echo "same"; } Something like that.
I think someone else had the exact same problem.
https://stackoverflow.com/a/3148991/2725502
I have two strings,one string which is an output from an API and
another which is stored in the MYSQL Database as longtext.I am trying
to compare these two strings,so here's what I did:
echo $stringfromMyDatabase;
echo "<br">;
echo $stringfromMyApi;
echo "<br>";
echo strcmp($stringfromMyDatabase,$stringfromMyapi);
echo "<br>";
echo "StringfrommyDatabase :".strlen($stringfromMyDatabase)."and StringfromApi:".strlen($stringfromMyApi);
and Here's the output I obtained:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1
StringfrommyDatabase :25 and StringfromApi:17
Although the string looks exactly similar while echoing them out,How
do I know how and where these two strings differ and How do i print
the two strings with all special characters enlcosed?
Any help with proper explanation will be highly appreciated!
use var_dump($stringfromMyDatabase, $stringfromMyApi) and look at this.
The strings are different according to the strlen you performed on them. I am pretty sure that your database stored the string with additional space characters (blank spaces, tabs, ...).
Try this:
$len = strlen($stringfromMyDatabase);
for ($i = 0; $i < $len; $i++) {
if ($stringfromMyDatabase[$i] != $stringfromMyapi[$i])
echo "--{$stringfromMyDatabase[$i]}-- (code " . ord($stringfromMyDatabase[$i]) .
") != --{$stringfromMyapi[$i]}-- (code " . ord($stringfromMyapi[$i]) . ") # char pos {$i}\n";
}
This will show you where the strings differ and what are the (possibly non printable) characters that are different.