我是PHP的初学者并创建了一个计算器,我将计算作为变量(q)发布到php文件中,并希望进行计算并将其作为结果回显给框。
通过php函数库我无法找到一个可以帮助我的功能,例如' eval'在JavaScript中。
我没有使用' eval'是计算必须在服务器端进行。
非常感谢任何帮助。
JAVASCRIPT
$('.equal').click(function(){
$.post('calculator.php',{q: $('.result').val()} ,function(a){
$('.result').val(a).show();
});
});
PHP
<?php
$_POST[q];
?>
HTML
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>calculator</title>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css'>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container col-sm-2 text-center" id="calc">
<div class="row">
<form name="calc">
<input type="text" class="screen text-right result" name="result" placeholder="0" readonly>
</form>
</div>
<!-- First row -->
<div class = "row">
<button class="btn btn-primary number" data-key="1" type="button">1</button>
<button class="btn btn-primary number" data-key="2" type="button">2</button>
<button class="btn btn-primary number" data-key="3" type="button">3</button>
<button class="btn btn-danger" data-key="reset" type="button">C</button>
</div>
<!-- Second row -->
<div class = "row">
<button class="btn btn-primary number" data-key="4" type="button">4</button>
<button class="btn btn-primary number" data-key="5" type="button">5</button>
<button class="btn btn-primary number" data-key="6" type="button">6</button>
<button class="btn btn-warning number" data-key="+" type="button">+</button>
</div>
<!-- Third row -->
<div class = "row">
<button class="btn btn-primary number" data-key="7" type="button">7</button>
<button class="btn btn-primary number" data-key="8" type="button">8</button>
<button class="btn btn-primary number" data-key="9" type="button">9</button>
<button class="btn btn-warning number" data-key="-" type="button">-</button>
</div>
<!-- Fourth row-->
<div class = "row">
<button class="btn btn-primary number" data-key="0" type="button">0</button>
<button class="btn btn-primary number" data-key="." type="button">.</button>
<button class="btn btn-warning number" data-key="/" type="button">/</button>
<button class="btn btn-warning number" data-key="*" type="button">*</button>
</div>
<div class="row">
<button class="btn btn-success btn-lg equal" type="button">=</button>
</div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script>
<script src="js/calculator.js"></script>
<!-- jquery fade in function -->
<script>
$( "#calc" ).hide( 500 ).delay( 1500 ).show( 300 );
</script>
</body>
</html>
答案 0 :(得分:0)
您可以在此处找到相关PHP功能列表:
http://www.w3schools.com/php/php_ref_math.asp
回调函数可能会有所帮助,这里有一些文档:
http://php.net/manual/en/language.types.callable.php
回调函数的一个示例:
N = numel(A);
r = randperm(N,3);
A = char(A);
A(r,[1,2]) = A(r,[2,1]);
A = cellstr(A);
使用中:
<?php
// Just an example, two numbers passed with GET
// $calc_choice will be add, subtract, etc.
$num_one = $_GET['number_one']);
$num_two = $_GET['number_two']);
$calc_choice = $_GET['calculation'];
// passing these variables into a callback function
customEval($num_one, $num_two, $calc_choice);
// This is abstraction
// $callback is going to be a function we pass
// You can name $numOne and $numTwo anything you would like
function customEval($numOne, $numTwo, $callback){
$answer = $callback($numOne, $numTwo);
echo '$answer';
}
//This will add two numbers together and return them
//What gets returns is what will be echoed in our callback function
function add($numOne, $numTwo){
return $numOne + $numTwo;
}
// You can make as many as you like
function subtract($numOne, $numTwo){
return $numOne - $numTwo;
}
?>