3 Control Structure and Funcrtions in PHP
3 Control Structure and Funcrtions in PHP
<html>
<body>
<?php $a = 0;
$b = 0;
for( $i = 0; $i<5; $i++ )
{
$a += 10;
$b += 5;
}
echo ("At the end of the loop a = $a and b = $b" );
?>
</body>
</html>
This will produce the following result −
At the end of the loop a = 50 and b = 25
The while loop statement
The while statement will execute a block of <html>
code if and as long as a test expression is <body>
true.
If the test expression is true then the code
<?php
block will be executed. After the code has $i = 0;
executed the test expression will again be $num = 50;
evaluated and the loop will continue until the
test expression is found to be false. while( $i < 10)
Syntax {
while (condition) $num--;
{
$i++;
code to be executed;
} }
Example echo ("Loop stopped at i =
This example decrements a variable value on $i and num = $num" );
each iteration of the loop and the counter
increments until it reaches 10 when the
?>
evaluation is false and the loop ends. </body>
</html>
This will produce the following result −
Loop stopped at i = 10 and num = 40
The do...while loop statement <html>
The do...while statement will execute a <body>
block of code at least once - it then will <?php
repeat the loop as long as a condition $i = 0;
is true.
$num = 0;
Syntax
do
do
{
{
$i++;
code to be executed;
} while( $i < 10 );
} while (condition);
echo ("Loop stopped at i =
$i" );
Example
?>
The following example will increment </body>
the value of i at least once, and it will </html>
continue incrementing the variable i as
long as it has a value of less than 10 −
This will produce the
following result −
Loop stopped at i = 10
The foreach loop
statement <html>
The foreach statement is used <body>
to loop through arrays. For <?php
each pass the value of the $array = array( 1, 2, 3, 4, 5);
current array element is foreach( $array as $value )
assigned to $value and the {
array pointer is moved by one echo "Value is $value <br />";
and in the next pass next }
element will be processed. ?>
Syntax </body>
foreach (array as value) </html>
{
code to be executed;
This will produce the following result
−
}
Example Value is 1 Value is 2 Value is 3 Value
Try out following example to is 4 Value is 5
list out the values of an array.
The break statement <html>
The PHP break keyword is <body>
used to terminate the <?php
execution of a loop
prematurely.
$i = 0;
The break statement is while( $i < 10)
situated inside the statement {
block. It gives you full control $i++;
and whenever you want to exit if( $i == 3 )
from the loop you can come break;
out. After coming out of a loop }
immediate statement to the
echo ("Loop stopped at i = $i" );
loop will be executed.
?>
Example </body>
In the following example </html>
condition test becomes true
when the counter value This will produce the following result
reaches 3 and loop terminates. −
Loop stopped at i = 3
The continue statement
The PHP continue keyword is used <html>
to halt the current iteration of a <body>
loop but it does not terminate the <?php
loop.
Just like the break statement
$array = array( 1, 2, 3, 4, 5);
the continue statement is situated foreach( $array as $value )
inside the statement block {
containing the code that the loop if( $value == 3 )
executes, preceded by a conditionalcontinue;
test. For the pass echo "Value is $value <br />";
encountering continue statement,
}
rest of the loop code is skipped and
next pass starts. ?>
Example </body>
</html>
In the following example loop prints
the value of array but for which
condition becomes true it just skip This will produce the following result
the code and next value is printed. −
Value is 1 Value is 2 Value is 4 Value
is 5
PHP Functions
PHP function is a piece of code that can be
reused many times. It can take input as
argument list and return value. There are
thousands of built-in functions in PHP.
In PHP, we can define Conditional
function, Function within
Function and Recursive function also.
Advantage of PHP Functions
Code Reusability: PHP functions are defined
only once and can be invoked many times, like
in other programming languages.
Less Code: It saves a lot of code because you
don't need to write the logic many times. By the
use of function, you can write the logic only
once and reuse it.
Easy to understand: PHP functions separate
the programming logic. So it is easier to
understand the flow of the application because
every logic is divided in the form of functions.
PHP User-defined Functions
We can declare and call user-defined functions
easily. Let's see the syntax to declare user-
defined functions.
Syntax
function functionname(){
//code to be executed
}
Note: Function name must be start with letter
and underscore only like other labels in PHP. It
can't be start with numbers or special symbols.
PHP Functions Example
File: function1.php
<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
Output:
Hello PHP Function
PHP Function Arguments
We can pass the information in PHP function
through arguments which is separated by
comma.
PHP supports Call by Value (default), Call
by Reference, Default argument
values and Variable-length argument list.
Let's see the example to pass single
argument in PHP function.
File: functionarg.php
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
Output:
Hello Sonoo Hello Vimal Hello John
example to pass two argument
Let's see the example to pass two argument in PHP function.
File: functionarg2.php
<?php
function sayHello($name,$age){
echo "Hello $name, you are $age years old<br/>";
}
sayHello("Sonoo",27);
sayHello("Vimal",29);
sayHello("John",23);
?>
Output:
Hello Sonoo, you are 27 years old Hello Vimal, you are 29
years old Hello John, you are 23 years old