Looping and Branching
Looping and Branching
The target label must be within the same file and context.
Meaning that you cannot jump out of a function or method, nor can you
jump into one.
You also cannot jump into any sort of loop or switch structure. You may
jump out of these.
Example:
<?php
goto a;
echo ‘hello';
a:
echo ‘hi';
?>
Example:
<?php
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
?>
<?php
goto loop;
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
loop:
}
}
echo "$i = $i";
?>
The above example will output:
Fatal error: 'goto' into loop or switch statement is disallowed in script on line 2
The DO-WHILE LOOP
Unlike for and while loops, which test the loop condition at the top of the
loop, the do...while loop checks its condition at the bottom of the loop.
Notice that the conditional expression appears at the end of the loop, so
the statement(s) in the loop execute once before the condition is tested.
The DO-WHILE LOOP
Example:
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
The above loop would run one time exactly, since after the first iteration,
when truth expression is checked, it evaluates to FALSE ($i is not bigger than
0) and the loop execution ends.
So output will be just: 0
The DO-WHILE LOOP
Example:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Assignments
Generate tables up to a given number
eg. Declare a variable $limit and give it a number (value can be
change at any time)
if we give a value: $limit = 3; then output should be like
2*1=2
2*2=4
.
.
2 * 10 = 20
3*1=3
3*2=6
.
.
3 * 10 = 30
Assignments
Generate Fibonacci Series
Fibonacci series is the one in which you will get your next term by adding previous two
numbers.
For example,
0 1 1 2 3 5 8 13 21 34
Here, 0 + 1 = 1
1+1=2
3+2=5
and so on.