Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

i had tried diff loops all works but i need to display same results out side the loops any one help me

PHP
//while loop
$x = 1;
while ($x <= 9)
{
    $numZero = 1 - strlen($x);
    echo $x;
	echo '';
    $x++;
}


//for loop
for($i=1;$i<=10;$i++){
echo $i.'';
}
Posted
Updated 23-Oct-14 20:29pm
v3
Comments
Maciej Los 24-Oct-14 2:09am    
What would you like to display outside the loop?
Use echo command outside the loop. That's all!
Arun-23 24-Oct-14 2:11am    
i had used but it displays only last value
Maciej Los 24-Oct-14 2:17am    
Use 'Reply' widget to post comment as a reply.
Do you want to display all values?
Arun-23 24-Oct-14 2:33am    
yes same has inside the loop
Maciej Los 24-Oct-14 2:45am    
Have you seen my answer?

1 solution

You need to declare variable, which you can use inside loop to build string.

Please, have a look here:
http://stackoverflow.com/questions/124067/php-string-concatenation-performance[^]
http://stackoverflow.com/questions/4218236/stringbuilder-for-php[^]

To convert integer to string, use:
PHP
$str = (string) $int;


Finaly...
PHP
$str = '';
//for loop
for($i=1;$i<=10;$i++){
$str .= (string)$i;
}
echo $str;


Note: i'm not php programmer ;)

Good luck!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-Oct-14 3:22am    
5ed. Being not a PHP programmer is itself a good characteristic. :-)
—SA
Maciej Los 24-Oct-14 3:24am    
:laugh:
Thank you very much, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900