PHP: Form Handling
";
else
echo "number1 is not int
";
if (filter_input(INPUT_GET, 'number2', FILTER_VALIDATE_FLOAT))
echo "number2 is float " . $_GET['number2'] . "
";
else
echo "number2 is not float
";
// echo gettype((int)$_GET['number1']) . "
";
// echo gettype((float)$_GET['number2']) . "
";
// echo gettype($sum) . " $sum
";
$sum = (int)$_GET['number1'] + (float)$_GET['number2'];
echo $_GET['number1'] . " + " . $_GET['number2'] . " = " . $sum . "
";
}
else
echo "Both number1 and number2 must be entered
";
}
// filter_input($type, $variable_name [, $filter])
// - Get a value from a superglobal variable and optionally filters it
// - Returns the requested value on success,
// FALSE if the filter fails,
// NULL if the requested value is not set
// type - specify the superglobal variable to access
// - common values include INPUT_GET, INPUT_POST, and INPUT_COOKIE
// variable_name - the name of the value to retrieve
// filter - optional. The constrant for the filter to apply
// FILTER_VALIDATE_INT -- validates an integeger value
// FILTER_VALIDATE_FLOAT -- validates a floating-point (double) value
// FILTER_VALIDATE_EMAIL -- validates an email address
// FILTER_VALIDATE_URL -- validates a URL
// FILTER_VALIDATE_BOOLEAN -- return TRUE for "1", "true" "on" "yes
// otherwise, return FALSE
// htmlspecialchars($string)
// - converts certain html special characters (&, ', ", <, >)
// to their corresponding html entities and then returns the resulting string
// - for example, converts the emphasand character (&) to the amphasand entity (&h)
// htmlentities($string)
// - converts html characters that have corresponding html entities
// and returns the resulting string
// when to use GET method
// - typically when the request is for a page that gets data from the server
// - when the request can be executed multiple times without causing any problems
// when to use POST method
// - POST request can get data
// - typically when the intention is to make modification to the data on the server or state of app
// - when executing the request multiple times may cause problems
// - when you don't want to include the parameters in the URL for security reasons
// - when you don't want users to be able to include parameters when they bookmark a page
// - when you need to transfer huge data
// Empty form fields will be NULL unless the user has entered a value.
// isset($var)
// - returns TRUE if the variable has been set and is not a NULL value
// empty($var)
// - returns TRUE if the variable hasn't been set, contains a NULL value,
// or contains an empty sting
// is_numeric($var)
// - returns TRUE if the variable is a number or a string that can be converted to a number
?>