0% found this document useful (0 votes)
96 views

Seiya Tanaka Subject: Internet and Web Development

PHP allows users to define their own functions for code reusability. Functions can accept arguments, return values, and are called using function names. Forms can submit data to PHP scripts using either the GET or POST methods. GET sends data in the URL while POST embeds it in the request body, making POST more secure but with data limits. Key decisions around using GET vs POST involve visibility of submitted data and ability to bookmark pages.

Uploaded by

TRYVERN MAMIZA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Seiya Tanaka Subject: Internet and Web Development

PHP allows users to define their own functions for code reusability. Functions can accept arguments, return values, and are called using function names. Forms can submit data to PHP scripts using either the GET or POST methods. GET sends data in the URL while POST embeds it in the request body, making POST more secure but with data limits. Key decisions around using GET vs POST involve visibility of submitted data and ability to bookmark pages.

Uploaded by

TRYVERN MAMIZA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PHP

Seiya Tanaka

Subject:

Internet and Web Development
Contents

Functions

Form

POST

GET
Functions
Besides the built-in PHP functions, we can
create our own functions.

A function is a block of statements that can


be used repeatedly in a program.

A function will not execute immediately


when a page loads.

A function will be executed by a call to the


function.
Create a User Defined
Function in PHP
A user defined function declaration starts
with the word "function":

<?php
function writeMsg() {
echo "Hello world!";
}

writeMsg(); // call the function


?>
PHP Function Arguments
Information can be passed to functions
through arguments. An argument is just like
a variable.

<?php
function familyName($fname) {
echo "$fname<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
Returning values
To let a function return a value, use the
return statement:

<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}

echo "5 + 10 = " . sum(5, 10) .


"<br>";
echo "7 + 13 = " . sum(7, 13) .
"<br>";
echo "2 + 4 = " . sum(2, 4);
?>
Form(POST)
The example below displays a simple HTML
form with two input fields and a submit
button:
index.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>
Form(POST)
To display the submitted data you could
simply echo all the variables. The
"welcome.php" looks like this:
welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo
$_POST["email"]; ?>

</body>
</html>
Form(GET)
The same result could also be achieved using
the HTTP GET method:
index.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>
Form(GET)
and "welcome_get.php" looks like this:

welcome_get.php
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo
$_GET["email"]; ?>

</body>
</html>
When to use

GET or POST?
GET

Information sent from a form with the GET method is visible to


everyone (all variable names and values are displayed in the
URL).

It is possible to bookmark the page.


POST

Information sent from a form with the POST method is invisible


to others (all names/values are embedded within the body of
the HTTP request) and has no limits on the amount of
information to send.

The security is better than GET.


Summary

Functions

Form

POST

GET

You might also like