include()

include() generates a warning, but the script will continue execution.

Syntax: include(path)

Description:

The argument “path” is Required. It specifies the path to the file that is to be called.

Example:
Code:

include("abc.php");
echo  "Hello";

Output:

Warning: include(abc.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\control structure\include.php on line 2

Warning: include() [function.include]: Failed opening ‘abc.php’ for inclusion (include_path=’.;C:\php5\pear’) in C:\wamp\www\control structure\include.php on line 2
Hello

switch

You can Use the switch statement to choose 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;
default:
code to be executed if n is different from both Label1 and Label2;
}

First we have a single expression “n”. 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. You can Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

Example:
Code:

$i=4;
switch ($i) {
case 0:
    echo "Tom";
    break;
case 1:
    echo "Lara";
    break;
case 2:
    echo "Lyod";
    break;
default:
    echo "Harper";
}

Output:

Harper

continue

This statement specifies to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

Syntax: continue [n]

Description:

The argument “n” is Optional.It specifies the Number of levels to skip.
Example:
Code:


for  ($i = 0; $i < 10; $i++) {
   if ($i== 5)
      continue;
   echo "$i ";
}

Output:

0 1 2 3 4 6 7 8 9

break

break stops execution of the current for, foreach, while, do-while or switch structure.

Syntax: break;

Example:
Code:

$name = array('Tom', 'Ross',   'Smith',  'Taylor','Mahanama', 'Harsha');
while (list(, $val) = each($name)) {
    if ($val == 'Taylor') {
        break;    
    }
    echo "$val
\n"; }

Output:

Tom
Ross
Smith

foreach loop

The foreach loop is used to loop through arrays.

Syntax:

foreach ($array as $value)
{
code to be executed;
}

foreach ($array as $key => $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) – so on the next loop iteration, you’ll be looking at the next array value.

Example1:
Code:

$arr = array(4,6,8,10);
foreach ($arr as &$value) {
    $value = $value * 3;
	echo $value;
}

Output:

12 18 24 30

Example2:
Code:

 &$value) {
   $value = $value * 3;
	echo "key: $key, value: $value 
"; }

Output:

key: 0, value: 12
key: 1, value: 18
key: 2, value: 24
key: 3, value: 30

Page 1 of 3123
eXTReMe Tracker