Ethical Hacking Programming, Blogging, Hosting, All Computer Software, PC Software Download, JAVA in hindi, HTML, PHP, C, C++, Free Learning, Software's Download, Technical Videos, Technical Tricks and Tips, How Make Money

Learn Php free in english

PHP Reference: Beginner to Intermediate PHP chapter 2 Class 3.



FOR


for (expr1; expr2; expr3) {
// If expr2 is TRUE, do this
}

When started, the for loop executes expr1 once at the beginning. Next, expr2 is evaluated. If expr2 is true, the code inside the for loop is executed. When the for loop reaches the end, expr3 is executed before looping and checking expr2 again.

Example:

for ($x = 1; $x <= 5; $x++){

echo $x;
}


See Also:

break – Stops the for loop and exits it immediately

continue – Stops the current iteration of the for loop, and expr3 is executed before checking expr2 again


Alternative syntax for a for statement:

for (expr1; expr2; expr3):

// If expr2 is TRUE, do this endfor;

An example of continue and break in a for loop:

for ($v=0;$v<=10;$v++){

echo $v;
if ($v == 5){
continue;
}
if ($v == 8){

break;
}

echo ',';
}

0,1,2,3,4,56,7,8




foreach

foreach ($array as $value){
// Do something

}

// Another form, for keys and values foreach ($array as $key => $value){

// Do something

}

The foreach loop goes through all items in an array, assigning a temporary variable name for value and, if chosen, the key as well so they can be used within the executed code inside the loop.

Examples:

$array = array('John' => 20, 'Jane' => 24, 'Joseph' => 28); foreach ($array as $value){

echo "$value, ";
}


20, 24, 28,

foreach ($array as $name => $age){

echo "$name - $age";
echo '<br />'; // XHTML for a line break
}


John - 20
Jane - 24
Joseph - 28

See Also:

Pass by Reference – Using the ampersand ( & ) to alter an array through foreach



break [$integer]

$integer – [optional] Specifies the number of nested loops to break out of

Exits and stops execution of the current (default) for, foreach, while, do-while, or switch loop.

Example:

$counter = 0;
while (1 == 1){ // Will run forever
while (0 == 0){ // Will also run forever
$counter++; // Increment $counter plus 1
echo $counter;

if ($counter == 5){
break 2;

}
}

echo 'First while loop'; // Never displayed because of break 2; break; // Never run, but if it did, would end the first while loop
}

continue [$integer]

$integer – [optional] Specifies the number of nested loops to skip out of Note: The $integer does not supply the number of iterations to skip, it always only stops the current iteration from continuing any further.

Skips the rest of the current loop iteration and if applicable, continues to the next iteration of the loop3.

Example:

for ($x=1;$x<=10;$x++){
if ($x == 5){
continue;

} // The echo never occurs if $x == 5 echo $x;

}



return [$variable]

$variable – [optional] The variable to be returned from a function

If used as part of a regular script and not part of a function, it works the same as exit() or die(). Return is more commonly used as part of a function to assign a value to the results of a function back at the original function call.

See Also:

Functions – Provides an example of returning a $variable as part of a function

exit() – Terminate the current script immediately

include(file)

file - $string

Include and evaluate the file as part of the current script/page. This is an easy way to store common variables, functions4, or lines of HTML that will be included by multiple scripts/pages. Failure of the function generates an error.

Example:

include('somefile.inc');


In the case of a switch, continue has the same effect as break

Functions should only be included once. Consider using include_once() or require_once()

include_once(file)


require_once(file)

file - $string

Include and evaluate the file as part of the current script/page. If the file has already been included, it will ignore the request. This is an easy way to store common variables, functions or lines of HTML that will be included by several scripts / pages.

Failure of the function generates an error and terminates the script immediately.

Example:

require_once('somefile.php');



 Functions should only be included once. Consider using require_once()


Share:

No comments:

Post a Comment

Follow On YouTube