
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































PHP BROWSER HANDLING POWER.......................................................................................................................... 20
Using Php’s Server Variable:........................................................................................................................................ 20 Using HTTP Headers.................................................................................................................................................... 21 Redirecting Browser with HTTP Headers.................................................................................................................... 22
Dumping a Form’s Data All at once..............................................................................................................................
Performing Data Validation...............................................................................................................................................
Checking for already visited page:................................................................................................................................ Requiring Numbers..................................................................................................................................................... 26 Requiring Text..................................................................................................................................................... 27 Persisting user data..................................................................................................................................................... 27 Client-Side Data Validation........................................................................................................................................... Handling html tags in user input................................................................................................................................... 29
functionfunction_name ([arg_list…])
{
[statements]
[returnreturn_value]
}
function display() { echo “Message from function”;
function average($arr) { $sum = 0; foreach($arr as $v) $sum += $v; echo "Average = " , $sum / 3; } ?> Output: Average = 70.
In the above program,
i. func_num_args () The function returns the number of arguments passed to it. ii. func_get_arg() Returns single arguments. iii. func_get_args() Returns all arguments in an array.
E.g. Program 2.
"; } // function call 1 display ("Hello"); display ("Welcome" , "to" , "all"); $x = "abc"; display ($x, "company's"); ?>In the above example,
$x = 10; echo "Value of X in function = $x"; echo "Value in V inside function = $v"; } ?>
Output: Value in x = 5 Value of x in function = 10 Error: undefined variable V in line 11 Current value in x = 5 Value in v = 8
$bool = TRUE;
echo "Conditional Function is not Ready";
cond_func();
if ($bool)
{
function cond_func()
{
echo "
Msg from conditional function";
}
}
?>
Output:
containing it is executed.
Eg:
Msg from conditional function"; } } cnd_func(); ?> Output:Conditional Function is not Ready
Msg from conditional function
{
echo "Red = $arg" ;
}
$f_var = "red";
$f_var ("color");
?>
Output:
Red = color
$f_var (“color”);
inner_function();
Main program:
include("math.inc");echo PI;
echo "
Sum of 5 and 9 is " , add(5,9);
?>
Save this file as include.php
Syntax:
or die(message);
E.g. Program 2.
echo "Reciprocal of 2 is:";recep(2) or die("can't find reciprocal of zero");
echo "Reciprocal of 0 is ";
recep(0) or die(" can't find reciprocal of zero");
function recep($a)
{
if ($a !=0) {
echo (1/$a);
return TRUE; }
else
return FALSE;
}
?>
Output:
Reciprocal of 2 is:0.5Reciprocal of 0 is can't find reciprocal of zero
The recep() function is called twice. When the argument is 0, the function is designed to return FALSE which will invoke the die() instead of giving warning.
E.g.
If POST method is used, the data can be gotten from $_POST array.
$_REQUEST array holds both data’s from $_post and $_GET.
<input type = “text” name = “txt1” value = “Enter name”>
E.g. Program 2.
Php program:
$nm = $_REQUEST ['txt1'];echo $nm;
?>
<text area name = “
</text area>
Php2-21.php:
echo "Reading data in Check boxesif(isset($_POST['check1']))
echo $_POST['check1'],"
";
if(isset($_POST['check2']))
echo $_POST['check2'],"
";
if(isset($_POST['check3']))
echo $_POST['check3'],"
";
?>
The above php coding will display the text in value attribute when the code runs.
{
echo $_REQUEST[‘radio1’];
}
?