初めてのPHPプログラムソースです。
お父さんに少しアドバイスもらいならが、Googleで調べながらやってみました^^
エディタはAtomを利用して、FFFTPを使ってサーバーにファイルをアップしました。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
//初期化
$var1 = "";
$var2 = "";
$n_var1 = "";
$n_var2 = "";
$mode = "0";
$sign = "";
$result = "";
//過去のデータを取りだす
if ($_POST['p_mode'] == 1) {
$mode = 1;
}
if ($mode == 1) {
$var1 = $_POST['p_var1'];
$sign = $_POST['p_sign'];
}
//ボタンごとのプログラム
//%
if (isset($_POST['%'])) {
if ($mode == 0) {
$var1 = $_POST['p_var1'];
$var1 = $var1 / 100;
} elseif (mode == 1) {
$var2 = $_POST['p_var2'];
$var2 = $var2 / 100;
}
}
//CE
if (isset($_POST['CE'])) {
if ($mode == 0) {
$var1 = "";
} elseif ($mode == 1) {
$var2 = "";
}
}
//C
if (isset($_POST['C'])) {
$var1 = "";
$var2 = "";
$n_var1 = "";
$n_var2 = "";
$mode = "0";
$sign = "";
$result = "";
}
//÷
if (isset($_POST['wa'])) {
if ($mode == 0) {
$mode = 1;
$var1 = $_POST['p_var1'];
$sign = '÷';
} else {
$message = 1;
}
}
//×
if (isset($_POST['ka'])) {
if ($mode == 0) {
$mode = 1;
$var1 = $_POST['p_var1'];
$sign = '×';
} else {
$message = 1;
}
}
//-
if (isset($_POST['ma'])) {
if ($mode == 0) {
$mode = 1;
$var1 = $_POST['p_var1'];
$sign = '-';
} else {
$message = 1;
}
}
//+
if (isset($_POST['ta'])) {
if ($mode == 0) {
$mode = 1;
$var1 = $_POST['p_var1'];
$sign = '+';
} else {
$message = 1;
}
}
//=
if (isset($_POST['ha'])) {
$var2 = $_POST['p_var2'];
$n_var1 = (float) $var1;
$n_var2 = (float) $var2;
if ($sign == '÷') {
$result = $n_var1 / $n_var2;
}
if ($sign == '×') {
$result = $n_var1 * $n_var2;
}
if ($sign == '-') {
$result = $n_var1 - $n_var2;
}
if ($sign == '+') {
$result = $n_var1 + $n_var2;
}
}
//.
if (isset($_POST['dot'])) {
if ($mode == 0) {
$_POST['p_var1'] .= '.';
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= '.';
$var2 = $_POST['p_var2'];
}
}
//-/+
if (isset($_POST['or'])) {
if ($mode == 0) {
$_POST['p_var1'] = -$_POST['p_var1'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] = -$_POST['p_var2'];
$var2 = $_POST['p_var2'];
}
}
//0
if (isset($_POST['0'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['0'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['0'];
$var2 = $_POST['p_var2'];
}
}
//1
if (isset($_POST['1'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['1'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['1'];
$var2 = $_POST['p_var2'];
}
}
//2
if (isset($_POST['2'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['2'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['2'];
$var2 = $_POST['p_var2'];
}
}
//3
if (isset($_POST['3'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['3'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['3'];
$var2 = $_POST['p_var2'];
}
}
//4
if (isset($_POST['4'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['4'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['4'];
$var2 = $_POST['p_var2'];
}
}
//5
if (isset($_POST['5'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['5'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['5'];
$var2 = $_POST['p_var2'];
}
}
//6
if (isset($_POST['6'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['6'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['6'];
$var2 = $_POST['p_var2'];
}
}
//7
if (isset($_POST['7'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['7'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['7'];
$var2 = $_POST['p_var2'];
}
}
//8
if (isset($_POST['8'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['8'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['8'];
$var2 = $_POST['p_var2'];
}
}
//9
if (isset($_POST['9'])) {
if ($mode == 0) {
$_POST['p_var1'] .= $_POST['9'];
$var1 = $_POST['p_var1'];
} elseif ($mode == 1) {
$_POST['p_var2'] .= $_POST['9'];
$var2 = $_POST['p_var2'];
}
}
?>
<!--表示-->
<div class = box2>
<?php
if ($mode == 1) {
if (isset($_POST['ha'])) {
echo $var1 . $sign . $var2;
} else {
echo $var1 . $sign;
}
}
?>
</div>
<div class ="box1">
<?php
if ($mode == 0) {
echo $var1;
}
if ($mode == 1) {
if (isset($_POST['ha'])) {
echo $result;
$var1 = "$result";
$var2 = "";
$n_var1 = "";
$n_var2 = "";
$mode = "0";
$sign = "";
$result = "";
} else {
echo $var2;
}
}
?>
</div>
<!--ボタンの表示とプログラム-->
<form action="calc.php" method="post">
<button class="button" type="submit" name="%" value="%">%</button>
<button class="button" type="submit" name="CE" value="CE">CE</button>
<button class="button" type="submit" name="C" value="C">C</button>
<button class="button" type="submit" name="wa" value="wa">÷</button>
<br>
<button class="button" type="submit" name="7" value="7">7</button>
<button class="button" type="submit" name="8" value="8">8</button>
<button class="button" type="submit" name="9" value="9">9</button>
<button class="button" type="submit" name="ka" value="ka">×</button>
<br>
<button class="button" type="submit" name="4" value="4">4</button>
<button class="button" type="submit" name="5" value="5">5</button>
<button class="button" type="submit" name="6" value="6">6</button>
<button class="button" type="submit" name="ma" value="ma">-</button>
<br>
<button class="button" type="submit" name="1" value="1">1</button>
<button class="button" type="submit" name="2" value="2">2</button>
<button class="button" type="submit" name="3" value="3">3</button>
<button class="button" type="submit" name="ta" value="ta">+</button>
<br>
<button class="button" type="submit" name="or" value="or">-/+</button>
<button class="button" type="submit" name="0" value="0">0</button>
<button class="button" type="submit" name="dot" value="dot">.</button>
<button class="button" type="submit" name="ha" value="ha">=</button>
<br>
<!--過去のデータを送信-->
<input type="hidden" name="p_var1" value="<?php echo $var1; ?>">
<input type="hidden" name="p_var2" value="<?php echo $var2; ?>">
<input type="hidden" name="p_sign" value="<?php echo $sign; ?>">
<input type="hidden" name="p_mode" value="<?php echo $mode; ?>">
</form>
<?php
//定義済み変数を表示
echo "<br>";
if ($message == 1) {
echo "同時計算はできません";
$message = 0;
}
?>
<?php
/*
//モード表示
var_dump(get_defined_vars());
echo "var1=".$var1."<br>";
echo "var2=".$var2."<br>";
echo "n_var1=".$n_var1."<br>";
echo "n_var2=".$n_var2."<br>";
echo "mode=".$mode."<br>";
echo "sign=".$sign."<br>";
echo "result=".$result."<br>";
if (isset($_POST['ha'])) {
echo $var1 . $sign . $var2;
;
}
*/
?>
</body>
</html>
リンク:php電卓
師匠のプログラムソースはこれでした。。。
全然違う・・・・ソースも短い・・・師匠流石です。
<?php
/**
* @param int $var1
* @param int $var2
* @param varchar $sign
* @return int
*/
function calc($var1, $var2, $sign){
if(is_numeric($var1) && is_numeric($var2)){
switch($sign){
case "+":
$ret = $var1 + $var2;
break;
case "-":
$ret = $var1 - $var2;
break;
case "*":
$ret = $var1 * $var2;
break;
case "/":
$ret = $var1 / $var2;
break;
}
}else{
$ret = "N/A";
}
return $ret;
}
//上部表示
if(empty($_POST["p_disp"])){
$disp = "";
}else{
$disp = $_POST["p_disp"];
}
//一つ前の上部情報
//数値の場合は前の計算式を変更しない
if(isset($_POST["num"]) && !is_null($_POST["num"])){
$before_disp = $_POST["p_before_disp"];
}else{
$before_disp = $_POST["p_disp"];
}
//一つ目の値
if(isset($_POST["num"]) && !is_null($_POST["num"]) && empty($_POST["p_sign"])){
$var1 = $_POST["p_var1"].$_POST["num"];
$disp .= $_POST["num"];
}else{
$var1 = $_POST["p_var1"];
}
//二つ目の値
if(isset($_POST["num"]) && !is_null($_POST["num"]) && !empty($_POST["p_sign"])){
$var2 = $_POST["p_var2"].$_POST["num"];
$disp .= $_POST["num"];
}else{
$var2 = $_POST["p_var2"];
}
//計算
//「=」若しくは「var2」に数値が入り符号が送信された場合
if((isset($_POST["ha"]) && !empty($_POST["ha"])) || (isset($_POST["sign"]) && !empty($_POST["sign"]) && !empty($_POST["p_var2"]))){
$var1 = calc($_POST["p_var1"], $_POST["p_var2"], $_POST["p_sign"]);
$var2 = "";
$sign = "";
}
//符号
if(isset($_POST["sign"]) && !empty($_POST["sign"])){
$sign = $_POST["sign"];
$disp .= $_POST["sign"];
}else if(!empty($_POST["p_sign"])){
$sign = $_POST["p_sign"];
}
//%
if(isset($_POST["%"]) && !empty($_POST["%"]) && empty($_POST["p_var2"])){
$var1 = $var1 / 100;
$disp .= $_POST["%"];
}else if(isset($_POST["%"]) && !empty($_POST["%"])){
$var2 = $var2 / 100;
$disp .= $_POST["%"];
}
//-/+
if(isset($_POST["or"]) && !empty($_POST["or"]) && empty($_POST["p_var2"])){
$var1 = -$var1;
$disp .= $_POST["or"];
}else if(isset($_POST["or"]) && !empty($_POST["or"])){
$var2 = -$var2;
$disp .= $_POST["or"];
}
//CE
if(isset($_POST["CE"]) && !empty($_POST["CE"])){
$var1 = "";
$var2 = "";
$sign = "";
$disp = "";
$before_disp = "";
}
//C
if(isset($_POST["C"]) && !empty($_POST["C"])){
$var2 = "";
$disp = $_POST["p_before_disp"];
}
//メイン表示
if(!empty($var2)){
$main_disp = $var2;
}else{
$main_disp = $var1;
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!--表示-->
<div class = box2>
<?php echo $disp; ?>
</div>
<div class ="box1">
<?php echo $main_disp; ?>
</div>
<!--ボタンの表示とプログラム-->
<form action="calc_hayashi.php" method="post">
<button class="button" type="submit" name="%" value="%">%</button>
<button class="button" type="submit" name="CE" value="CE">CE</button>
<button class="button" type="submit" name="C" value="C">C</button>
<button class="button" type="submit" name="sign" value="/">÷</button>
<br>
<button class="button" type="submit" name="num" value="7">7</button>
<button class="button" type="submit" name="num" value="8">8</button>
<button class="button" type="submit" name="num" value="9">9</button>
<button class="button" type="submit" name="sign" value="*">×</button>
<br>
<button class="button" type="submit" name="num" value="4">4</button>
<button class="button" type="submit" name="num" value="5">5</button>
<button class="button" type="submit" name="num" value="6">6</button>
<button class="button" type="submit" name="sign" value="-">-</button>
<br>
<button class="button" type="submit" name="num" value="1">1</button>
<button class="button" type="submit" name="num" value="2">2</button>
<button class="button" type="submit" name="num" value="3">3</button>
<button class="button" type="submit" name="sign" value="+">+</button>
<br>
<button class="button" type="submit" name="or" value="or">-/+</button>
<button class="button" type="submit" name="num" value="0">0</button>
<button class="button" type="submit" name="num" value=".">.</button>
<button class="button" type="submit" name="ha" value="ha">=</button>
<br>
<!--過去のデータを送信-->
<input type="hidden" name="p_var1" value="<?php echo $var1; ?>">
<input type="hidden" name="p_var2" value="<?php echo $var2; ?>">
<input type="hidden" name="p_sign" value="<?php echo $sign; ?>">
<input type="hidden" name="p_disp" value="<?php echo $disp; ?>">
<input type="hidden" name="p_before_disp" value="<?php echo $before_disp; ?>">
</form>
</body>
</html>