Get up to 80 % extra points for free! More info:

Discussion – Lesson 6 - Conditions in PHP

Back

 

Comments
Avatar
Tabish Ali
Member
Avatar
Tabish Ali:2/7/2017 2:29

Here i modified this code , now result is displayed in same page and i used some bootstrap classes for good looking. :-D

<!DOCTYPE>
<html lang="en-US">
<head>

<meta charset="utf-8">
<title>Calcula­tor</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https:/­/maxcdn.bootstrap­cdn.com/bootstrap/3­.3.7/css/boot­strap.min.css">

<!-- jQuery library -->
<script src="https://­ajax.googleapis­.com/ajax/lib­s/jquery/3.1.1/jqu­ery.min.js"></scrip­t>

<!-- Latest compiled JavaScript -->
<script src="https://­maxcdn.bootstrap­cdn.com/bootstrap/3­.3.7/js/bootstrap­.min.js"></scrip­t> </head>
<!----------------------------------------------------------------------------------------------------------->

<body>
<div class="container">
<form method="post" action="<?php echo htmlspecialchar­s($_SERVER["PHP_SEL­F"]);?> " role="form" class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2">Number 1 :</label>
<div class="col-sm-4">
<input placeholder="Number-1" class="form-control" name="num1"></­input>
</div>
</div>

<div class="form-group">
<label class="control-label col-sm-2">Number 2 :</label>
<div class="col-sm-4">
<input placeholder="Number-2" class="form-control" name="num2"></­input>
</div>
</div>

<div class="form-group">
<label class="control-label col-sm-2">Operation :</label>

<div class="col-sm-2">

<select class="form-control" name="oper">
<option value="sum">Sum<­/option>
<option value="sub">Sub­traction</opti­on>
<option value="mul">Mul­tiplication</op­tion>
<option value="div">Di­vision</option>

</select>

</div>

</div>

<div class="form-group">
<div class="col-sm-2 col-sm-offset-2">

<button value="submit" name="submit" type="submit" class="btn btn-success">Compu­te</button>

</div>
</div>

</form>
</div>

<?php

$a = $b = $op = "";

if ($_SERVER["RE­QUEST_METHOD"] == "POST"){

$a = $_POST['num1'];
$b = $_POST['num2'];
$op = $_POST['oper'];

if ($op == 'sum')
$result = $a + $b;

else if ($op == 'sub')
$result = $a - $b;

else if ($op == 'div')
{
if ($b != 0){
$result = $a / $b;}
else
$result = 'Infinity';

}
else if ($op == 'mul')
$result = $a * $b;

echo '<div class="container" > <div class= "alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><st­rong>Answer: ' .
"$result </strong></div></di­v>";

}
?>

</body>
</html>

Reply
2/7/2017 2:29
there is no limit to learn.
Avatar
Justin Jake Telo:2/4/2018 22:41

how about use curly braces in the end of condition?

if ($a == 0){
echo "blah blah blag";
}
else if {
echo "blah blah nah";
}
else {
echo "nanana";
}

//something like this

Edited 2/4/2018 22:41
 
Reply
2/4/2018 22:41
Avatar
IT Man
Member
Avatar
Replies to Justin Jake Telo
IT Man:2/5/2018 1:40

You can if you want to. It won't change the meaning. :)

 
Reply
2/5/2018 1:40
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

3 messages from 3 displayed.