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;
Related
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));
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:
Convert a string to a double - is this possible?
(4 answers)
Closed 7 years ago.
I have a string value $a=10. I have to convert to double value before storing. how to convert string to double in php.
I tried adding 0.00 to the $a. i.e $a= $a+0.00; But it doesn't work
Just use this
$a = 10;
echo number_format($a, 2);
You can try doubleval() which is an alias of the floatval function that get float value of a variable.
<?php
$a = doubleval($a);
You can quick-cast the variable or the operation (or both)
$a = (float) $a + 0.00
$a = (float) $a
You can use PHP's floatval.
http://php.net/manual/en/function.floatval.php
<?php
$a=10;
$floatVal = floatval($a);
?>
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:
How do I create variable variables?
(17 answers)
Closed 9 years ago.
In PHP like below
<?php
$a = 'b'
$b = 'c'
echo $$a
?>
Output: c
Is there any similar kind of implementation in Python like $$..?
no .... not really you can do stuff like
a = 'b'
b = 'c'
globals().getattr(a,None)
or even better use a dict (you should really do this!!!)
env = {'a':5,'b':'a'}
env[env['b']]
but there is nothing like $$a