This question already has answers here:
Can I rely on PHP php.ini precision workaround for floating point issue
(5 answers)
Closed 3 years ago.
I am having a very different question. For some reason, when the json_encode function receives a variable that has the value assigned by a multiplication, the echo result will be a different value than expected. Example:
<?php
$test = 1.1 * 122;
echo json_encode(array("test" => $test)); // prints {"test":134.20000000000002}
echo $test; // prints 134.2
?>
For some reason, it doesn't work on every version of PHP, so I created a snippet on a tester that works:
Online Tester
Why does this happen?
Just use round function
$test = round(1.1 * 122, 2);
echo json_encode(array("test" => $test));
Related
This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
Closed 6 years ago.
Today i am working on a small PHP code to plus/minus and multiply, but i end up found something which i wasn't knew before.
I was Plus 2 value, 123456+123456=24690 and my code is this:
<?php
$value1=12345;
$value2=12345;
$output=$value1+$value2;
echo $output;
?>
Which return a correct value:
So than i have to print this value with string Like this:
Total Value: 24690
I use this code:
echo 'Total Value:'.$value1+$value2;
Its return: 12345 instead of
Total Value: 24690
I know that "." isn't a Arithmetic operators, so i need small explanation why is this doing that.
But when i do
echo 'Total Value:'.($value1+$value2);
Its work fine!
Little Confused!
It first concatenates the $value1 with the string and then tries to add it to a string and number can't be added to string and hence you get that output.
While using braces, it just concatenates the contents of the brace and performs the mathematical operation inside the brace.
It follows BODMAS
When you
echo 'Total Value:'.$value1+$value2;
code execution will be like this actually:
echo ('Total Value: ' . $value1) + $value2;
This behavior is called Type Juggling. From the manual it states:
a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.
Example:
$foo = "1"; // $foo is string (ASCII 49)
$foo *= 2; // $foo is now an integer (2)
More examples on the link.
This question already has answers here:
PHP - concatenate or directly insert variables in string
(15 answers)
Closed 6 years ago.
I have a question about dynamic variables. (I have trouble searching because I have a hard time describing my problem)
In this example:
$x = 1;
$var = "A$x";
echo $var; //prints 'A1'
Now my question is, is there a way to combine "computation" without adding another variable?
What I want to do is:
$x = 1;
$var = "A($x+1)";
echo $var; //I want to output to be 'A2' but it gives 'A(1+1)'
I know that this works:
$var = "A".($x+1)
But this is not applicable to the program that I am doing. $var is initiated on the beginning of the program and will be used at the end waiting for any value of $x.
You need to concatenate your output.
$x = 1;
$var = "A". ($x + 1);
echo $var;
In your example the "+1" is inside the quotes and thus is a literal string.
This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 8 years ago.
What I mean is can I do something like this...
$number = 1;
$varname = 'number';
Now I want to get the value of $number by using $varname so something like...
echo $($varname);
Output:
1
You need to use {} instead of ():
echo ${$varname};
Or even shorter:
echo $$varname;
Which equals:
echo ${'number'};
But as kingkero pointed out: You probably want to do something like ${'number'.$index} and that is easier solved with arrays.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP String in Array Only Returns First Character
I've got following problem. When I run script below I got string(1) "F" as an output. How is that possible? No error, notice displayed.. nothing. Key whatever doesn't exist in $c. Can you explain that?
<?php
$c = 'FEEDBACK_REGISTER_ACTIVATION_COMPLETED_MSG';
var_dump ($c['whatever']);
?>
I'm having this issue on PHP 5.3.3. (LINUX)
PHP lets you index on strings:
$str = "Hello, world!";
echo $str[0]; // H
echo $str[3]; // l
PHP also converts strings to integers implicitly, but when it fails, uses zero:
$str = "1";
echo $str + 1; // 2
$str = "invalid";
echo $str + 1; // 1
So what it's trying to do is index on the string, but the index is not an integer, so it tries to convert the string to an integer, yielding zero, and then it's accessing the first character of the string, which happens to be F.
Through Magic type casting of PHP when an associative array can not find the index, index itself is converted to int 0 and hence it is like
if
$sample = 'Sample';
$sample['anystring'] = $sample[0];
so if o/p is 'S';
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP: Calculate a math function f(x) in a string
I have a little problem. I have a string, such that,
$a = '(2+3+4)';
How can I handle $a such that the output, echo $a should be 9 instead of just giving (2+3+4).
If your code is indeed $a = (2+3+4), then echo will output 9.
It sounds like you have it a string. You could eval() that string to get the 9.
Have a look at php's Eval function
$string = "(2+3+4)";
eval('$a = '.$string);
echo $a;