Reputation: 689
Currently have this.
for ($i = 0; isset($house[$i]); $i++) {
print $i++;
}
and it returns something like this. 0,1,2,3,4,5,6,7,8,9
I want it to return that same format, but replace the initial 0
with a 1
then count forward from there, how can I achieve this?
Upvotes: 1
Views: 480
Reputation: 2029
Try this code ! Replace the i++ with i+1
for ($i = 0; isset($house[$i]); $i++)
{
print $i + 1 ;
}
Upvotes: 1
Reputation: 26153
Write it using pre-increment
for ($i = 0; isset($house[$i]); ) {
print ++$i;
}
Upvotes: 2
Reputation: 133360
Be careful You are increasing the count two time .. one in the for and one in the print
for ($i = 0; isset($house[$i]); $i++) { // first increment add and the onf a iteration
print $i++; // second increment
}
you should simply
for ($i = 0; isset($house[$i]); $i++) {
print $i;
}
and if you need start from 1 for count the iteration set $1= 1
for ($i = 1; isset($house[$i]); $i++) {
print $i;
}
Upvotes: 0
Reputation: 13509
What about -
for ($i = 0; isset($house[$i]); $i++) {
if ($i = 0){
print 1;}
else{
print $i++;}
}
Upvotes: 0
Reputation: 36
You can use $i + 1
instead of $i++
$i++
is equals to $i = $i + 1
, and it's not correct way in your case
Upvotes: 1
Reputation: 1632
Change:
for ($i = 0; isset($house[$i]); $i++) {
To:
for ($i = 1; isset($house[$i]) + 1; $i++) {
You basically want to start the counter at 1. And the "+ 1 " adds 1 to the ending limit to allow it to reach 9
Upvotes: -1