SakhaliaNetHome PageHistory of the RailwayVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Exercises (V)

PHP Exercise #12

Maths program

Write a program that performs basic arithmetic operations. It must take two operands via respective text fields and have the four basic arithmetic operations available thru a select menu. Input must be validated to ensure that it is numeric and therefore suitable for mathematical operations. The processing function should display the operands, the operator and the result.

<?php
$ops = array('+','-','*','/');
if (! array_key_exists('_submit_check', $_POST)) {
$_POST['_submit_check'] = 0;
}
if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The data sent is valid, therefore, process it
process_form();
}
} else {
// The form had been sent, therefore, show it
show_form();
}
function show_form($errors = '') {
if ($errors) {
print 'You need to correct the following errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
// The beginning of the form
print '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
// The first operand
print '<input type="text" name="operand_1" size="5" value="';
if (array_key_exists('operand_1', $_POST)) {
print htmlspecialchars($_POST['operand_1']);
}
print ' "/>';
// The operator
print '<select name="operator">';
foreach ($GLOBALS['ops'] as $op) {
print '<option';
if (array_key_exists('operator', $_POST)) {
if ($_POST['operator'] == $op) { print ' selected="selected"'; }
}
print "> $op</option>";
}
// The second operand
print '<input type="text" name="operand_2" size="5" value="';
if (array_key_exists('operand_2', $_POST)) {
print htmlspecialchars($_POST['operand_2']);
}
print ' "/>';
// The submit button
print '<input type="submit" value="Calculate"/>';
// The hidden variable _submit_check
print '<input type="hidden" name="_submit_check" value="1"/>';
// The end of the form
print '</form>';
}
function validate_form() {
$errors = array();
// The operand 1 must be numeric
if (! strlen($_POST['operand_1'])) {
$errors[] = 'Enter a number for the first operand.';
} elseif (! floatval($_POST['operand_1']) == $_POST['operand_1']) {
$errors[] = 'The first operand must be numeric.';
}
// The operand 2 must be numeric
if (! strlen($_POST['operand_2'])) {
$errors[] = 'Enter a number for the second operand.';
} elseif (! floatval($_POST['operand_2']) == $_POST['operand_2']) {
$errors[] = 'The second operand must be numeric.';
}
// The operator must be valid
if (! in_array($_POST['operator'], $GLOBALS['ops'])) {
$errors[] = 'Please select a valid operator.';
}
return $errors;
}
function process_form() {
if ('+' == $_POST['operator']) {
$total = $_POST['operand_1'] + $_POST['operand_2'];
} elseif ('-' == $_POST['operator']) {
$total = $_POST['operand_1'] - $_POST['operand_2'];
} elseif ('*' == $_POST['operator']) {
$total = $_POST['operand_1'] * $_POST['operand_2'];
} elseif ('/' == $_POST['operator']) {
$total = $_POST['operand_1'] / $_POST['operand_2'];
}
print "$_POST[operand_1] $_POST[operator] $_POST[operand_2] = $total";
// print nl2br("\n\n"); // These lines would allow the form
// show_form(); // to be displayed after sending it
}
?>

PHP Exercise #13

Processing a form with data in arrays

Write a function that could display all the parameters sent by a form, being the values of $_POST not simple variables, but arrays.

In this simulation, the function print_array pass thru the array $_POST printing its keys and values. If these values are arrays, then the function calls to itself, passing to itself the subarray to print. A function that calls to itself is called a recursive function.

<?php
$_POST = array('New York' => array('state' => 'NY', 'pop' => 8000278),
'Los Angeles' => array('state' => 'CA', 'pop' => 3694820),
'Chicago' => array('state' => 'IL', 'pop' => 2896016),
'Houston' => array('state' => 'TX', 'pop' => 1953631),
'Philadelphia' => array('state' => 'PA', 'pop' => 1517550),
'Phoenix' => array('state' => 'AZ', 'pop' => 1321045),
'San Diego' => array('state' => 'CA', 'pop' => 1223400),
'Dallas' => array('state' => 'TX', 'pop' => 1188580),
'San Antonio' => array('state' => 'TX', 'pop' => 1144646),
'Detroit' => array('state' => 'MI', 'pop' => 951270));
function print_array($ar, $prefix) {
// Pass thru the array
foreach ($ar as $key => $value) {
// If the value of an element is an array, then call again print_array()
// to pass thru that subarray and add the name of the key to the prefix
if (is_array($value)) {
print_array($value, $prefix . "['" . htmlentities($key) . "']");
} else {
// If the value is not an array, then print it with any prefix
print $prefix;
print "['" . htmlentities($key) . "'] = ";
print htmlentities($value) . '
';
}
}
}
function process_form2() {
print_array($_POST, '$_POST');
}
process_form2();
?>
$_POST['New York']['state'] = NY
$_POST['New York']['pop'] = 8000278
$_POST['Los Angeles']['state'] = CA
$_POST['Los Angeles']['pop'] = 3694820
$_POST['Chicago']['state'] = IL
$_POST['Chicago']['pop'] = 2896016
$_POST['Houston']['state'] = TX
$_POST['Houston']['pop'] = 1953631
$_POST['Philadelphia']['state'] = PA
$_POST['Philadelphia']['pop'] = 1517550
$_POST['Phoenix']['state'] = AZ
$_POST['Phoenix']['pop'] = 1321045
$_POST['San Diego']['state'] = CA
$_POST['San Diego']['pop'] = 1223400
$_POST['Dallas']['state'] = TX
$_POST['Dallas']['pop'] = 1188580
$_POST['San Antonio']['state'] = TX
$_POST['San Antonio']['pop'] = 1144646
$_POST['Detroit']['state'] = MI
$_POST['Detroit']['pop'] = 951270