PHP Looping
Looping statements in PHP are used
to execute the same block of code a specified number of times.
Looping
Very often when you write code, you want the same block of code to
run a number of times. You can use looping statements in your code to perform
this.
In PHP we have the following looping statements:
- while - loops through a block of code if and as long as a specified condition is true
- do...while - loops through a block of code once, and then repeats the loop as long as a special 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 while Statement
The while statement will execute a block of code if and as long
as a condition is true.
Syntax
while (condition)
code to be executed; |
Example
The following example demonstrates a loop that will continue to
run as long as the variable i is less than, or equal to 5. i will increase by 1
each time the loop runs:
<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
|
The do...while Statement
The do...while statement will execute a block of code at least
once - it then will repeat the loop as long as a condition is true.
Syntax
do
{
code to be executed;
}
while (condition);
|
Example
The following example will increment the value of i at least once,
and it will continue incrementing the variable i as long as it has a value of
less than 5:
<html>
<body>
<?php
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
</body>
</html>
|
The for Statement
The for statement is used when you know how many times you want to
execute a statement or a list of statements.
Syntax
for (initialization; condition; increment)
{
code to be executed;
}
|
Note:
The for statement has three parameters. The first parameter initializes
variables, the second parameter holds the condition, and the third parameter
contains the increments required to implement the loop. If more than one
variable is included in the initialization or the increment parameter, they
should be separated by commas. The condition must evaluate to true or false.
Example
The following example prints the text "Hello World!"
five times:
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?>
</body>
</html>
|
The foreach Statement
The foreach statement is used to loop through arrays.
For every loop, the value of the current array element is assigned
to $value (and the array pointer is moved by one) - so on the next loop, you'll
be looking at the next element.
Syntax
foreach (array as value)
{
code to be executed;
}
|
Example
The following example demonstrates a loop that will print the values
of the given array:
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>
|
PHP Functions
The real power of PHP comes from
its functions.
In PHP - there are more than 700
built-in functions available.
PHP Functions
In this tutorial we will show you how to create your own
functions.
For a reference and examples of the built-in functions, please
visit our PHP Reference.
Create a PHP Function
A function is a block of code that can be executed whenever we
need it.
Creating PHP functions:
Creating PHP functions:
- All functions start with the word "function()"
- Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
- Add a "{" - The function code starts after the opening curly brace
- Insert the function code
- Add a "}" - The function is finished by a closing curly brace
Example
A simple function that writes my name when it is called:
<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
writeMyName();
?>
</body>
</html>
|
Use a PHP Function
Now we will use the function in a PHP script:
<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>
|
The output of the code above will be:
Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name.
|
PHP Functions - Adding parameters
Our first function (writeMyName()) is a very simple function. It
only writes a static string.
To add more functionality to a function, we can add parameters. A
parameter is just like a variable.
You may have noticed the parentheses after the function name,
like: writeMyName(). The parameters are specified inside the parentheses.
Example 1
The following example will write different first names, but the
same last name:
<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
</body>
</html>
|
The output of the code above will be:
My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.
|
Example 2
The following function has two parameters:
<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Ståle","...");
?>
</body>
</html>
|
The output of the code above will be:
My name is Kai Jim Refsnes.
My name is Hege Refsnes!
My name is Ståle Refsnes...
|
PHP Functions - Return values
Functions can also be used to return values.
Example
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
</body>
</html>
|
The output of the code above will be:
1 + 16 = 17
|
No comments:
Post a Comment