

Here is an example: " Īs an alternative, you can replace the opening curly bracket of the while condition with a colon and replace the closing curly bracket with the endwhile keyword. $counterĭelimiting the statement is a requirement if it is made of various lines of code.

You can delimit the body with curly brackets. For this reason, its body can or must be delimited. $counter ++Īs seen with other conditional statements, a while loop may include many statements in its body. If you are counting some numbers as in our example, you can ask the parser to keep incrementing the number until a new value reaches a threshold. This means that you must specify a condition by which the loop should stop. Such a statement would run forever (or until the computer memory or the processor cannot take it anymore). Here is an example (don't run the following program): Number: ". That statement will execute as long as the parenthesed condition is true. In the body of the condition, create a statement. In the parentheses of while, include a Boolean expression that involves the variable and can be assessed as being true or false. For example, if you are planning to count some numbers, you can specify by what value to start. The formula to follow is: while( Condition)īefore stating the condition, you should have a primary expression against which the condition would be checked. This can be done using the while keyword. PHP provide a means to perform an action as long as a certain condition is true. This would be done as follows: for( ) statement

The parentheses must still include their two required semicolons. In fact, you can create a loop whose three factors are defined outside the parentheses. If you have a way to increment the variable in the body of the loop, you can omit it in the parentheses of a for loop. If you already have the starting point before the for loop is accessed, you can omit that starting point but its semicolon must be represented. Here is an example: " Īs seen above the parentheses of a for loop are made in three sections. To indicate the end of the loop, use the endfor keyword.Īlso, the statement(s) must end with a semicolon.
#PHP FOR LOOP COUNTING LETTERS CODE#
The delimiting is required if the body of the loop is made of various lines of code.Īs an alternative, you can enf the line with the for code with a colon. You can include any HTML code anywhere in the loop as longĪs you delimit the loop code with its own PHP delimitiers. In this case, the statement must end with a semicolon. That body can be delimited with curly brackets.

The body of a loop is the section from its closing parentheses to the end of its statement. In this case, the starting point must be high and the condition should include a comparison for higher value. If you want to count down, decrement the value of the variable in the parentheses. The above example counted the numbers up. In the body of the condition, create the desired statement. After its required semicolon, specify a way to move to the next value. After its required semicolon, create a Boolean expression that specify a condition by which the coundting will stop. In the parentheses, declare a variable and initialize with a value by which to start. The for keyword is required and it has parentheses. The formula to perform this opeation is: for($ variable-name = start-value condition $ variable-name++ | $ variable-name-) The most fundamental consists ofĬounting numbers from one starting point to an ending value while incrementing the number. There are various types of loops you can use. Looping consists of going through a list of items and doing something about each element of the list.
