PHP echo with int values [duplicate] - php

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

Related

how to convert a string of numbers and operators to a final number in php [duplicate]

This question already has an answer here:
How to evaluate formula passed as string in PHP?
(1 answer)
Closed 2 years ago.
let's say we have a string like:
"23+sqrt(53)*7"
Is there any way to convert this to a number and print the result of it?
$string = "23+sqrt(53)*7";
$number = eval('return ' . $string . ';');
echo $number;

PHP value comparison wrong despite correct typing [duplicate]

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

PHP echo, how it's produce the output? [duplicate]

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;
?>

A string in PHP that doesn't make sense

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;

PHP string number concatenation messed up

I got some PHP code here:
<?php
echo 'hello ' . 1 + 2 . '34';
?>
which outputs 234,
But when I add a number 11 before "hello":
<?php
echo '11hello ' . 1 + 2 . '34';
?>
It outputs 1334 rather than 245 (which I expected it to). Why is that?
That's strange...
But
<?php
echo '11hello ' . (1 + 2) . '34';
?>
or
<?php
echo '11hello ', 1 + 2, '34';
?>
fixes the issue.
UPDATE v1:
I finally managed to get the proper answer:
'hello' = 0 (contains no leading digits, so PHP assumes it is zero).
So 'hello' . 1 + 2 simplifies to 'hello1' + 2 is 2. Because there aren't any leading digits in 'hello1' it is zero too.
'11hello ' = 11 (contains leading digits, so PHP assumes it is eleven).
So '11hello ' . 1 + 2 simplifies to '11hello 1' + 2 as 11 + 2 is 13.
UPDATE v2:
From Strings:
The value is given by the initial portion of the string. If the string
starts with valid numeric data, this will be the value used.
Otherwise, the value will be 0 (zero). Valid numeric data is an
optional sign, followed by one or more digits (optionally containing a
decimal point), followed by an optional exponent. The exponent is an
'e' or 'E' followed by one or more digits.
The dot operator has the same precedence as + and -, which can yield unexpected results.
That technically answers your question... if you want numbers to be treated as numbers during concatenation, just wrap them in parentheses.
<?php
echo '11hello ' . (1 + 2) . '34';
?>
You have to use () in a mathematical operation:
echo 'hello ' . (1 + 2) . '34'; // output hello334
echo '11hello ' . (1 + 2) . '34'; // output 11hello334
You should check the PHP type conversion table to get a better idea of what's happening behind the scenes.
If you hate putting operators in between, assign them to a variable:
$var = 1 + 2;
echo 'hello ' . $var . '34';

Categories