John Davidson

PHP: Updating double-quoted string inside a while loop

0 comments
Message:


I'm expecting this to print from "Count 1" to "Count 9". But the $str variable is not updating inside the while loop. It just prints "Count 1" nine times. In my use-case it's not possible to initiate $str inside the while loop therefore I'm looking for a dynamic update of the $str var inside the while loop.


    $p   = 1;
$str = "Count {$p}";
while ( $p < 10 ) {
$p++;
echo $str . PHP_EOL;
}


My Answer:
1 to 10:

php
$count = 1;

while ($count <= 10) {
echo "The current count is: $count\n";
$count++;
}


This code will output:


The current count is: 1
The current count is: 2
The current count is: 3
The current count is: 4
The current count is: 5
The current count is: 6
The current count is: 7
The current count is: 8
The current count is: 9
The current count is: 10

Rate this post

3 of 5 based on 1290 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us