I'm working in an invoice system, that has to calculate different formulas for every product, which will need to be filled with Width, Height, and Material cost.
I did a database where i'm storing
t_title -> "Curtain 1"
t_formula -> "({ANCHO}*{ALTURA})*{PRECIO}"
and then php does this:
<?php
$ancho = str_replace("{ANCHO}", $_POST['ancho'], $articulo['t_formula']);
$alto = str_replace("{ALTURA}", $_POST['alto'], $ancho);
$precio = str_replace("{PRECIO}", $_POST['precio'], $alto);
$total = $precio; echo eval($total);
?>
and the result is not giving anything, but a blank space.
How can i make it to work? I know that php can't calculate from variables as php but, i can't find another way to do it.
The eval() function expects the string parameter to be a proper code statement.
$str = '(10*10) * 1000';
echo eval($str);
will give you an error, because php cannot evaluate that string. However,
$str = 'return (10*10) * 1000;';
echo eval($str);
will evaluate the expression correctly.
You should use:
$total = 'return ' . $precio . ';';
echo eval($total);
Your using eval is wrong. The string passed to eval must be valid PHP code. i.e:
$precio2 = '$total = '.$precio.';';
eval($precio2);
echo $total;
http://php.net/manual/en/function.eval.php
Related
I am performing subtraction on two variable.
$first_variable = 20;
$second_variable = 30;
$result = $first_variable - $second_variable;
So how do i get that the result $result is positive or negative?
Have any PHP function to determine that the result of subtraction is positive or negative?
I know the i can use if statement to get it done. but i am asking for any predefined function to do it.
The reason i asked it hear is just a curiosity.
You can use php gmp_sign function to achieve that check this
Example:-
<?php
// positive
echo gmp_sign("500") . "\n";
// negative
echo gmp_sign("-500") . "\n";
// zero
echo gmp_sign("0") . "\n";
?>
output
1
-1
0
Well, for pure academic purposes, you can sort of do it with a function:
$sign = sprintf("%+d", $number)[0];
I want to send some string parameter to a cpp.exe from PHP thanks to exec function. The aim of the exe is to compute a rank of documents according to a query.
Here is the code in php :
$query = "is strong";
$file = "vsm.exe $query";
exec($file,$out);
echo $out[0];`
I received this output for echo $out[0];
Notice: Undefined offset: 0 in C:\xampp\htdocs\analysis.php on line 25
But, my vsm.exe only work (meaning I receive my ranks in the $out variable as a string which is okay) when the query is without space:
$query = "is";
$file = "vsm.exe $query";
exec($file,$out);
echo $out[0];
I followed that example which works with integer parameter (this is not what I want, I want to send sentences):
$a = 2;
$b = 5;
exec("tryphp.exe $a $b",$c_output);
echo $c_output[0];
$c_array0 = explode(" ",$c_output[0]);
echo "Output: " . ($c_array0[0] + 1);
echo "Output: " . ($c_output[0] + 1);
How could I send strings including spaces (could be long text) as parameters to c++?
Thanks in advance.
I actually find something but still not enough good:
$query = "is strong";
$file = 'vsm.exe "is strong"';
exec($file,$out);
echo $out[0];
It returns me the rank I wanted. However, I look for a way to use $query as a parameter, not directly the string "is strong".
As an example I have code similar to this:
$r1 = 7.39999999999999;
$r2 = 10000;
echo bcmul($r1,$r2,100);
//returns 74000.0
echo ($r1*$r2);
//returns 74000.0
I am wanting it to return 73999.9999999999 rather than rounding it off.
Is there a formula or function to do this?
The doc http://php.net/manual/de/function.bcmul.php says:
left_operand: The left operand, as a string.
right_operand: The right operand, as a string.
So use strings instead:
$r1 = "7.39999999999999";
$r2 = "10000";
echo bcmul($r1,$r2,100);
works.
Or if you have these varibales from somewhere cast them (via (string) ) to string. Maybe at this step you could encounter some roundings already...
I'm not a php person, but a quick Google suggests you may want the
$number_format()
function and specify the $decimals parameter. See this link
I have a question about eval() secourity risks
This is my own code
<?php
$str = 'nabi<'.$_GET['hackme']; // $_GET['hackme']=2;
$str = str_replace("nabi", 1, $str);
$hmm = eval('return ('.$str.');');
if($hmm){
echo 'yeah';
}
else{
echo 'no';
}
Result is will be:
yeah
My code workes well
It's what i want!
But i am afraid of the security risks!
Please offer a new solution
If all you're doing is checking if something is less than 1, typecast $_GET['hackme'] to int or double.
$str = 'nabi<' . (int) $_GET['hackme'];
There is zero security... Every code passed to hackme will be executed.
I'm new to PHP.
My code reads a price value from a Steam game's json data.
http://store.steampowered.com/api/appdetails/?appids=8870
Problem is that the value of the price node is not formatted with a comma separator for dollars and cents. My code works to piece together the dollars and cents but is it the right way to do it for this instance. Also if there is another easier method of doing my newbie code, feel free to show me where it can be improved. Thanks!
<?php
$appid = '8870';
$ht = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($ht);
$jd = json_decode($fgc, true);
$gdata = $jd[$appid]['data'];
$gname = $gdata['name'];
$gprice = $gdata['price_overview']['final'];
$gdesc = $gdata['detailed_description'];
$gusd = substr($gprice, 0, -2);
$gcent = substr($gprice, 2);
echo $gname. '<br>';
echo 'Price: $' .$gusd. ',' .$gcent;
?>
If I may ask another question... can the price data aka $gprice be added to another price data that is fetched, to return a total.
I would essentially do what you are doing except its much simpler to just divide by 100:
Turn the price into a float:
$gprice = $gprice / 100;
and then use money_format
Ref: PHP Docs - money_format
You could also do this, but there isn't really a need.
$gprice = (int) $gdata['price_overview']['final'];
The conversion is not bad, but you could also use this:
$gusd = $gprice/100;
echo $gname. '<br>';
echo 'Price: $' .str_replace('.', ',', $gusd);
or use money_format instead of replace, but it's a little more complicated.
also, to add another you could do it with just the + or += operators like this:
$gprice+= $gdata['price_overview']['final'];