For Loop in PHP
Examples of for loop in PHP
Program to show a table of a number till 10 using for loop and if else
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<form Method="post"> <br><br> Enter number:<input type="text" name="value"/><br><br> <input type="submit" name="btn" value="print table" /> </form> <?php if(isset($_POST['btn'])) { $n=$_POST['value']; $res=0; for($i=1;$i<=10;$i++) { $res=$n*$i; echo $n; echo"*"; echo $i; echo "="; echo $res; echo "<br>"; } } ?> |
|||| Click to read Complex programs of ‘show a table of a number till 10” (not for beginners) ||||
PHP program to find the Fibonacci series
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<html> <body> </body> </html> <?php function F($n) { $number1 = 0; $number2 = 1; echo "Fibonacci Series \n"; echo $number1.' '.$number2.' '; for($i = 2; $i < $n; $i++){ $number3 = $number1 + $number2; echo $number3.' '; $number1 = $number2; $number2 = $number3; } } F(2); ?> |
|||| Click to read Complex programs of Fibonacci seriesĀ (not for beginners) ||||
PHP program to find the factorial of a number
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<html> <body> <?php $p = 3; $f = 1; for ($x=$p; $x>=1; $x--) { $f = $f * $x; } echo "Factorial of number is $f"; ?> </body> </html> |
|||| Click to read Complex programs of factorial of a numberĀ (not for beginners) ||||
Prime Number Program in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<html> <body> <center> <h2>Program 1</h2> <p>Prime number program in PHP </p> <?php $num=29; $LeastPrime=2; if($num<2) { echo"$num is less than 2 Smallest prime number is 2"; } for($LeastPrime=2;$LeastPrime<$num;$LeastPrime++) { if($num%$LeastPrime==0) { echo"$num is not a prime number"; break; } } if($num==$LeastPrime) { echo"$num is a Prime number"; } ?> </center> </body> </html> |
|||| Click to read Complex programs of Prime NumberĀ (not for beginners) ||||
Next Prime Number Program in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<html> <body> <center> <h2>Program 1</h2> <p>Next Prime number program in PHP </p> <?php $NextPrime=0; $Counter=2; $num=17; for($NextPrime=$num+1;$NextPrime<10000;$NextPrime++) { for($Counter=2;$Counter<$NextPrime;$Counter++) { if($NextPrime %$Counter==0) { break; } } if($NextPrime==$Counter || $NextPrime==1) { echo"You entered $num and next prime is $NextPrime"; break; } } ?> </center> </body> </html> |
|||| Click to read Complex programs of Next Prime NumberĀ (not for beginners) ||||
PHP Program to replace the words
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<html> <head> <title>Replace the words</title> </head> <body> <?php for ($i=1; $i <=100 ; $i++) { if ($i%3==0 && $i%5==0) { echo $i."NO.1"."<br>"; }elseif ($i%3==0) { echo $i."WEBSITE"."<br>"."<br>"; }elseif ($i%5==0) { echo $i."T4TURORIALS.COM"."<br>"."<br>"; }else{ echo $i."<br>"; } } ?> </body> </html> |
|||| Click to read Complex programs of replace the wordsĀ (not for beginners) ||||