Home » PHP Tutorial » PHP for

Summary : in this tutorial, you will learn about PHP for statement to execute a block of code repeatedly.

Introduction to PHP for statement

The for statement allows you to execute a code block repeatedly. The syntax of the for statement is as follows:

How it works.

  • The start is evaluated once when the loop starts.
  • The condition is evaluated once in each iteration. If the condition is true , the statement in the body is executed. Otherwise, the loop ends.
  • The increment expression is evaluated once after each iteration.

PHP allows you to specify multiple expressions in the start , condition , and increment of the for statement.

In addition, you can leave the start , condition , and increment empty, indicating that PHP should do nothing for that phase.

The following flowchart illustrates how the for statement works:

php for loop assignment

When you leave all three parts empty, you should use a break statement to exit the loop at some point. Otherwise, you’ll have an infinite loop:

PHP for statement example

The following shows a simple example that adds numbers from 1 to 10:

  • First, initialize the $total to zero.
  • Second, start the loop by setting the variable $i to 1. This initialization will be evaluated once when the loop starts.
  • Third, the loop continues as long as $i is less than or equal to 10 . The expression $i <= 10 is evaluated once after every iteration.
  • Fourth, the expression $i++ is evaluated after each iteration.
  • Finally, the loop runs exactly 10 iterations and stops once $i becomes 11 .

Alternative syntax of the for statement

The for statement has the alternative syntax as follows:

The following script uses the alternative syntax to calculate the sum of 10 numbers from 1 to 10:

  • Use the PHP for statement to execute a code block in a specified number of times.

PHP for loop

Php tutorial index.

PHP for loop is very similar to a while loop in that it continues to process a block of code until a statement becomes false, and everything is defined in a single line.

php-for

Program Output:

  • Language Reference
  • Control Structures

(PHP 4, PHP 5, PHP 7, PHP 8)

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes: foreach (iterable_expression as $value) statement foreach (iterable_expression as $key => $value) statement

The first form traverses the iterable given by iterable_expression . On each iteration, the value of the current element is assigned to $value .

The second form will additionally assign the current element's key to the $key variable on each iteration.

Note that foreach does not modify the internal array pointer, which is used by functions such as current() and key() .

It is possible to customize object iteration .

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference . <?php $arr = array( 1 , 2 , 3 , 4 ); foreach ( $arr as & $value ) { $value = $value * 2 ; } // $arr is now array(2, 4, 6, 8) unset( $value ); // break the reference with the last element ?>

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset() . Otherwise you will experience the following behavior:

It is possible to iterate a constant array's value by reference: <?php foreach (array( 1 , 2 , 3 , 4 ) as & $value ) { $value = $value * 2 ; } ?>

Note : foreach does not support the ability to suppress error messages using @ .

Some more examples to demonstrate usage: <?php /* foreach example 1: value only */ $a = array( 1 , 2 , 3 , 17 ); foreach ( $a as $v ) { echo "Current value of \$a: $v .\n" ; } /* foreach example 2: value (with its manual access notation printed for illustration) */ $a = array( 1 , 2 , 3 , 17 ); $i = 0 ; /* for illustrative purposes only */ foreach ( $a as $v ) { echo "\$a[ $i ] => $v .\n" ; $i ++; } /* foreach example 3: key and value */ $a = array( "one" => 1 , "two" => 2 , "three" => 3 , "seventeen" => 17 ); foreach ( $a as $k => $v ) { echo "\$a[ $k ] => $v .\n" ; } /* foreach example 4: multi-dimensional arrays */ $a = array(); $a [ 0 ][ 0 ] = "a" ; $a [ 0 ][ 1 ] = "b" ; $a [ 1 ][ 0 ] = "y" ; $a [ 1 ][ 1 ] = "z" ; foreach ( $a as $v1 ) { foreach ( $v1 as $v2 ) { echo " $v2 \n" ; } } /* foreach example 5: dynamic arrays */ foreach (array( 1 , 2 , 3 , 4 , 5 ) as $v ) { echo " $v \n" ; } ?>

Unpacking nested arrays with list()

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

It is possible to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value.

The above example will output:

User Contributed Notes 2 notes

To Top

PHP Tutorial

Php advanced, mysql database, php examples, php reference, php foreach loop.

The foreach loop - Loops through a block of code for each element in an array or each property in an object.

The foreach Loop on Arrays

The most common use of the foreach loop, is to loop through the items of an array.

Loop through the items of an indexed array:

For every loop iteration, the value of the current array element is assigned to the variabe $x . The iteration continues until it reaches the last array element.

Keys and Values

The array above is an indexed array, where the first item has the key 0, the second has the key 1, and so on.

Associative arrays are different, associative arrays use named keys that you assign to them, and when looping through associative arrays, you might want to keep the key as well as the value.

This can be done by specifying both the key and value in the foreach defintition, like this:

Print both the key and the value from the $members array:

You will learn more about arrays in the PHP Arrays chapter.

Advertisement

The foreach Loop on Objects

The foreach loop can also be used to loop through properties of an object:

Print the property names and values of the $myCar object:

You will learn more about objects in the PHP Objects and Classes chapter.

The break Statement

With the break statement we can stop the loop even if it has not reached the end:

Stop the loop if $x is "blue":

The continue Statement

With the continue statement we can stop the current iteration, and continue with the next:

Stop, and jump to the next iteration if $x is "blue":

Foreach Byref

When looping through the array items, any changes done to the array item will, by default, NOT affect the original array:

By default, changing an array item will not affect the original array:

BUT, by using the & character in the foreach declaration, the array item is assigned by reference , which results in any changes done to the array item will also be done to the original array:

By assigning the array items by reference , changes will affect the original array:

Alternative Syntax

The foreach loop syntax can also be written with the endforeach statement like this

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

The Electric Toolbox Blog

PHP for loops and counting arrays

It’s well known that calling count($array) in a for loop in PHP is slower than assigning the count to a variable and using that variable in the for loop instead. However until recently, I wasn’t aware that the assignment to a variable can be done in the for loop itself and share this here in this post.

The "wrong" way

The following example loops through an array in the variable $array:

The count() function is called on each loop which adds extra unecessary overhead. Even if the array only has a couple of items in it processing will still take longer than assigning count() to a variable.

The "right" way

Here’s one way of doing it the "right" way:

The count is now assigned to the variable $j so the function is only called once.

Another way of doing the above is like so:

The assignment $j = count($array) is part of the for loop, separated by a comma from the $i = 0 assignment. It is only called once at the start of the loop. It is not necesssarily superior to the first "right" example above but it does reduce the number of lines of code by one and means the purpose of the variable is clearly for of the loop.

Benchmarking

Out of interest I created an array with 100 elements and looped through the "wrong" way and the "right" way (and the whole thing 10,000 times for measurement purposes); the "right" way averaged about .20 seconds on my test box and the "wrong" way about .55 seconds.

Obviously these sorts of micro-optimizations aren’t really going to have much of an effect on your own website (that .20 vs .55 seconds was looping through the test 10k times, remember) but it is interesting to see the differences.

Check Out These Related posts:

  • Bash For Loop
  • How to use an associative array with PHP’s str_replace function
  • Get unique array values with PHP
  • PHP for loops with multiple statements in each expression
  • PHP Tutorial
  • PHP Calendar
  • PHP Filesystem
  • PHP Programs
  • PHP Interview Questions
  • PHP IntlChar
  • PHP Image Processing
  • PHP Array Programs
  • PHP String Programs
  • PHP Formatter
  • Web Technology

Related Articles

  • Solve Coding Problems
  • PHP | Introduction
  • How to install PHP in windows 10 ?
  • How to Install PHP on Linux?
  • PHP | Basic Syntax
  • How to write comments in PHP ?
  • PHP | Variables
  • PHP echo and print
  • PHP | Data Types
  • PHP | Strings
  • Associative Arrays in PHP
  • Multidimensional arrays in PHP
  • Sorting Arrays in PHP 5

PHP Constants

  • PHP | Constants
  • PHP Constant Class
  • PHP Defining Constants
  • PHP | Magic Constants
  • PHP Operators
  • PHP | Bitwise Operators
  • PHP | Ternary Operator

PHP Control Statements

  • PHP | Decision Making
  • PHP switch Statement
  • PHP break (Single and Nested Loops)
  • PHP continue Statement

PHP | Loops

  • PHP while Loop
  • PHP do-while Loop
  • PHP for Loop
  • PHP | foreach Loop

PHP Functions

  • PHP | Functions
  • PHP Arrow Functions
  • Anonymous recursive function in PHP

PHP Advanced

  • PHP | Superglobals
  • HTTP GET and POST Methods in PHP
  • PHP | Regular Expressions
  • PHP Form Processing
  • PHP Date and Time
  • Describe PHP Include and Require
  • PHP | Basics of File Handling
  • PHP | Uploading File
  • PHP Cookies
  • PHP | Sessions
  • Implementing callback in PHP
  • PHP | Classes
  • PHP | Constructors and Destructors
  • PHP | Access Specifiers
  • Multiple Inheritance in PHP
  • Abstract Classes in PHP
  • PHP | Interface
  • Static Function in PHP
  • PHP | Namespace

MySQL Database

  • PHP | MySQL Database Introduction
  • PHP | MySQL ( Creating Database )
  • PHP Database connection
  • Connect PHP to MySQL
  • PHP | MySQL ( Creating Table )
  • PHP | Inserting into MySQL database
  • PHP | MySQL Select Query
  • PHP | MySQL Delete Query
  • PHP | MySQL WHERE Clause
  • PHP | MySQL UPDATE Query
  • PHP | MySQL LIMIT Clause

Complete References

  • PHP Array Functions
  • PHP String Functions Complete Reference
  • PHP Math Functions Complete Reference
  • PHP Filesystem Functions Complete Reference
  • PHP intl Functions Complete Reference
  • PHP IntlChar Functions Complete Reference
  • PHP Image Processing and GD Functions Complete Reference
  • PHP Imagick Functions Complete Reference
  • PHP ImagickDraw Functions Complete Reference
  • PHP Gmagick Functions Complete Reference
  • PHP GMP Functions Complete Reference
  • PHP Ds\Set Functions Complete Reference
  • PHP Ds\Map Functions Complete Reference
  • PHP Ds\Stack Functions Complete Reference
  • PHP Ds\Queue Functions Complete Reference
  • PHP Ds\PriorityQueue Functions Complete Reference
  • PHP Ds\Deque Functions Complete Reference
  • PHP Ds\Sequence Functions Complete Reference
  • PHP SPL Data structures Complete Reference
  • PHP Ds\Vector Functions Complete Reference
  • PHP DOM Functions Complete Reference
  • do-while loop
  • foreach loop
  • Initialization Expression : In this expression we have to initialize the loop counter to some value. for example: $num = 1;
  • Test Expression : In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of loop and go to update expression otherwise we will exit from the for loop. For example: $num <= 10;
  • Update Expression : After executing loop body this expression increments/decrements the loop variable by some value. for example: $num += 2;

php for loop assignment

Please Login to comment...

  • Web Technologies

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Tutorials Class - Logo

  • PHP All Exercises & Assignments

Practice your PHP skills using PHP Exercises & Assignments. Tutorials Class provides you exercises on PHP basics, variables, operators, loops, forms, and database.

Once you learn PHP, it is important to practice to understand PHP concepts. This will also help you with preparing for PHP Interview Questions.

Here, you will find a list of PHP programs, along with problem description and solution. These programs can also be used as assignments for PHP students.

Write a program to count 5 to 15 using PHP loop

Description: Write a Program to display count, from 5 to 15 using PHP loop as given below.

Rules & Hint

  • You can use “for” or “while” loop
  • You can use variable to initialize count
  • You can use html tag for line break

View Solution/Program

5 6 7 8 9 10 11 12 13 14 15

Write a program to print “Hello World” using echo

Description: Write a program to print “Hello World” using echo only?

Conditions:

  • You can not use any variable.

View Solution /Program

Hello World

Write a program to print “Hello PHP” using variable

Description: Write a program to print “Hello PHP” using php variable?

  • You can not use text directly in echo but can use variable.

Write a program to print a string using echo+variable.

Description: Write a program to print “Welcome to the PHP World” using some part of the text in variable & some part directly in echo.

  • You have to use a variable that contains string “PHP World”.

Welcome to the PHP World

Write a program to print two variables in single echo

Description: Write a program to print 2 php variables using single echo statement.

  • First variable have text “Good Morning.”
  • Second variable have text “Have a nice day!”
  • Your output should be “Good morning. Have a nice day!”
  • You are allowed to use only one echo statement in this program.

Good Morning. Have a nice day!

Write a program to check student grade based on marks

Description:.

Write a program to check student grade based on the marks using if-else statement.

  • If marks are 60% or more, grade will be First Division.
  • If marks between 45% to 59%, grade will be Second Division.
  • If marks between 33% to 44%, grade will be Third Division.
  • If marks are less than 33%, student will be Fail.

Click to View Solution/Program

Third Division

Write a program to show day of the week using switch

Write a program to show day of the week (for example: Monday) based on numbers using switch/case statements.

  • You can pass 1 to 7 number in switch
  • Day 1 will be considered as Monday
  • If number is not between 1 to 7, show invalid number in default

It is Friday!

Write a factorial program using for loop in php

Write a program to calculate factorial of a number using for loop in php.

The factorial of 3 is 6

Factorial program in PHP using recursive function

Exercise Description: Write a PHP program to find factorial of a number using recursive function .

What is Recursive Function?

  • A recursive function is a function that calls itself.

Write a program to create Chess board in PHP using for loop

Write a PHP program using nested for loop that creates a chess board.

  • You can use html table having width=”400px” and take “30px” as cell height and width for check boxes.

Chess-board-in-PHP-using-for-loop

Write a Program to create given pattern with * using for loop

Description: Write a Program to create following pattern using for loops:

  • You can use for or while loop
  • You can use multiple (nested) loop to draw above pattern

View Solution/Program using two for loops

* ** *** **** ***** ****** ******* ********

Simple Tips for PHP Beginners

When a beginner start PHP programming, he often gets some syntax errors. Sometimes these are small errors but takes a lot of time to fix. This happens when we are not familiar with the basic syntax and do small mistakes in programs. These mistakes can be avoided if you practice more and taking care of small things.

I would like to say that it is never a good idea to become smart and start copying. This will save your time but you would not be able to understand PHP syntax. Rather, Type your program and get friendly with PHP code.

Follow Simple Tips for PHP Beginners to avoid errors in Programming

  • Start with simple & small programs.
  • Type your PHP program code manually. Do not just Copy Paste.
  • Always create a new file for new code and keep backup of old files. This will make it easy to find old programs when needed.
  • Keep your PHP files in organized folders rather than keeping all files in same folder.
  • Use meaningful names for PHP files or folders. Some examples are: “ variable-test.php “, “ loops.php ” etc. Do not just use “ abc.php “, “ 123.php ” or “ sample.php “
  • Avoid space between file or folder names. Use hyphens (-) instead.
  • Use lower case letters for file or folder names. This will help you make a consistent code

These points are not mandatory but they help you to make consistent and understandable code. Once you practice this for 20 to 30 PHP programs, you can go further with more standards.

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interop Group.

Experiment with Basic PHP Syntax Errors

When you start PHP Programming, you may face some programming errors. These errors stops your program execution. Sometimes you quickly find your solutions while sometimes it may take long time even if there is small mistake. It is important to get familiar with Basic PHP Syntax Errors

Basic Syntax errors occurs when we do not write PHP Code correctly. We cannot avoid all those errors but we can learn from them.

Here is a working PHP Code example to output a simple line.

Output: Hello World!

It is better to experiment with PHP Basic code and see what errors happens.

  • Remove semicolon from the end of second line and see what error occurs
  • Remove double quote from “Hello World!” what error occurs
  • Remove PHP ending statement “?>” error occurs
  • Use “
  • Try some space between “

Try above changes one at a time and see error. Observe What you did and what error happens.

Take care of the line number mentioned in error message. It will give you hint about the place where there is some mistake in the code.

Read Carefully Error message. Once you will understand the meaning of these basic error messages, you will be able to fix them later on easily.

Note: Most of the time error can be found in previous line instead of actual mentioned line. For example: If your program miss semicolon in line number 6, it will show error in line number 7.

Using phpinfo() – Display PHP Configuration & Modules

phpinfo()   is a PHP built-in function used to display information about PHP’s configuration settings and modules.

When we install PHP, there are many additional modules also get installed. Most of them are enabled and some are disabled. These modules or extensions enhance PHP functionality. For example, the date-time extension provides some ready-made function related to date and time formatting. MySQL modules are integrated to deal with PHP Connections.

It is good to take a look on those extensions. Simply use

phpinfo() function as given below.

Example Using phpinfo() function

Using-phpinfo-Display-PHP-Configuration-Modules

Write a PHP program to add two numbers

Write a program to perform sum or addition of two numbers in PHP programming. You can use PHP Variables and Operators

PHP Program to add two numbers:

Write a program to calculate electricity bill in php.

You need to write a PHP program to calculate electricity bill using if-else conditions.

  • For first 50 units – Rs. 3.50/unit
  • For next 100 units – Rs. 4.00/unit
  • For next 100 units – Rs. 5.20/unit
  • For units above 250 – Rs. 6.50/unit
  • You can use conditional statements .

php for loop assignment

Write a simple calculator program in PHP using switch case

You need to write a simple calculator program in PHP using switch case.

Operations:

  • Subtraction
  • Multiplication

simple-calculator-program-in-PHP-using-switch-case

Remove specific element by value from an array in PHP?

You need to write a program in PHP to remove specific element by value from an array using PHP program.

Instructions:

  • Take an array with list of month names.
  • Take a variable with the name of value to be deleted.
  • You can use PHP array functions or foreach loop.

Solution 1: Using array_search()

With the help of  array_search()  function, we can remove specific elements from an array.

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [3]=> string(5) “april” [4]=> string(3) “may” }

Solution 2: Using  foreach()

By using  foreach()  loop, we can also remove specific elements from an array.

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [3]=> string(5) “april” [4]=> string(3) “may” }

Solution 3: Using array_diff()

With the help of  array_diff()  function, we also can remove specific elements from an array.

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [2]=> string(5) “march” [4]=> string(3) “may” }

Write a PHP program to check if a person is eligible to vote

Write a PHP program to check if a person is eligible to vote or not.

  • Minimum age required for vote is 18.
  • You can use PHP Functions .
  • You can use Decision Making Statements .

Click to View Solution/Program.

You Are Eligible For Vote

Write a PHP program to calculate area of rectangle

Write a PHP program to calculate area of rectangle by using PHP Function.

  • You must use a PHP Function .
  • There should be two arguments i.e. length & width.

View Solution/Program.

Area Of Rectangle with length 2 & width 4 is 8 .

  • Next »
  • PHP Exercises Categories
  • PHP Top Exercises
  • PHP Variables
  • PHP Decision Making
  • PHP Functions
  • PHP Operators

Programming Code Examples

How to Write Nested for loop in PHP (with for Loop Examples) 5 min read

In this post, we will learn about nested for loop in PHP and different ways to use them in a program.

Table of Contents

What is for loop

The  for  keyword indicates a loop in PHP. The  for  loop executes a block of statements repeatedly until the specified condition returns false.

What is Nested for loop

A  nested loop  is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a  break  within either the inner or outer loop would interrupt this process.

Nested for loop Examples

Example 1: Write a PHP script using nested for loop that creates a chess board

Write a PHP script using nested for loop that creates a chess board

Example 2: Create a script to construct the following pattern, using nested for loop

Create a script to construct the following pattern, using nested for loop

Example 3: Write a program to create given pattern with * using for loop.

write a program to create given pattern with * using for loop.

Example 4: write a program to create given pattern with * using for loop.

php for loop assignment

Example 5: Create A Script To Construct The Following Pattern, Using A Nested For Loop. ** * * * ** * * * * * * * * ***

Create A Script To Construct The Following Pattern, Using A Nested For Loop. ** * * * ** * * * * * * * * ***

You may also like

php for loop assignment

Calculating the Surface Area and Volume of a Cylinder...

Php script to demonstrate arithmetic operators..., php script to display a welcome message, how to search using a stored procedure in php, how to retrieve an array of ids from a collection in..., how to get php errors to display, how to get the last character of a string in php, php get last 4 characters from a string example, how to calculate average of numbers in php, write a php program to print all natural numbers from....

[…] You may also like: PHP Nested for loop Examples […]

Leave a Comment X

Notify me of follow-up comments by email.

Notify me of new posts by email.

IMAGES

  1. php

    php for loop assignment

  2. PHP FOR LOOP EXAMPLES (STEP BY STEP For Beginners)

    php for loop assignment

  3. PHP Beginner 4

    php for loop assignment

  4. PHP Loops Tutorial

    php for loop assignment

  5. For Loop in PHP

    php for loop assignment

  6. PHP for loop: Explained with 6 examples

    php for loop assignment

VIDEO

  1. php://input

  2. PHP Loop and Array

  3. PHP Loop, Arabic

  4. 36- Programming

  5. PHP structure

  6. Video19 While Loop Assignment 2

COMMENTS

  1. PHP for loop

    Write a PHP script that creates the following table using for loops. Add cellpadding="3px" and cellspacing="0px" to the table tag. Click me to see the solution 9. Write a PHP script using nested for loop that creates a chess board as shown below. Use table width="270px" and take 30px as cell height and width.

  2. PHP for loops

    Syntax for (expression1, expression2, expression3) { // code block } This is how it works: expression1 is evaluated once expression2 is evaluated before each iterarion expression3 is evaluated after each iterarion Example Get your own PHP Server Print the numbers from 0 to 10: for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>"; }

  3. for loop with assignment in php

    for loop with assignment in php - Stack Overflow for loop with assignment in php Ask Question Asked 10 years, 6 months ago Modified 10 years, 6 months ago Viewed 257 times Part of PHP Collective -3 When I want to iterate an array I usually do: foreach ($array as $a) { //do something with $a }

  4. PHP: for

    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.

  5. PHP: Assignment

    PHP Manual Language Reference Operators Change language: Submit a Pull Request Report a Bug Assignment Operators ¶ The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").

  6. PHP for Loop

    Example 1: The following code shows a simple example using for loop. PHP <?php for( $num = 0; $num < 20; $num += 5) { echo $num . "\n"; } ?> Output 0 5 10 15 Example 2: The following code shows another example of for loop. PHP <?php for( $num = 1; $num < 50; $num++) { if($num % 5 == 0) echo $num . "\n"; } ?> Output 5 10 15 20 25 30 35 40 45

  7. PHP for

    Summary: in this tutorial, you will learn about PHP for statement to execute a block of code repeatedly.. Introduction to PHP for statement. The for statement allows you to execute a code block repeatedly. The syntax of the for statement is as follows: <?php for (start; condition; increment) { statement; } Code language: HTML, XML (xml). How it works. The start is evaluated once when the loop ...

  8. PHP for loop

    PHP for loop is a powerful feature that allows you to execute a block of code repeatedly until a certain condition is met. In this tutorial, you will learn how to use PHP for loop with examples and exercises. You will also see how PHP for loop differs from other programming languages such as Python, Java, and C.

  9. PHP Loops

    Here, you will find Assignments & Exercises related to PHP Loops. Once you learn PHP Loops, it is important to practice on these to understand concepts well. These PHP Loop exercises contain programs on PHP for Loop, while Loop, mathematical series, and various string pattern designs. All PHP exercises are available in the form of PHP problem ...

  10. PHP: foreach

    The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

  11. PHP for loops

    The foreach loop - Loops through a block of code for each element in an array or each property in an object. The foreach Loop on Arrays The most common use of the foreach loop, is to loop through the items of an array. Example Get your own PHP Server Loop through the items of an indexed array:

  12. How to Use the PHP for Loop

    The for loop continued to iterate until it no longer matched the condition within the for loops parenthesis. The value of x is 1. The value of x is 2. Skipping. The value of x is 4. The value of x is 5. Conclusion. You will likely find yourself using for loops a lot in PHP programming, so they are incredibly important to understand. I hope that ...

  13. PHP for loops and counting arrays

    Benchmarking. Out of interest I created an array with 100 elements and looped through the "wrong" way and the "right" way (and the whole thing 10,000 times for measurement purposes); the "right" way averaged about .20 seconds on my test box and the "wrong" way about .55 seconds.

  14. PHP For Loop

    The php for loop is similar to the java/C/C++ for loop. The parameters of for loop have the following meanings: initialization - Initialize the loop counter value. The initial value of the for loop is done only once. This parameter is optional. condition - Evaluate each iteration value. The loop continuously executes until the condition is false.

  15. PHP

    Syntax : for (initialization expression; test condition; update expression) { // code to be executed } In for loop, a loop variable is used to control the loop. First initialize this loop variable to some value, then check whether this variable is less than or greater than counter value.

  16. PHP All Exercises & Assignments

    Description: Write a Program to display count, from 5 to 15 using PHP loop as given below. Rules & Hint You can use "for" or "while" loop You can use variable to initialize count You can use html tag for line break View Solution/Program <?php $count = 5; while($count <= 15) { echo $count; echo "<br>" ; $count++; } ?> Tutorials Class - Output Window

  17. How to Write Nested for loop in PHP (with for Loop Examples)

    1 2 3 4 5 6 for (initializer; condition; iterator) { //code block } What is Nested for loop A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion.

  18. San Antonio traffic: Loop 1604 North Expansion Project closures

    TxDOT's closures at the Loop 1604 and I-10 interchange will run from 9 p.m. Thursday, Feb. 22, 2024, to 5 a.m. Friday, Feb. 23, 2024. San Antonio Express-News Hearst Newspapers Logo Skip to main ...

  19. How to assign PHP variables from foreach loop output?

    How to assign PHP variables from foreach loop output? Ask Question Asked 9 years, 9 months ago Modified 3 years, 10 months ago Viewed 14k times Part of PHP Collective 0 I have a foreach loop which prints out the usernames of all entries in a database table like so: foreach ($results as $result) { echo $result->username; echo '<br>'; }

  20. PHP for loop variables unassigned

    1 I have a for loop that writes the results of a query into a table. I have a variable ( $rID_s) that is being assigned from a value in the query. For some reason, it omits the first iteration. I have a variable for the total rows of the query and it is assigning the correct number.