PHP for-loop with integer

print 1 to 10 using for loop in php
php foreach associative array
php while loop array
foreach loop
count loop php
nested for loop in php
php increment loop
php end foreach

I start learning PHP(beginners) and got some trouble with for-loop. Need to Create a for-loop that iterates from 0 to (including) 16. Add the integer value for each iteration to a string and separate each item with a "," (comma). Neet answer with the final string without an ending ",".

So long i came.

 <?php
$string = "";
for ($x = 0; $x <= 16; $x++) {
    $string=$x ",";
} 
echo $string;
?>

Sorry guys for your time.

Have nice day !


There are many ways to do this:

METHOD 1

$output = implode(',', range(0,16));  
echo $output;   //0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16

Breakdown: From inside out The range() function is used to create an array (from 0 to 16) and then the implode() function is used to join the elements of the array using what we call a glue and in our case that glue is a ,.

METHOD 2 - FOREACH LOOP

This could be done using the traditional for loop but I always favor the foreach loop because it has a cleaner syntax and it's easier to understand.

<?php
    $arr = range(0,16);  //Create an array from 0 - 16
    $output = "";       //Create an empty string to store the ouput
    foreach($arr as $item){   //Loop through each item of the array
        $output .= $item.',';   //Concatenate a comma to the current item and assign it to the output
    }

    echo rtrim($output, ',');   //trim the last comma to the right using `rtrim()` and then `echo` the output  //0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16

?>

METHOD 3 - FOR LOOP

<?php

    $output = "";

    for($i = 0; $i <= 16; $i++) {
        $output .= $i.',';
    }

    echo rtrim($output, ',');
?>

loops in PHP, 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​  PHP for-loop with integer. I start learning PHP(beginners) and got some trouble with for-loop. Need to Create a for-loop that iterates from 0 to (including) 16. Add the integer value for each iteration to a string and separate each item with a "," (comma).


Make use of trim() after completion of concatination

$string = "";
for($i =0; $i<=16;$i++)
{
   $string.=$i.",";
} 
$string = trim($string,",");
echo $string;

You can also get same result by single line of code as follows-

echo implode(",", range(0, 16));

foreach - Manual, PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value. For example: <?php $  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.


You only need to add the comma when (if) $x is not the last number in the "loop":

<?php

$string = "";

for ($x = 0; $x <= 16; $x++) {
    $string .= $x;
    if ($x != 16 ) {
        $string .= ",";
    }
}

echo $string;

PHP for loops, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  A Loop in PHP is an Iterative Control Structure that involves executing the same number of code a number of times until a certain condition is met. PHP For, ForEach, While, Do While loop PHP Loop: For, ForEach, While, Do While [Example]


PHP Loops, Loops are used to execute the same block of code again and again, as long as a certain condition is true. In PHP, we have the following loop types: while - loops  Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types. for − loops through a block of code a specified number of times. while − loops through a block of code if and as long as a specified condition is true.


PHP for-loop with integer, I start learning PHP(beginners) and got some trouble with for-loop. Need to Create a for-loop that iterates from 0 to (including) 16. Add the integer value for each  PHP Loops. In this tutorial you will learn how to repeat a series of actions using loops in PHP. Different Types of Loops in PHP. Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort.


PHP Loop: For, ForEach, While, Do While [Example], “initialize” usually an integer; it is used to set the counter's initial value. “condition” the condition that is evaluated for each php execution. If it  PHP Integer. An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. Rules for integers: An integer must have at least one digit. An integer must not have a decimal point. An integer can be either positive or negative.