我的php正在打印数字而不是结果。例如,它将打印出“3 + 3”而不是“6”。
我做错了什么?有人可以解释php如何读取代码以及我如何解决它。 - 新的初学者学习php
<?php
//Check to see if the submit button got posted
if(isset($_POST['submit'])){
//If it did, get the values from the textbox
$first_number = $_POST['first_number'];
$second_number= $_POST['second_number'];
$operator = $_POST['operator'];
//Check to see if they are empty
if(!empty($first_number) && !empty($second_number)){
echo $first_number . $operator . $second_number;
}
else {
echo 'Please fill out all the forms';
}
}
?>
</head>
<body>
<div id='wrap'>
<header id='header'>
<h1> This Is A Simple Math Calculator </h1>
</header>
<section id='main'>
<form action='math.php' method='POST'>
<table cellspacing='10px'>
<tr>
<td>
<input type='text' name='first_number' placeholder='First Number' size='15px'/>
</td>
<td>
<select name='operator'>
<option value='+'>+</option>
<option value='-'>-</option>
<option value='*'>*</option>
<option value='/'>/</option>
<option value='%'>%</option>
</select>
</td>
<td>
<input type='text' name='second_number' placeholder='Second Number' size='15px' />
</td>
<td>
<input type='submit' name='submit' value='=' />
</td>
<td>
<input type='text' name='answer' placeholder='' size='10px' />
</td>
</tr>
</table>
</form>
</section>
<footer>
<p> MADE BY @KD </p>
</footer>
答案 0 :(得分:0)
由于您是初学者,您可以像这样构建它
if($operator == "+"){
echo $first_number + $second_number;
}
if($operator == "-"){
echo $first_number - $second_number;
}
// same for other operators you permit.
答案 1 :(得分:-1)
更改
echo $first_number . $operator . $second_number;
到
echo eval("echo $first_number$operator$second_number ;");
答案 2 :(得分:-2)
echo $result = eval('return '.$first_number . $operator . $second_number.';');