Seiya Tanaka Subject: Internet and Web Development

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy