用php做計算
㈠ 用php做一個計算的程序,求思路
這個用php 感覺比較麻煩啊。
㈡ 用PHP做簡易計算器
結果出現在第一個頁面上 一般就不用php
一般用javascript做
jquery庫做的話更簡單。
㈢ 用php製作一個兩整數的加法運算網頁
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>四則運算</title>
</head>
<body>
<formaction="test.php"method="post">
<table>
<tr>
<td>第一個數</td>
<td><inputtype="text"name="first"></td>
</tr>
<tr>
<td>第二個數</td>
<td><inputtype="text"name="second"></td>
</tr>
<tr>
<td>運算符</td>
<td><selectname="operator">
<optionvalue="+">加</option>
<optionvalue="-">減</option>
<optionvalue="*">乘</option>
<optionvalue="/">除</option>
</select></td>
</tr>
<tr>
<tdcolspan="2"><inputtype="submit"value="開始計算"></td>
</tr>
</table>
</form>
<hr>
<?php
@$first=$_POST['first'];
@$second=$_POST['second'];
@$op=$_POST['operator'];
$res='';
switch($op){
case'+':
#code...
$res=$first+$second;
break;
case'-':
#code...
$res=$first-$second;
break;
case'*':
#code...
$res=$first*$second;
break;
case'/':
#code...
$res=$first/$second;
break;
}
echo"計算結果為:".$res;
?>
</body>
</html>
文件名:test.php
㈣ php 計算總價
要算單件商品的總價你把單件商品的數量先查出來再乘上商品的單價不就行了嗎!至於所有單件商品的總價,在你算出單件商品的總價的時候再定義一個變數把所有的單件商品的總價加起來賦值給他就行了吧。不太清楚你想問什麼,不知道對你有沒有用。
㈤ 用php做一個簡單的計算器,在線等!
你這種寫法用get吧,form屬性method也用get,回顯用value="<?php echo $x;?>"。這么樣會跳轉,用ajax會好一點。
㈥ 使用PHP編程。一個網頁計算器,實現簡單四則運算
把下面內容存成PHP格式,就OK了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>四則運算</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<input name="y" type="text" id="y" size="10" />
<select name="y0" id="y0">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input name="y1" type="text" id="y1" size="10" />
<input type="submit" name="Submit" value="計算" />
</p>
<p> </p>
<p> </p>
</form>
<?php
function make_safe($variable) {
$variable = addslashes(trim($variable));
return $variable;
}
$y0=make_safe($_REQUEST["y0"]);
$y1=make_safe($_REQUEST["y1"]);
$y=make_safe($_REQUEST["y"]);
if ($y0 != "")
{
switch($y0)
{
case "+":
$chaxuntj =$y+$y1;
break;
case "-":
$chaxuntj =$y-$y1;
break;
case "*":
$chaxuntj =$y*$y1;
break;
case "/":
$chaxuntj =$y/$y1;
break;
default:
}
echo $y.$y0.$y1."=".$chaxuntj;
}
?>
</body>
</html>
㈦ 一個簡單的PHP運算
簡單的說「遞歸來函數」就是在函數內自部又調用該函數本身,如這個例子:在recurser的裡面又調用了recurser函數。
只要遇到 recurser($n) 的時候就把 $n 帶到遞歸函數中計算,用計算的結果(可能是具體的值,也可能是表達式)替換當前函數直到全部替換為具體的值後,結果也就出來了。
例如你這個例子(函數名用X代替),一步步替換的結果就是:
5 * X(5 - 1)
=> 5 * 4 * X(4 - 1)
=> 5 * 4 * 3 * X(3 - 1)
=> 5 * 4 * 3 * 2 * X(2 - 1)
=> 5 * 4 * 3 * 2 * 1 * X(1 - 1)
=> 5 * 4 * 3 * 2 * 1 * 1
㈧ 用php做個計算器(加減乘除),兩個文本框輸入數字,第三個輸出結果並
不需要php呀
這樣寫的行不
<!DOCTYPEhtml>
<html>
<head>
<title>簡單計算器</title>
</head>
<body>
<inputtype="text"name="first"id="first">
<selectid="operate">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<inputtype="text"name="second"id="second">=
<inputtype="text"name="result"id="result">
<inputtype="button"name="運算"value="運算"onClick="operate()">
<scripttype="text/javascript">
functionoperate(){
varfirst=parseInt(document.getElementById("first").value);
varsecond=parseInt(document.getElementById("second").value);
varresult=document.getElementById("result");
varopt=document.getElementById("operate");
if(0==opt.selectedIndex){
resultvalue=first+second;
}elseif(1==opt.selectedIndex){
resultvalue=first-second;
}elseif(2==opt.selectedIndex){
resultvalue=first*second;
}elseif(3==opt.selectedIndex){
if(second==0){
alert("除數不能為0");
}
resultvalue=first/second;
}
result.setAttribute("value",resultvalue);
}
</script>
</body>
</html>
㈨ 用PHP編寫代碼,計算1到100的累加值
一個經典的小學問題也是一個簡單的PHP小應用,1+2+3……100=多少?使用PHP應該怎麼寫?
這里總結了以下幾種思路:
1.普通PHPer:
$sum=0;
for($i=1;$i<=100;$i++) {
$sum+=$i;
}
echo $sum;
2.文藝PHPer:
$sum= $i = 0;
while( $i<= 100 ) {
$sum += ( $i++ );
}
echo $sum;
3.遞歸帝:
function get_sum($n){
return $n==1?1:$n+get_sum($n-1);
}
echo get_sum(100)
4.神:
echo array_sum(range(1,100));
5.二逼青年歡樂多:
$n = 100;
echo (1+$n)*($n/2);
㈩ 如何用PHP做的計算器嗎
<?php/**
* Created by PhpStorm.
* User: ITAK
* Date: 2017/3/3
* Time: 10:28
*/
error_reporting(E_ALL & ~E_NOTICE); if(isset($_POST['submit'])){ $ok = true; $error = "出現的問題:<br>"; if($_POST['num1'] == ""){ $ok = false; $error = $error."第一個數字不能為空<br>";
} else{ if(!is_numeric($_POST['num1'])){ $ok = false; $error = $error."第一個數字不是數字<br>";
}
} if($_POST['num2'] == ""){ $ok = false; $error = $error."第二個數字不能為空<br>";
} else{ if(!is_numeric($_POST['num2'])){ $ok = false; $error = $error."第二個數字不是數字<br>";
}
}
} if($ok){ $sum = ""; $fuhao = $_POST['fuhao']; if($fuhao == '+') $sum = $_POST['num1'] + $_POST['num2']; if($fuhao == '-') $sum = $_POST['num1'] - $_POST['num2']; if($fuhao == '*') $sum = $_POST['num1'] * $_POST['num2']; if($fuhao == '/') $sum = $_POST['num1'] / $_POST['num2']; if($fuhao == '%') $sum = $_POST['num1'] % $_POST['num2'];
} echo "<br>";?><html>
<head>
<meta charset="UTF-8">
<title>簡單計算器</title>
</head>
<body>
<table border="0" width="400" align="center">
<form action="cal.php" method="post">
<caption><h1>簡單計算器</h1></caption>
<tr>
<td>
<input type="text" size="5" name="num1" value="<?php echo $_POST['num1'] ?>"/>
</td>
<td>
<select name="fuhao">//下拉列表 <option <?php if($_POST['fuhao']=="+") echo "selected"?>
value="+"> + </option>
<option <?php if($_POST['fuhao']=="-") echo "selected"?>
value="-"> - </option>
<option <?php if($_POST['fuhao']=="*") echo "selected"?>
value="*"> * </option>
<option <?php if($_POST['fuhao']=="/") echo "selected"?>
value="/"> / </option>
<option <?php if($_POST['fuhao']=="%") echo "selected"?>
value="%"> % </option>
</select>
</td>
<td>
<input type="text" name="num2" size="5" value="<?php echo $_POST['num2'] ?>"/>
</td>
<td>
= </td>
<td>
<input type="text" name="res" size="5" value="<?php echo $sum ?>"/>
</td>
</tr>
<tr align="center">
<td>
<input type="submit" value="計算" name="submit">
</td>
</tr>
<br>
<tr>
<td colspan="4">
<?php
if($ok){ echo "結果為: {$_POST['num1']} {$_POST['fuhao']} {$_POST['num2']} = {$sum}";} else{ echo $error;} ?>
</td>
</tr>
</form>
</table>
</body></html>