PHP User Defined Functions
PHP User Defined Functions
Syntax
function functionName() {
code to be executed;
}
Example
<!DOCTYPE html>
<html>
<body>
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg();
?>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<?php
function familyName($fname) {
echo "$fname.<br>";
familyName("san");
familyName("dan");
?>
</body>
</html>
Example
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
<!DOCTYPE html>
<html>
<body>
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");
?>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
function test(){
$a = 10;
test();
?>
output
The Value of $a inside function is : 10
The Value of $a Outside Function is :
Warning: Undefined variable $a in C:\xampp\htdocs\san\summ.php on line 7
PHP The static Keyword
Normally, when a function is completed/executed, all of its
variables are deleted. However, sometimes we want a local
variable NOT to be deleted. We need it for a further job.
<html>
<body>
<?php
function myTest()
static $x = 0;
$b=5;
$x++;
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
output
x= 0
b= 5
x= 1
b= 5
x= 2
b= 5
<html>
<body>
<?php
$x = 5; // global scope
function myTest() {
myTest();
?>
</body>
</html>
Output
Warning: Undefined variable $x in C:\xampp\htdocs\san\summ.php on line 10
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
PHP $GLOBALS
$GLOBALS is a PHP super global variable which is used to
access global variables from anywhere in the PHP script
(also from within functions or methods).
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
</body>
</html>
output
/san/summ.php
localhost
localhost
PHP $_REQUEST
PHP $_REQUEST is used to collect data after submitting an
HTML form.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = htmlspecialchars($_REQUEST['fname']);
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
PHP $_POST
PHP $_POST is widely used to collect form data after
submitting an HTML form with method="post". $_POST is
also widely used to pass variables.
The example below shows a form with an input field and a
submit button. When a user submits the data by clicking on
"Submit", the form data is sent to the file specified in the
action attribute of the <form> tag. In this example, we point
to the file itself for processing form data. If you wish to use
another PHP file to process form data, replace that with the
filename of your choice. Then, we can use the super global
variable $_POST to collect the value of the input field:
Example
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?
>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
PHP 5 Form Handling
The PHP superglobals $_GET and $_POST are used to
collect form-data.
Example
<!DOCTYPE HTML>
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
When the user fills out the form above and clicks the submit
button, the form data is sent for processing to a PHP file
named "welcome.php". The form data is sent with the HTTP
POST method.
To display the submitted data you could simply echo all the
variables. The "welcome.php" looks like this:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
Welcome John
Your email address is john.doe@example.com
The same result could also be achieved using the HTTP GET
method:
Example
<!DOCTYPE HTML>
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
Program1
<html>
<body>
<form method="post">
</form>
<?php
if(isset($_POST['submit']))
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$sum = $number1+$number2;
?>
</body>
</html>
Program2
<!DOCTYPE html>
<html>
<head>
<title>PHP insertion</title>
<body>
<div class="title">
</div>
<h2>Form</h2>
<label>Code:</label>
<label>FirstName:</label>
<label>LastName:</label>
</form>
</body>
</html>
This function returns true if the variable exists and is not NULL, otherwise it
returns false.
insert.php
<!DOCTYPE html>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
$ecode = $_POST['cd'];
$efname = $_POST['fn'];
$elname = $_POST['ln'];
?>
</body>
</html>