dongluanban3536 2017-01-09 01:11
浏览 41

PHP变量计算

I am quite a beginner in PHP and have created a calculator and I am posting the calculation as a variable(q) to a php file and want to make the calculation and echo it back to the box as the result.

Going through the php function library I could not locate a function the can help me with that like 'eval' in JavaScript.

The reason I am not using 'eval' is that the calculation must happen on the server side.

Any help will be greatly appreciated.

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>
  • 写回答

1条回答 默认 最新

  • douwei1128 2017-01-09 01:44
    关注

    You can find a list of related PHP functions here:
    http://www.w3schools.com/php/php_ref_math.asp

    Callback functions might be helpful, here is some documentation:
    http://php.net/manual/en/language.types.callable.php

    An example of a callback function:

    <?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;
    }
    
    ?>
    

    In use:

    $num_one = 5;
    $num_two = 10;
    $calc_choice = "add";
    
    // our function would be doing this behind the scene
    customEval(5, 10, "add"){
        // Because our add function returns 15 in this case
        echo '15'
    }  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题