Sie sind auf Seite 1von 15

Lab Work : Tutorial 1

Exercise 1
Coding:
Calculator.php
<html>
<head>
<title>Calculator</title>
</head>
<body>
<form action="calc_result.php" method="post">
<fieldset><legend>Calculator:</legend>
<p><b>First Number:</b> <input type="text" name="f1" size="20" maxlength="40"
/></p>
<p><b>Second Number:</b> <input type="text" name="f2" size="20" maxlength="40"
/></p>
</fieldset>
<div align="left"><input type="submit" name="submit" value="Calculate" /></div>
</form>
</body>
</html>
calc_result.php
<html>
<head>
<title>Calculator Result</title>
</head>
<body>
<?php
// Validate quantity
if (!empty($_POST['f1'])) {
$f1 = $_POST['f1'];
} else {
$f1 = NULL;
echo '<p><b>You forgot to enter first number!</b></p>';
}
// Validate price
if (!empty($_POST['f2'])) {
$f2 = $_POST['f2'];
} else {
$f2 = NULL;
echo '<p><b>You forgot to enter second number!</b></p>';
}

// If everything is okay, print the message.


if ($f1 && $f2) {
// Calculate the total.
$add = $f1 + $f2;
$sub = $f1 - $f2;
$mul = $f1 * $f2;
$div = $f1 / $f2;
$add = number_format ($add, 2);
$sub = number_format ($sub, 2);
$mul = number_format ($mul, 2);
$div = number_format ($div, 2);
// Print the results.

echo 'The total addition of two numbers are '.$add.'<br />The total subtraction of
two numbers are '.$sub.'<br />The total multiplication of two numbers are '.
$mul.'<br />The total division of two numbers are '.$div;
} else { // One form element was not filled out properly.
echo '<p><font color="red">Please go back and fill out the form again.</font></p>';
}
?>
</body>
</html>

Print Screen:

Exercise 2

Coding:
<html>
<head>
<title>Sugar Converter</title>
</head>
<body>
<form action="" method="post">
<select name="direction">
<option value="ptkg" selected>Pound to Kilogram </option>
<option value="ptg">Pound to Gram </option>
</select>
<input type="text" name="weight" size="6"/> <br/>
<input type="Submit" value="Convert"/>
</form>
</body>
</html>
<?php
if (isset($_POST['direction']) && !empty($_POST['weight']))
{
$direction = $_POST['direction'];
$weight = $_POST['weight'];
if ( ! is_numeric($weight))
{
$error[] = "Input is not a valid number.";
}
if ( ! isset($error))
{
if ($direction == "ptkg")
{
$weight_new = convert_weight($weight);
echo "$weight pound coverted to kilogram is <b>$weight_new</b>
Kilogram " ;
}
else
{
$weight_new = convert_weight($weight,true);
echo "$weight pound convert to is <b>$weight_new</b> Gram " ;
}
}
else
{
foreach ($error as $err)
{
echo $err . '<br />';
}
}
}
function convert_weight($weight, $pound=false)
{
if ($weight)
{
$ret = $weight * 0.45;
}

else
{
$ret = $weight * 453.6 ;
}
return round($ret,1);
}
?>

Print Screen:

Exercise 3
Coding:
<html>
<head>
<title>Dinasour Age</title>
</head>
<body>
<form action="" method="post">
<fieldset><legend>Enter the dinasour age:</legend>
<p><b>Age:</b> <input type="text" name="age" /></p>
</fieldset>
<div align="left"><input type="submit" name="submit" value="Calculate!" /></div>
</form>
</body>
</html>
<?php
$a = $_POST['age'];
$day = $a * 365;
$week = $day / 7;
$month = $day / 30;
$day = number_format ($day, 1);
$week = number_format ($week, 1);
$month = number_format ($month, 1);

echo 'Age of dinasour in <br />day: '.$day.'<br />week: '.$week.'<br />month: '.
$month;
?>

Print Screen:

Exercise 4
Coding:
<html>
<head>
<title>Circle</title>
</head>
<body>
<form action="" method="post">
<fieldset><legend>Circle:</legend>
<p><b>Radius:</b> <input type="text" name="radius" /></p>
</fieldset>
<div align="left"><input type="submit" name="submit" value="Calculate!" /></div>
</form>
</body>
</html>
<?php
$a = $_POST['radius'];

$perimeter = $a * 2 * 3.142;
$area = pow($a,2) * 3.142;
$perimeter = number_format ($perimeter, 2);
$area = number_format($area, 2);
echo 'The answers are <br />perimeter: '.$perimeter.'<br />area: '.$area;
?>

Print Screen:

Exercise 5
Coding:
<html>
<head>
<title>CAR</title>
</head>
<body>
<form action="" method="post">
<fieldset><legend>Car:</legend>
<p><b>Kilometer(km):</b> <input type="number" name="km" size="20" maxlength="40"
/></p>
<p><b>Hours(h):</b> <input type="number" name="h" size="20" maxlength="40" /></p>
</fieldset>
<input type="Submit" value="Calculate"/>
</form>
</body>
</html>
<?php
// Validate quantity
if (!empty($_POST['km'])) {
$km = $_POST['km'];
} else {
$km = NULL;
echo '<p><b>You forgot to enter kilometer!</b></p>';
}
// Validate price
if (!empty($_POST['h'])) {
$h = $_POST['h'];
} else {
$h = NULL;
echo '<p><b>You forgot to enter hours!</b></p>';
}

// If everything is okay, print the message.


if ($km && $h) {
// Calculate the total.
$total = $km / $h;
$total = number_format ($total, 2);
// Print the results.
echo 'The rate to reach the destination is <b>'.$total;
} else { // One form element was not filled out properly.
echo '<p><font color="red">Please go back and fill out the form again.</font></p>';
}
?>
</body>
</html>

Print Screen:

Exercise 6
Coding:
<html>
<head>
<title>TV Channel</title>
</head>
<body>

<form action="" method="post">


<select name='channel'>
<option
<option
<option
<option
<option

value='Selection'>Please Select Your Channel</option>


value='3'>3</option>
value='104'>104</option>
value='114'>114</option>
value='Others'>Others</option>

</select>

<div align="left"><input type="submit" name="submit" value="submit" /></div>


</p>
</form>
</body>
</html>

<?php
$channel = $_POST['channel'] ;
if ($channel == "3" )
{
echo "Your channel is CHANNEL 3 : Merlin" ;
}
elseif ($channel == "104" )
{
echo "Your channel is CHANNEL 104 : Master Chef" ;
}
elseif ($channel == "114" )
{ echo "Your channel is CHANNEL 114 : Reflections" ;
}
elseif ($channel == "Others" )
{ echo "Not my favorite program" ;
}
else
{
echo "Not a valid option." ;
}
?>

Print Screen:

Exercise 7 (switch)
Coding:
<html>
<head>
<title>TV Channel</title>
</head>
<body>

<form action="" method="get">

<select name='channel'>
<option
<option
<option
<option
<option

value='Selection'>Please Select Your Channel</option>


value='3'>3</option>
value='104'>104</option>
value='114'>114</option>
value='Others'>Others</option>

</select>

<div align="left"><input type="submit" name="submit" value="submit" /></div>


</p>
</form>
</body>
</html>
<?php
$channel = $_GET['channel'] ;
switch ($channel) {
case '3':
echo "Your channel is CHANNEL 3: Merlin";
break;
case '104':
echo "Your channel is CHANNEL 104: Master Chef";
break;
case '114':
echo "Your channel is CHANNEL 114: Reflection";
break;
default:
echo "Not my favorite program" ;
}
?>

Print Screen:

Exercise 8 (Loop 1)
Coding:
<?php
for ($i=0; $i<11; $i++) {
echo "$i<br />";
}
?>

Print Screen:

Exercise 9 (Loop 2)
Coding:
<?php
$array = array(1, 5, 9, 13, 17, 21);
foreach ($array as $value) {
echo "$value<br />";
}
?>

Print Screen:

Exercise 10 (Loop 3)
Coding:
<?php
$a = range(1,10);
$b = range("a","n");
print_r ($a);
print_r ($b);
?>

Print Screen:

Exercise 11 (Loop 4)
Coding:
<?php
$num = 1;
while ( $num <= 10 ) {
print "$num<br />";
$num += 2;
}
?>

Print Screen:

Exercise 12 (Loop 5)
Coding:
<?php
$num = 50;
while ( $num <= 100 ) {
print "$num<br />";
$num += 2;
}
?>

Print Screen:

Das könnte Ihnen auch gefallen