php


PHP return result from a loop


I have a PHP script with a while loop and I need to return the result as a single variable !
Code schema :
$count = "0";
while($count < "4")
{
echo $count;
$count = $count + "1";
}
It will return : 0123,
What I want to have a variable with the value : 0123
I also tried :
$count = "0";
while($count < "4")
{
$result .= $count;
$count = $count + "1";
}
echo $result;
But it will show the result like : 0010120123
I know I can use array to get all results outside the loop, but I want to have all results as a single variable but not in an array !
Update :
I found the problem :S stupid mistake ! I used :
echo $result
Inside my loop :S
So
$result .= $count;
Work fine :)
$count = "0";
$var = "";
while($count < "4")
{
$var .= $count;
$count++;
}
echo $var;
Here is a function that can do it to you
Pass 4 in the parametrs to match your example
function abc($limit){
$count = "";
for($i=0; $i<$limit; $i++)
{
$count .= $i;
}
return $count;
}
echo abc(4); // outputs 01234
Try this, it should do it:
<?php
$count = "0";
$var = "";
while($count < "4")
{
$var .= $count;
$count++;
}
echo $var;
?>

Related Links

Composer, class within my package is not being autoloaded in application using it
How to match any characters without space and don't array usefull things
How do I optimize memory usage in PHP for this function?
Using php file_get_contents() does not work when using ajax
GROUP BY selection with filling up missing times
Get value of an multidimensional array [duplicate]
Calling C/C++ library function from PHP - How to send a -std=c+11 to compiler
if an array default is true, how to display his content?
JSON array containing an image stored as a blob
highcharts php array unset/reset keys after sorting
Count group by in laravel
Why php function doesn't work inside second <?php ?>
Codeigniter base_url() function, go to parent folder
How can I display an amount of units in my table with the same value?
query mysql database by field and return all occurrences
How to include a PHP variable inside a MySQL insert statement with where condition

Categories

HOME
knockout.js
firebase
gaming
windows-8.1
livecode
mousemove
plpgsql
embedded-resource
celery
semantic-web
nsis
mathematical-optimization
header
swift2
dji-sdk
share
graphql-js
django-rq
dma
web-deployment
tomcat8
office-ui-fabric
coroutine
adminlte
opam
spring-ldap
unity-container
maven-surefire-plugin
amazon-sns
channel
software-packaging
mod-wsgi
nurbs
laravel-4.2
mdns
instructions
knights-tour
counting
nest
db2-luw
spinner
djcelery
vb.net-to-c#
ejml
fluid-mac-app-engine
httphandler
realstudio
viewmodel
windows-media-player
libreoffice-writer
apache-modules
adobe-reader
cognos-tm1
dcount
p6spy
coovachilli
dojo-build
powerpoint-2013
persistent-object-store
ibm-was
highest
screen-orientation
canopy
karma-coverage
whitespace-language
signed
sql-processor
microsoft-expression-web
wicket-1.5
dmake
rpg
flipboard
pre
jomsocial
strdup
hyphen
infinite
iirf
google-code-prettify
ember-table
postgres-xc
ldif
session-0-isolation
lynx
coordinate
uipangesturerecognizer
nsfont
mkannotation
filelock
database-deadlocks
gaelyk
hungarian-notation
reliability
iphone-sdk-4.3
.net-client-profile
fuzzy-comparison
efs
error-detection

Resources

Encrypt Message