

How to select a host and deciding hosting packages.PHP Info: Details on PHP script settings & installation at server end.Calling another PHP file from main Php file.Disabling a part of the code by using comments in PHP.Adding delay to the script execution time by using sleep function.print command & escape character for string outputs.echo command & escape character for string outputs.Introduction to PHP: How scripting languages work.← Introduction to PHP Sample codes in PHP →Ĭheck the string or number is Palindrome or not in PHP → Let us try with some simple PHP examples. Now script execution will stop after 2 seconds Set_time_limit ( 2 ) // Max execution time is set to 2 seconds This value of 30 seconds is set at php.ini file, we can change this upper limit of execution time by using ini_set() function.Įrror_reporting(E_ALL) // Display all types of error Server stops the execution of the script after 30 seconds. Inside the loop each time we are resetting the value of $i to 5, so it can never reach value of 10 to exit the loop.įatal error: Maximum execution time of 30 seconds exceeded in H:\php_files\plus2net\z\for.php on line 18 What happens when we keep a condition which the loop can never meets? While loops are used for conditional looping. Here we have printed from 1 to 10 numbers, by changing the values inside the for loop you can print numbers starting from any value to ending at any value.įor loop is used when we know exactly how many times the looping is required.

This way all numbers will be printed in one new line. In the next line we have used the same echo command to print one html line break. The echo command will print the value of $i to the screen.

You can easily extend this program to print any numbers from starting from any value and ending on any value. We can print numbers from 1 to 10 by using for loop. Here is an example.For and foreach loop in PHP with break statement and creating patterns using nested for loops One for loop can be kept inside another for loop and we can generate different scripts using nested loops. $no=count($a) // Number of elements in array We can get the number of elements and use that in our condition checking. This is not a good idea as the count() function returns the same value in each iteration all the time. Here we are counting number of elements in array in each iteration. We can get number of elements in an array by using count(). Read how to display elements of an array Speed of looping This will list all the values of the array from starting to end. So we need not call the reset() again( Reset() rewinds array's internal pointer to the first element.

Once the foreach function is called the array pointer will reset to the first element of the array. While handling PHP Arrays foreach function is required to display the elements of an array. If the condition to check is never meet then it will be an infinite loop execution and PHP execution will hang or stop after reaching maximum execution time. We can reduce the step value in each step of iteration. Similarly $i -=10 is same as $i=$i-10 Decrement step value in loop Now we will use a step value of 10 so the variable $i will increase its value by 10 ( it was increasing by 1 in previous cases ) in each looping. This increase or step value can be changed. So far we have seen each time the variable value is increased by 1. The If condition inside the for loop will exit the loop when the value of $i exceeds 5 ( that is when it equals to 6) Check the ouput just outside the for loop.Įcho " Value of \$i Outside the loop: ".$i īy using break we can exit the loop without completing the total required loopings (early exit). Here condition is checked ( Expr 2) at the staring of the loop.Įxpr3 is executed at the end of the loop. Next time when the value of $i becomes 11 the condition checking fails and the loop is escaped without executing. So in the first loop the value of $i became 1 and in the last loop the value equals to 10. In above case the loop is executed 10 times and each time the value of $i is increased by 1. For and foreach loop in PHP with break statement and creating patterns using nested for loops
