PHP Calculate Math Function by Variable [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP: Calculate a math function f(x) in a string
please anyone help me to solve a math function,
for example i want to solve 3x+2y+4z
the value of the variable should be declared in php,
like $x=4, $y=2, $z=1
i know that there is PHP: Calculate a math function f(x) in a string ,
but is there any simple method ?

You can just create a simple equation:
$val = (3*$x)+(2*$y)+(4*$z)

Related

PHP return 1 number after dot in float [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 6 years ago.
I have got this php code
$Likes=1112;
$Likes=$Likes/1000;
echo $Likes."k";
This code returns me 1.112k,but my goal is to get 1.1k
Use the number_format function:
echo number_format($Likes,1)."k";
And a coding style advice: don't start your variables with an upper case letter!

How to do math with a string? [duplicate]

This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 7 years ago.
So lets show an example here shall we?
Let's say I have 2 strings that are like this....
$math1 = "+300";
$math2 = "-125";
So obviously the answer would have to be +175 but how do I actually do the math while the input is a string.
I can't simply do
$math1 - $math2
Because it'd have to figure out if it is a + or a -, so how can this exactly be done?
You can use intval to get the integer value of a variable, and then you can do basic math operations.
<?php
$math1 = intval("+300");
$math2 = intval("-125");
echo $math1 - $math2;
?>
intval($math1) + intval($math2);
Can be:
$result = abs($math1) - abs($math2);

PHP - Need to calculate the string formula to integer value [duplicate]

This question already has answers here:
How to evaluate formula passed as string in PHP?
(1 answer)
PHP: Calculate a math function f(x) in a string
(1 answer)
Closed 9 years ago.
I need to convert the formula like string to its calculated value, in PHP.
For eg:
$str = "(20000+(2000*(40/100)))*(10/100)";
$calculated_value = ????; // Calculated value of the above string "$str".
I need to calculate value of the string "$str" and store it in separate string.
Please help me out.
Thanks in advance.
Take a look on this: http://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html
And Process mathematical equations in php

PHP - How can I determine the numeric sequential value of a character in the english alphabet? [duplicate]

This question already has answers here:
How to convert some character into numeric in php?
(6 answers)
Closed 9 years ago.
Without using a loop, is there a way (function) to convert, for example, the letter "c" to its numeric sequence in the alphabet (3)?
I'm trying to take a literal string - $var = "c" and apply a function to it that returns 3.
Are there any built-in PHP functions that do this? I can't find any online and would rather avoid writing the function if necessary.
Anyone know of such a conversion function?
Yup.
$alphabet_position = ord(strtoupper($character)) - ord('A');

php function variable arguments [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pass variable number of arguments to a PHP function
I make my framework in php
I want to have variable function arguments
ex
if I have 2 parameter
$a,$b;
the function become
function name($a,$b);
and if I have 3 parameter
$a,$b,$c;
the function become
function name($a,$b,$c);
Check out http://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list. Is that what you're looking for? If not, can you just accept an array?

Categories