This question already has answers here:
Pre-incrementation vs. post-incrementation
(3 answers)
Closed 8 years ago.
I have checked for loop these two ways, but given same output:
1: Increment use ++$i
for($i=0; $i<10; ++$i){
echo $i;
}
2: Increment use $i++
for($i=0; $i<10; $i++){
echo $i;
}
These both code gave this output:
0 1 2 3 4 5 6 7 8 9
We learn ++$i mean pre increment, $i++ mean post increment,
why that post and pre increment not work? can someone please explain me? thank you.
Value of $i++ and ++$i will be same after evaluation.
As $i++ first evaluate value of $i then increment $i.
And
++$i first increment then evaluate value of $i.
Here in for loop, it follows step
initialization
test condition(if true then execute body/else exit)
increment/decrement
As again its working for a new line, either you use $i++ or ++$i it will be the same.
But if you use it in between the for loop you can see the difference.
Check Link for more details
for example
$i++; // Or ++$i;
echo $i;
It will give same value in both condition.
But if you use echo $i++; or echo ++$i then you will find difference.
Pre- and post- incrementing only make a difference when there are other operations happening in the same statement. For example:
$i = 0;
echo ++$i;
Will return 1, as opposed to this:
$i = 0;
echo $i++;
Which will return 0. In the latter case, the increment happens after the echo.
In your original example, the entire statement being executed is either ++$i or $i++. Either way, the order doesn't matter because nothing else is happening.
Related
This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
How to prevent for loop from exceeding condition?
Loop below returns 25. I want it to return 20.
for ($i = 5; $sum < 23;)
{
echo $sum += $i;
}
This loop is just an example. There will be variables with any value in place of 5 and 23
You have to write your loop differently.
A for loop is designed to run until the conditions evaluates false. In your case, the loop at sum = 20 will still run, becuase sum < 23 evaluates to true. So if you want 20, simply write $sum < 20.
Or if I give it a second thought, you may want to do it like that:
<?PHP
$sum = 0;
for($i = 5; ($sum+$i) < 23;)
{
$sum += $i;
}
echo $sum;
?>
I know this below statement works
for ($i= 0; $i=<10; $i++)
will output till 0 to 10
but when I write this code
for ($i=0; $i=10; $i++)
it print 10 for unlimited times... why why it not print 0 to 10...
what error I have done to get the result 0 to 10 for it....
The middle term in a for loop is the condition that says whether the loop should continue running. i=10 assigns 10 to i, and it also evaluates to the number 10, which is not zero, so it's considered true. Since the loop's condition is always true, it never stops running.
because i=10 always be true i==10 will be good
The second statement in the for loop is the condition. When its not true it will leave the loop. i=10 is not a comparator. i will be set to 10 every round that loop goes and because it "works" its resulting in true. i == 10 would be a comparator but i would never be 10 in its first round.
if $i< or =10 Will perform,if $i> 10 will break,each time $i will +1 you can set the $i begin to value or set the maximum to achieve the result that you want.
" = " sign is an assignment operator and can't be used as conditional where as >= , == , <= such operators are conditional, so while checking for a condition you should not use " = "
please write like this below
for($i=0; $i <= 10; $i++)
{
echo $i;
}
Case 1 loop inside another loop Can I assign both the $i variable for incrementing?
for($i=0; $i < 10; $i++)
{
for($i=0; $i < 5; $i++)
{
echo "You are too cute";
}
}
Case 2 : if it's not inside, Could I declare $i for both?
like this
for($i=0; $i < 10; $i++)
{
}
for($i=0; $i < 5; $i++)
{
}
There are already some answers that are just fine, but here's a slightly different perspective.
It depends on what you mean by "can". You can do this in the sense that it is syntactically correct PHP code.
for($i=0; $i < 10; $i++)
{
for($i=0; $i < 5; $i++)
{
echo "You will never see this text in your browser.";
}
}
But because a loop does not introduce a new variable scope in PHP, it creates an infinite loop.
The outer loop will execute once, then the inner loop will reset $i to 0, increment it to 5, return control to the outer loop, which will execute once, immediately causing the inner loop to start again, resetting $i to 0 and incrementing it to 5, and so on, forever (at least until your script times out). The outer loop can never end because the inner loop affects $i so that it can never satisfy the exit condition of the outer loop.
In other words, just use a different variable for the inner loop. Removing one integer variable is not going to be a noticeable optimization of your code, if that's what you're concerned about.
In the second example, there's no reason not to reuse $i.
Case 1: It will you get a really odd result, check it out here
How to do it properly? Check that out here
for($i=0; $i < 10; $i++){
for($k=0; $k < 5; $k++){
echo "1)".$i." 2)".$k."\n";
}
echo "\n";
}
Case 2: Works fine as stated in above comments and other answer. But, may I also add that in for instance this example.
for($i=0; $i < 10; $i++){
echo $i."\n";
}
echo "\n Outside the loop: ".$i." \n";
for($i=0; $i < 5; $i++){
echo $i."\n";
}
You can acces $i still after the loop has happend. The reason why you can use $i again is because you are declaring it $i =0; again, without interest toward another loop that is currently running (as is happening in case 1).
You can test this example here
Case 1 : No, you cant. you need to create variable individually for each loop.
for($i=0; $i < 10; $i++)
{
for($j=0; $j < 5; $j++)
{
echo "You are too cute";
}
}
Case 2 : Yes you can.
for($i=0; $i < 10; $i++)
{
echo "You are too cute";
}
for($i=0; $i < 5; $i++)
{
echo "You are too cute";
}
Case 1:
Short answer No you can't .
Long Answer .
First you need to understand what actually a variable is and How that Loop actually works .
Each and every variably is actually a reference to memory. In you example you have created a variable named $i and it can't be greater or equal 10 after incrementing value by one.
In the machine level it is translated to an address in the memory. say for example $i points to a random address 0xF25 When ever you loop it and incrementing it, the next address becomes 0xF30.
When ever you write a for loop, compiler automatically assigns a fixed memory address and that address it limited to your variable scope.
What compiler does is, it creates a table for that token($i). In simple form Look below an example
$i(This is the token ) -> 0xF25 (This is the value)
This value is updated when you do $i++
In nested Loop compiler assigns same table(though outer loop cant access inner loop variables). If compiler puts same variable for inner loop, it will be contradictory. Because inner loop may start from memory address 0xE21. In that case when your outer loop increment value by One it will be 0xE22 but as discussed above it needs to be 0xF30 .
That is why compiler does not allow this and we need to use CASE 2 example.
See the following code snippet
$i=1;
echo $i.($i++);
in a quick, I thought the result would be 12 but the actual result is 21.
also
echo $i,$i++;
I thought it would be 12 but its 11 .
echo ($i = ($i++)); //result is 1
echo ($i = ($i+1)); //result is 2
But why?
When a variable is not involved into any arithmetic operation (like your first $i), PHP will not create a temporary variable.
Thus, your first $i will be evaluated at the end of the statement, when $i++ has already been executed.
To prevent this, you can still write:
echo ($i += 0).($i++);
But this is obviously not a good coding practice.
EDIT: When you use , it is actually syntactic sugar to shorten two PHP statements. It is strictly equivalent to:
echo $i;
echo $i++;
Since incrementation is executed after the last statement, 11 is indeed the result.
First example
Code in brackets is evaluated first - in this case ($i++). The value of $i is taken (1) and then the variable is incremented to 2. So you then have this, where $i is 2.
echo $i . '1'
From this, the value of $i is substituted in, and you get '2' . '1', which is concatenated to give '21'.
Second example
It's easier to rewrite this to clear up the , separator. The line echo $i, $i++; is equivalent to:
echo $i;
echo $i++;
The first line obviously outputs 1, and the second will output that same value, then increment $i (++ is the post-increment operator). If you were to put another echo $i; at the end, it would output 2.
As per the PHP documentation stated at: Operator Precedence
First Case
$i=1;
echo $i.($i++);
$i is initialized to value 1. Now, ++ follows a higer precedence than . and it has right-associative. This means your $i++ will be evaluated first. In this case , the value of $i++ will be 1 and the next value of $i will get incremented to 2. hence $i is 2
Now . has the next precendence after ++, which is left-associative. hence it will evaluate values starting from left.
so $i=2 and $i++ =1, hence the output 21
Second Case
$i=1;
echo $i,$i++;
Here, there is only one operator ++. Hence the need for comparision of precedence doesn't arise. Hence, it will be evalauted by default standard of left-associative. $i = 1, $i++ = 1. Hence 11
Third Case
echo ($i = ($i++)); //result is 1
In this case, now = is an assignment operator and is right-associative, so $i++ = 1. And since it is an assignment operator value of $i++ will be stored in $i. hence echo ($i = 1); which will result in output being 1.
Fourth Case
echo ($i = ($i+1)); //result is 2
Again, this will be right-associative, so $i+1 = 2. hence echo ($i = 2); which will result in output being 2.
Firstly for the second place it uses $i eq 1
Then it increases it to 2;
So for the first place it uses 2 and for the second - 1
i have following code for loop
for ($i=0; $i<=(count($subusers)-1); ++$i) {
is there a reason to use ++$i instead of $i++ if latter doing same thing?
In a for loop, it doesn't matter since you're not doing anything with the returned value.
However you should still note the difference between ++$i and $i++, which is that $i++ returns $i and ++$i returns $i+1.
For example...
$i=0;
echo $i++; //0
echo ++$i; //2
++$i is a micro-optimisation, it executes fractionally faster than $i++. However, unless the $subusers array is being changed within the loop so that count($subusers) can change from one iteration to the next, then any slight positive gain in speed is being negated (and then some) by counting the number of array entries every iteration.
Note that $i++ and ++$i would both execute at the end of each iteration of the loop. It isn't the same as initialising $i to 1 rather than to 0.
In this case there is no difference because you are in a loop.
I would suggest you read up a bit on post and pre incrementation since it is always one of the favorite questions in interviews ^^
if you do i++, the value of i is first used then incremented
if you do ++i, i is incremented then used
for example
int i = 0;
while (aBool){
print (i++);
}
will show 0,1,2,3,4,...
as
int i = 0;
while (aBool){
print (++i);
}
will show 1,2,3,4,5,...
No, in this case it is only stylistic. Maybe someone just wanted to use a pre-increment operator for once.
++$i make php execution fast and also increment the thing on the same line of code.
this link may helpful:- http://ilia.ws/archives/12-PHP-Optimization-Tricks.html