PHP
PHP
Syntax
if (condition)
{
code to be executed if condition is true;
}
Example
Output "Have a good day!" if the current time (HOUR) is less than 20:
<?php
$t = date("H");
if (condition)
{
code to be executed if this condition is true;
} elseif (condition)
{
code to be executed if first condition is false and this condition is true;
} else
{
code to be executed if all conditions are false;
}
Example
Output "Have a good morning!" if the current time is less than 10,
and "Have a good day!" if the current time is less than 20.
Otherwise it will output "Have a good night!":
<?php
$t = date("H");
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Loops
Often when you write code, you want the same block of code to run over and over again in a
row.
Instead of adding several almost equal code-lines in a script, we can use loops to perform a
task like this.
In PHP, we have the following looping statements:
•while - loops through a block of code as long as the specified condition is true
•do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
The PHP do...while Loop
The do...while loop will always execute the block of code once,
it will then check the condition, and repeat the loop while the specified condition is true.
Syntax
do {
code to be executed;
} while (condition is true);
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
The PHP for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
for (init counter; test counter; increment counter)
{
code to be executed;
}
Parameters:
•init counter: Initialize the loop counter value
•test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop
continues.
If it evaluates to FALSE, the loop ends.
•increment counter: Increases the loop counter value
The following example demonstrates a loop that will output the values of the given array ($colors):
Example
<?php
$colors = array("red", "green", "blue", "yellow");
</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>
</body>
</html>
The output could be something like this:
Welcome John
Your email address is [email protected]
The same result could also be achieved using the HTTP GET method:
Example
<html>
<body>
</body>
</html>
</body>
</html>
GET vs. POST
Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3
=> value3, ...)). This array holds key/value pairs, where keys are the names of the
form controls and values are the input data from the user.
Both GET and POST are treated as $_GET and $_POST. These are superglobals,
which means that they are always accessible, regardless of scope - and you can
access them from any function, class or file without having to do anything special.
$_GET is an array of variables passed to the current script via the URL parameters.
$_POST is an array of variables passed to the current script via the HTTP POST
method.
When to use GET?
Information sent from a form with the GET method is visible to
everyone (all variable names and values are displayed in the URL). GET
also has limits on the amount of information to send. The limitation is
about 2000 characters. However, because the variables are displayed in
the URL, it is possible to bookmark the page. This can be useful in some
cases.
GET may be used for sending non-sensitive data.
Note: GET should NEVER be used for sending passwords or other
sensitive information!
When to use 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.
Moreover POST supports advanced functionality such as support for multi-part
binary input while uploading files to server.
However, because the variables are not displayed in the URL, it is not possible to
bookmark the page.
Developers prefer POST for sending form data.
Next, lets see how we can process PHP forms the secure way!