for Loop inPHP

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

<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

<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

<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

<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

<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

<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) ||||

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.