<?php
$number = 6; /*number to get factorial */
$fact = 1;
for($k=1;$k<=$number;++$k)
{
$fact = $fact*$k;
}
echo "Factorial of $number is ".$fact;
?>
<?PHP
$first = 0;
$second = 1;
echo $first.' ,';
echo $second.' ,';
for($limit=0;$limit<10;$limit++)
{
$third = $first+$second;
echo $third.' ,';;
$first = $second;
$second = $third;
}
?>
// If the sum of cubes of individual digits of a number is equal to the number itslef then it is called Armstrong Number.
<?php
if(isset($_POST['number']) && $_POST['number']!='')
{
$number = $_POST[ 'number' ]; // get the number entered by user
$temp = $number;
$sum = 0;
while($temp != 0 )
{
$remainder = $temp % 10; //find reminder
$sum = $sum + ( $remainder * $remainder * $remainder );
$temp = $temp / 10;
}
if( $number == $sum )
{
echo "$number is an Armstrong Number";
}else
{
echo "$number is not an Armstrong Number";
}
}
?>
<?php
if(isset($_POST['rev2']))
{
$rev=0;
$num=$_POST['rev'];
while($num>=1)
{
$re=$num%10;
$rev=$rev*10+$re;
$num=$num/10;
}
}
?>
<html>
<head>
<title>Reverse</title>
</head>
<body>
<table>
<form name="frm" method="post">
<tr><td>Number</td><td><input type="text" name="rev"></td></tr>
<tr><td>Reverse is:</td><td><input type="text" value="<?php if(isset($_POST['rev2'])){echo $rev;} ?>" name="rev1"></td></tr>
<tr><td> </td><td><input type="Submit" value="Reverse" name="rev2"></td></tr>
</form>
</table>
</body>
</html>
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Check' )
{
$check=0;
$num=$_POST['number'];
for($i=2;$i<=($num/2);$i++)
{
if($num%$i==0)
{
$check++;
if($check==1)
{
break;
}
}
}
}
?>
<html>
<head>
<title>Prime Number</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number:</td><td><input type="text" name="number" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Check" /></td>
<td>
<center><span>
<?php if(isset($_POST['sub']))
{if($check==0)
{echo "It is a Prime Number";
}
else
{
echo "It is not a Prime Number";}
}
?>
</span>
</center>
</td>
</tr>
</form>
</table>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];
function hcf($i1,$i2)
{
if($i2!=0)
{
return hcf($i2,$i1%$i2);
}
else
{
return $i1;
}
}
$hcfofnumber=hcf($num1,$num2);
}
?>
<html>
<head>
<title>Hcf</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number1:</td><td><input type="text" name="number1" /></td></tr>
<tr><td>Number2:</td><td><input type="text" name="number2" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="submit" /></td>
<td><center><span>
<?php if(isset($_POST['submit']))
{
echo "HCF is ".$hcfofnumber; }
?>
</span></center></td></tr>
</form>
</table>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$year=$_POST['year'];
if($year%4==0)
{
$leap="It is a leap year";
}
else
{
$leap="It is not a leap year";
}
}
?>
<html>
<head>
<title>Leap Year</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Enter the year:</td><td><input type="text" name="year" /></td></tr>
<tr><td></td> <td><input type="submit" name="submit" value="submit" /></td>
<td><center><span>
<?php
if(isset($_POST['submit'])){
echo $leap; }
?>
</span></center></td></tr>
</form>
</table>
</body>
</html>
<?php
if(isset($_POST['sub']))
{ $j=0;
$factor=array();
$num=$_POST['nm1'];
for($i=1;$i<=$num;$i++)
{
if($num%$i==0)
{
$j++;
$factor[$j]=$i;
}
}
}
?>
<table>
<form name="frm" method="post" action="">
<tr> <td>Number:</td> <td><input type="text" name="nm1" /></td> </tr>
<tr><td></td><td><input type="submit" name="sub" /></td>
<td><center><span>
<?php
if(isset($_POST['sub']))
{
echo "Factors are :";for($i=1;$i<=count($factor);$i++)
{ echo $factor[$i].",";
}
}
?>
</span></center></td></tr>
</form>
</table>
<?php
if(isset($_POST['sub']))
{
$num=$_POST['num'];
echo "<h1><center>Table of " .$num."</center></h1>";
for($i=1;$i<=10;$i++)
{
echo $num*$i;
echo "<br />";
}
}
?>
<table>
<form name="frm" method="post">
<tr><td>Number</td><td><input type="text" name="num"></td></tr>
<tr><td> </td><td><input type="Submit" value="Submit" name="sub"></td></tr>
</form>
</table>
<?php
$numbers = array(12,23,45,20,5,6,34,17,9,56,999);
$length = count($numbers);
$max = $numbers[0];
for($i=1;$i<$length;$i++)
{
if($numbers[$i]>$max)
{
$max=$numbers[$i];
}
}
echo "The biggest number is ".$max;
?>
<?php
$a=15;
$b=10;
echo "The value of a is ".$a." and b is ".$b;
echo " before swapping.<br />";
$temp=$a;
$a=$b;
$b=$temp;
echo "The value of a is ".$a." and b is ".$b;
echo " After swapping <br />";
?>
<?php
$numbers=array(12,23,45,20,5,6,34,17,9,56);
$length=count($numbers);
$min=$numbers[0];
for($i=1;$i<$length;$i++)
{
if($numbers[$i]<$min)
{
$min=$numbers[$i];
}
}
echo "The smallest number is ".$min;
?>
<?php
for($i=1;$i<=4;$i++)
{
$i1=$i+4;
$i2=$i+8;
echo $i." ".$i1." ".$i2;
echo "<br />";
}
?>
<?php
for($i = 1; $i<=5; $i++){
for($j = 1; $j<=5; $j++){
if($i == 1 || $i == 5){
echo "*";
}
else if($j == 1 || $j == 5){
echo "*";
}
else {
echo " ";
}
}
echo "<br/>";
}
?>
<?php
$a = 1;
for($i = 1; $i<=5; $i++)
{
for($j = 1; $j<=$i; $j++)
{
echo $a;
$a++;
}
echo '<br/>';
}
?>
bool(true) // and operator works as OR because = operator has the precedence over and.
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];
$hcf = gcd($num1, $num2);
$lcm = ($num1*$num2)/$hcf;
}
function gcd($x, $y)
{
if ($x == 0) {
return $y;
}
while ($y != 0) {
if ($x > $y) {
$x = $x - $y;
}
else {
$y = $y - $x;
}
}
return $x;
}
?>
session_id() function returns the session id for the current session.
<?php
for($i=0;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo "* ";
}
echo "<br>";
}
?>
<?php
for($i=0;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
<?php
if(isset($_POST['submit']))
{
$check=0;
$num=$_POST['num'];
for($i=2;$i<=($num/2);$i++)
{
if($num%$i==0)
{
$check++;
if($check==1)
{
break ;
}
}
}
if($check==0)
{
echo "It is a Prime Number";
}
else
{
echo "It is not a Prime Number";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Check whether a number prime or not</title>
</head>
<body>
<form name="primenumber" action="" method="post">
Number :<input type="text" name="num" value="" required><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<html>
<head>
<title>Number Pyramid</title>
</head>
<body>
<?php
$r;
$c;
for($r=1;$r<=5;$r++)
{
for($c=1;$c<=$r;$c++)
{
print("$r");
}
print "<br>";
}
?>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$rev=0;
$num=$_POST['num'];
while($num>=1)
{
$re=$num%10;
$rev=$rev*10+$re;
$num=$num/10;
}
echo "Reverse number of is " .$rev;
}
?>
<?php
if(isset($_POST['submit']))
{
$value1=$_POST['num1'];
$value2=$_POST['num2'];
$value1=$value1+$value2;
$value2=$value1-$value2;
$value1=$value1-$value2;
echo "Value of first variable after swapping" .$value1."<br />";
echo "Value of second variable after swapping" .$value2;
}
?>
<?php
if(isset($_POST['submit']))
{
$string=$_POST['string'];
$length = strlen($string);
for ($i=($length-1) ; $i >= 0 ; $i--)
{
echo $string[$i];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Reverse a String</title>
</head>
<body>
<table>
<form name="reversestring" method="post">
<tr>
<td>Enter a String :</td>
<td><input type="text" name="string" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Reverse" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
<?php
$str= "mahi";
$str2 = "habib";
$a = str_split($str);
$a2 = str_split($str2);
static $j = 0;
for($i = 0; $i<= 9; $i++){
if($i%2 !== 0 && $i >0) {
array_splice($a,$i,0,$a2[$j]);
$j++;
}
}
echo $str_new = implode('',$a);
echo '<br/>';
?>
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];
$hcf = gcd($num1, $num2);
$lcm = ($num1*$num2)/$hcf;
}
function gcd($x, $y)
{
if ($x == 0) {
return $y;
}
while ($y != 0) {
if ($x > $y) {
$x = $x - $y;
}
else {
$y = $y - $x;
}
}
return $x;
}
?>
<?php
$array = array('200', '12','69','250','50','500');
$maxnum1 = 0;
$maxnum2 = 0;
for($i=0; $i< count($array); $i++)
{
if($array[$i] > $maxnum1)
{
$maxnum2 = $maxnum1;
$maxnum1 = $array[$i];
}
else if($array[$i] > $maxnum2)
{
$maxnum2 = $array[$i];
}
}
echo "Second Maximum NO is ".$maxnum2;
?>
You can declare three types of arrays in PHP. They are numeric, associative and multidimensional arrays.
//Numeric Array
$computer = array("Dell", "Lenavo", "HP");
//Associative Array
$color = array("Sithi"=>"Red", "Amit"=>"Blue", "Mahek"=>"Green");
//Multidimensional Array
$courses = array ( array("PHP",50), array("JQuery",15), array("AngularJS",20) );
function Palindrome($number){
$temp = $number;
$new = 0;
while (floor($temp)) {
$d = $temp % 10;
$new = $new * 10 + $d;
$temp = $temp/10;
}
if ($new == $number){
return 1;
}
else{
return 0;
}
}
// Driver Code
$original = 1441;
if (Palindrome($original)){
echo "Palindrome";
}
else {
echo "Not a Palindrome";
}
?>
function Palindrome($string){
if (strrev($string) == $string){
return 1;
}
else{
return 0;
}
}
// Driver Code
$original = "DAD";
if(Palindrome($original)){
echo "Palindrome";
}
else {
echo "Not a Palindrome";
}
?>
if (!empty($_POST))
if (isset($_POST['submit']))
When the variable is passed as value then it is called pass variable by value.
function test($n) {
$n=$n+10;
}
$m=5;
test($m);
echo $m;
When the variable is passed as a reference then it is called pass variable by reference.
function test(&$n) {
$n=$n+10;
}
$m=5;
test($m);
echo $m;
The way by which PHP can assign a particular data type for any variable is called typecasting.
$str = "10"; // $str is now string
$bool = (boolean) $str; // $bool is now boolean
PHP does not support data type for variable declaration. The type of the variable is changed automatically based on the assigned value and it is called type juggling.
$val = 5; // $val is now number
$val = "500" //$val is now string
How to get current Indian time in PHP
When you click on link then redirect to the google play store .
Trending Tutorials