Hosted by Dailymotion. For legal issues report at the Copyright Center, report us on DMC, or use the Instant Removal tool.
PHP MYSQL LESSON 7 ( PART 1 ) _ LOOPS IN PHP
90 Views • Dec 16, 2017
Description
(PHP 4, PHP 5, PHP 7)
for loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a for loop is:
for (expr1; expr2; expr3)
statement
The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.
In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.
At the end of each iteration, expr3 is evaluated (executed).
Each of the expressions can be empty or contain multiple expressions separated by commas. In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part. expr2 being empty means the loop should be run indefinitely (PHP implicitly considers it as TRUE, like C). This may not be as useless as you might think, since often you'd want to end the loop using a conditional break statement instead of using the for truth expression.
Consider the following examples. All of them display the numbers 1 through 10:
10) {
break;
}
echo $i;
}
/* example 3 */
$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
/* example 4 */
for ($i = 1, $j = 0; $i
Of course, the first example appears to be the nicest one (or perhaps the fourth), but you may find that being able to use empty expressions in for loops comes in handy in many occasions.
PHP also supports the alternate "colon syntax" for for loops.
for (expr1; expr2; expr3):
statement
...
endfor;
It's a common thing to many users to iterate through arrays like in the example below.
'Kalle', 'salt' => 856412),
array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0; $i
The above code can be slow, because the array size is fetched on every iteration. Since the size never changes, the loop be easily optimized by using an intermediate variable to store the size instead of repeatedly calling count():
More from User
Almond Butter Recipe
Akash Jangra
PHP MYSQL LESSON 7 ( PART 1 ) _ LOOPS IN PHP
Akash Jangra
PHP MYSQL LESSON 6 ( PART 2 ) _ ARRAY IN PHP
Akash Jangra
PHP MYSQL LESSON 6 ( PART 1 ) _ ARRAY IN PHP
Akash Jangra
PHP MYSQL LESSON 5 (Part 1) _ OPERATORS IN PHP
Akash Jangra
PHP MYSQL LESSON 5 ( Part 2 ) _ OPERATORS IN PHP
Akash Jangra
Related Videos
Read PHP MySQL Web Programming Interview Questions Answers and Explanations: PHP MySQL FAQ
Boesen
Just Enough Web Programming with XHTML PHP and MySQL
Jamesgoff
Setting up a VPS for web hosting. How to Install LAMP, Apache, Mysql, PHP, phpmyadmin
panduanvps
Read Web Programming for Business: PHP Object-Oriented Programming with Oracle Ebook Free
Rmetz
Applications of PHP website development Programming Language|PHP Web Development Company | Webhonchoz
Webhonchoz Itservices
[PDF Download] Programming For Beginner's Box Set: Learn HTML HTML5 & CSS3 Java PHP & MySQL
Challenbec