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

PHP

This document discusses PHP conditional statements and loops that allow code to execute differently based on conditions or iterate over blocks of code. It covers the if, if/else, if/elseif/else statements and switch statement for conditional logic. It also covers the while, do/while, for, and foreach loops for iterative logic. Form handling with PHP is demonstrated using the $_GET and $_POST superglobals to access form data submitted via GET or POST methods.

Uploaded by

Tushar Shelake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

PHP

This document discusses PHP conditional statements and loops that allow code to execute differently based on conditions or iterate over blocks of code. It covers the if, if/else, if/elseif/else statements and switch statement for conditional logic. It also covers the while, do/while, for, and foreach loops for iterative logic. Form handling with PHP is demonstrated using the $_GET and $_POST superglobals to access form data submitted via GET or POST methods.

Uploaded by

Tushar Shelake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

PHP Conditional Statements

Very often when you write code, you want


to perform different actions for different conditions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:

•if statement - executes some code if one condition is true

•if...else statement - executes some code if a condition is true


and another code if that condition is false

•if...elseif...else statement - executes different codes for more


than two conditions

•switch statement - selects one of many blocks of code to be


executed
PHP - The if Statement
The if statement executes some code if one condition is true.

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 ($t < "20") {


echo "Have a good day!";
}
?>
PHP - The if...else Statement
The if...else statement executes some code if a condition is true and another
code if that condition is false.
Syntax
if (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}
Example
Output "Have a good day!" if the current time is less than 20, and "Have a good
night!" otherwise:
<?php
$t = date("H");

if ($t < "20") {


echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The if...elseif...else Statement
The if...elseif...else statement executes different codes for more than two
conditions.
Syntax

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");

if ($t < "10")


{
echo "Have a good morning!";
} elseif ($t < "20")
{
echo "Have a good day!";
} else
{
echo "Have a good night!";
}
?>
The PHP switch Statement
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
This is how it works: First we have a single expression n (most often a variable),
that is evaluated once. The value of the expression is then compared with the values for
each case in the structure. If there is a match, the block of code associated with
that case is executed. Use break to prevent the code from running into
the next case automatically. The default statement is used if no match is found.
Example
<?php
$favcolor = "red";

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

•for - loops through a block of code a specified number of times

•foreach - loops through a block of code for each element in an array


The PHP while Loop
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true)
{
code to be executed;
}

The example below first sets a variable $x to 1 ($x = 1).


Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5).
$x will increase by 1 each time the loop runs ($x++):
Example
<?php
$x = 1;

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);

The example below first sets a variable $x to 1 ($x = 1).


Then, the do while loop will write some output, and then increment the variable $x with 1.
Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue
to run as long as $x is less than, or equal to 5:
Example
<?php
$x = 1;

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 example below displays the numbers from 0 to 10:


Example
<?php
for ($x = 0; $x <= 10; $x++)
{
echo "The number is: $x <br>";
}
?>
The PHP foreach Loop
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value
and the array pointer is moved by one, until it reaches the last array element.

The following example demonstrates a loop that will output the values of the given array ($colors):
Example
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value)


{
echo "$value <br>";
}
?>
The PHP superglobals $_GET and $_POST are used to collect form-data.

PHP - A Simple HTML Form


The example below displays a simple HTML form with two input fields and a submit
button:
Example
<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>
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>

<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>

and "welcome_get.php" looks like this:


<html>
<body>

Welcome <?php echo $_GET["name"]; ?><br>


Your email address is: <?php echo $_GET["email"]; ?>

</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!

You might also like