PHP programs :-

Example : Factorial Program

<html>
<body>
<title>Factorial Program in PHP</title>
<form action="" method="post">
<label>Enter Number to calculate Factorial</label> <input type="text" name="number" size="2" />
</form>
</body>
</html>

<?php
if($_POST){
$fact = 1;
$number = $_POST['number'];
echo "Factorial of $number:<br><br>";
for ($i = 1; $i <= $number; $i++){
$fact = $fact * $i;
print $fact . "<br>";
}

echo "Factorials of given number is ".$fact;
}

?>
/ ***************** OR ************* */<br>
$num = $_POST["number"];
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";


Output :-

Enter Number to calculate Factorial
5
Factorial of 5:

1
2
6
24
120
Factorials of given number is 120

Enter Number to calculate Factorial 5
Factorial of 5 is 120

Example : Factorial number using Ajax

<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
</head>
<body>
<form action="" method="post">
<input type="hidden" name="ispost" value="y" />
Enter a Number: <input type="text" name="number" id="number" />
<input type="button" id="factorial_button" value="Calculate Factorial">
</form>
<script>
jQuery(document).ready(function(){
jQuery("#factorial_button").click(function(){
var number = jQuery("#number").val();

jQuery.ajax({
url: "Factcalc.php",
type: "POST",
data: {
"number":number,
},
dataType : "html",
cache: false,
beforeSend: function () {
jQuery("#result").html("retrieving information...");
},
success: function( data ) {
jQuery("#result").html(data);
},
error: function( xhr, status, errorThrown ) {
jQuery("#result").html("Ajax error");
}
});
});
});
</script>
<div id="result"></div>
</body>
</html>


Example : Factcalc.php

<?php
$num = $_POST["number"];
$factorial = 1;

for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}

echo "Factorial of $num is $factorial";
?>


Output :-


Example : Palindrome Check.

<html>
<head>
<title>Check Given no is palindrome or not./title>
</head>
<body>

<h3>Write PHP Program to find if a number is Palindrome or not! <a href="itechtuto.com">itechtuto.com</a></h3>
Enter a Number <br />
<br />
<form method="post">
<input type="text" name="number"/>
<button type="submit">Check</button>
</form>
</body>
</html>

<?php

if($_POST)
{
$number = $_POST['number'];

//reverse the number
$reverse = strrev($number);

//check if the number and reverse is equal
if($number == $reverse){
echo "<span style='color:green'>The number is Palindrome!!</span>";
}else{
echo "<span style='color:red'>Sorry! the number is not a Palindrome!!</span>";
}

}

?>


Palindrome check

File name : index.php

<?php
$word = "level";
echo "String: " . $word . "<br>";
$reverse = strrev($word); // reverse the word
if ($word == $reverse) // compare if the original word is same as the reverse of the same word
echo 'Output: This string is a palindrome';
else
echo 'Output: This is not a palindrome';
?>

Output :-

Output: This string is a palindrome

Example without reverse function

File name : index.php

<?php
$mystring = "level"; // set the string
echo "String: " . $mystring;
$myArray = array(); // php array
$myArray = str_split($mystring); //split the array
$len = sizeof($myArray); // get the size of array
$newString = "";

for ($i = $len; $i >= 0; $i--) {
$newString.=$myArray[$i];
}
echo "<br>";
if ($mystring == $newString) {
echo "Output: " . $mystring . " is a palindrome";
} else {
echo "Output: " . $mystring . " is not a palindrome";
}
?>

Output :-

level is a palindrome

Example number is palindrome or not

File name : index.php

<?php
$num=$_GET['n'];
$p=$num;
while((int)$num!=0)
{
$rem=$num%10;
$sum=$sum*10+$rem;
$num=$num/10;

}
if($sum==$p)
{
echo $p." is palindrome number";
}
else
{
echo $p." is not palindrome number";
}
?>
<form>
enter your number<input type="text" name="n"/>
<input type="submit" value="Chk Result"/>
</form>

Check given number is prime or not

File name : index.php

<?php
$num = 4; // <=== Replace desired number to check
for ($i = 2; $i <= $num - 1; $i++) {
if (bcmod($num, $i) == 0) {
echo $num . " is not prime number :(";
break;
}
}
if ($num == $i)
echo $num . " is prime number :)";
?>

Example

File name : index.php

<?php
function IsPrime($n)
{
for($x=2; $x<$n; $x++)
{
if($n %$x ==0)
{
return 0;
}
}
return 1;
}
$a = IsPrime(3);
if ($a==0)
echo 'This is not a Prime Number.....';
else
echo 'This is a Prime Number..';
?>

Example : Fibonic series .

<?php $count = 0 ;
$f1 = 0;
$f2 = 1;
echo $f1." , ";
echo $f2." , ";
while ($count < 20 )
{ $f3 = $f2 + $f1 ;
echo $f3." , ";
$f1 = $f2 ;
$f2 = $f3 ;
$count = $count + 1;
}
?>


Output :-

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 , 987 , 1597 , 2584 , 4181 , 6765 , 10946 ,

Example : Find highest and smallest no in an array.

<html>
<head>
<title>Find highest & Smallest numbers in an Array</title>
</head>
<body>

Enter the Numbers separated by Commas <br />
(eg: 10,2,50)
<br /><br />
<form method="post">
<input type="text" name="numbers"/>
<button type="submit">Check</button>
</form>
</body>
</html>

<?php

if($_POST)
{
$numbers = $_POST['numbers'];

//separate the numbers and make into array
$numArray = explode(',', $numbers);

//assign the first value of the above array for the highest & Smallest variables
$highest = $numArray[0];
$smallest = $numArray[0];
foreach($numArray as $num){
if($num > $largest){
$largest = $num;
}
else if($num < $smallest){
$smallest = $num;
}

}

echo "highest Number is: $largest <br />";
echo "Smallest Number is: $smallest";

}

?>


Output :-


Example : Count the number of occurrences of a character in a string

<?php
$string = "Hello World";
//first replace the space between words
$string = str_replace(' ', '', $string);
$str_array = count_chars($string, 1);
foreach ($str_array as $ascii => $occurences) {
echo chr($ascii) . '- occurs ' . $occurences . ' time(s)<br />';
}
?>


Example : PHP Percentage Calculator.

<?php

if($_POST){

$percent = $_POST['percent'];
$number = $_POST['number'];
$answer = ($percent / 100) * $number;
}
?>
<html>
<head>
<title>PHP Percentage Calculator </title>
</head>
<body>
<form action="" method="post">
<input size="7" type="text" name="percent" value="<?php echo isset($percent) ? $percent : '';?>" /> % of <input size="10" type="text" name="number" value="<?php echo isset($number) ? $number : '';?>" /> = <input size="14" type="text" name="a-ans" value="<?php echo isset($answer) ? $answer: ''; ?>" class="disabled-cursor" disabled />
<input value="Calculate" type="submit" />
</form>
</body>
</html>


Example : Check Year is leap or Not.

<html>
<body>
<h2>find Leap year or not</h2>
<form action="" method="post">
<input type="text" name="year" />
<input type="submit" />
</form>
</body>
</html>
<?php

if( $_POST )
{
//get the year
$year = $_POST[ 'year' ];

//check if entered value is a number
if(!is_numeric($year))
{
echo "Strings not allowed, Input should be a number";
return;
}

//multiple conditions to check the leap year
if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) )
{
echo "$year is a leap year";
}
else
{
echo "$year is not a leap year";
}

}

?>


Example : Check number of vowels in string.

<html>
<body>

<h2>Find Number of Vowels in a String - PHP Script by <a href="http://www.tutorialsmade.com/">Tutorialsmade.com</a></h2>
<form action="" method="post">
<input type="text" name="string" />
<input type="submit" />
</form>
</body>
</html>

<?php

if($_POST)
{
//get the input value and convert string to lowercase
$string = strtolower($_POST['string']);
//all vowels in array
$vowels = array('a','e','i','o','u');
//find length of the string
$len = strlen($string);
$num = 0;

//loop through each letter
for($i=0; $i<$len; $i++){
if(in_array($string[$i], $vowels))
{
$num++;
}
}

//output
echo "Number of vowels : <span style='color:green; font-weight:bold;'>". $num ."</span>";
}

?>


Example : 2nd method Check number of vowels in string.

<html>
<body>

<h2>Find Number of Vowels in a String - PHP Script by <a href="http://www.tutorialsmade.com/">Tutorialsmade.com</a></h2>
<form action="" method="post">
<input type="text" name="string" />
<input type="submit" />
</form>
</body>
</html>

<?php

if($_POST)
{
$string = strtolower($_POST['string']);
$num = preg_match_all('/[aeiou]/i',$string,$matches);
echo "Number of Vowels : ". $num;
}

?>


Example : check no is arm strong or not.

<html>
<body>

<!-- 371 or 407, these are armstrong numbers -->

<form action="" method="post">
<input type="text" name="number" />
<input type="submit" />
</form>
</body>
</html>

<?php

if( $_POST )
{
//get the enter number
$number = $_POST[ 'number' ];

//store it in a temp variable
$temp = $number;
$sum = 0;

//loop till the quotient is 0
while( $temp != 0 )
{
$rem = $temp % 10; //find reminder
$sum = $sum + ( $rem * $rem * $rem ); //cube the reminder and add it to the sum variable till the loop ends
$temp = $temp / 10; //find quotient. if 0 then loop again
}

//if the entered number and the $sum value matches then it is an armstrong number
if( $number == $sum )
{
echo "Armstrong Number";
}else
{
echo "Not an Armstrong Number";
}
}

?>


Example : print A to Z or a to z alphabets in PHP!

<?php

$alpharange = range( 'a', 'z' );

foreach ( $alpharange as $i ){
echo "$i\n";
}

?>
<br>
<br>
<?php

foreach (range('A', 'Z') as $i){
echo "$i\n";
}

?>


Reverse a string using string function

<?php

$str = "RADIO";

echo strrev ( $str );

?>

Example : Reverse a string without using string function!

<?php

//your string
$string = 'TEST';

//find string length
$len = strlen($string);

//loop through it and print it reverse
for ( $i = $len - 1; $i >=0;$i-- )
{
echo $string[$i];
}
?>


reverse arry using without rsort() function.

File name : index.php

<?php
$array = array(1, 2, 3, 4);
$size = sizeof($array);

for($i=$size-1; $i>=0; $i--){
echo $array[$i];
}
?>

sorting array without function.

File name : index.php

<?php

$array=array('2','4','8','5','1','7','6','9','10','3');

echo "Unsorted array is: ";
echo "<br />";
print_r($array);


for($j = 0; $j < count($array); $j ++) {
for($i = 0; $i < count($array)-1; $i ++){

if($array[$i] > $array[$i+1]) {
$temp = $array[$i+1];
$array[$i+1]=$array[$i];
$array[$i]=$temp;
}
}
}

echo "Sorted Array is: ";
echo "<br />";
print_r($array);

?>

how to find highest and second highest number in an array without using max function

File name : index.php

<?
$array = array('15','200','69','122','50','201');
$max_1 = $max_2 = 0;

for($i=0; $i<count($array); $i++)
{
if($array[$i] > $max_1)
{
$max_2 = $max_1;
$max_1 = $array[$i];
}
else if($array[$i] > $max_2)
{
$max_2 = $array[$i];
}
}
echo "Max=".$max_1;
echo "<br />";
echo "Smax 2=".$max_2;



?>

How to count second value of array from bottom.

File name : index.php

<?
$ar = array(1,10,5,20,50);
$c = count($ar);

for($i=$c-1;$i>=$c-1;$i--)
{
$k = $ar[$i];
}
echo $ar[$i];
?>

How to count total array value in php without sizeof() and count() funtion.

File name : index.php

<?
$ar = array(1,2,5,8,9);
$max = 0;
$c = 0;
foreach ( $ar as $item ) {
if ( $max<$item ) {
$max = $item;
$c++;
}

}
echo $c;
?>

function maximum($array) {
$max = 0;
foreach($array as $value) { // get every value of array
if($max < $value) // if value of array is bigger then last one
$max = $value; // put it into $max
}
return $max;
}

what is output of this programs

File name : index.php

<?php
$foo = "1"; // $foo is string (ASCII 49)
$foo *= 2; // $foo is now an integer (2)
$foo = $foo * 1.3; // $foo is now a float (2.6)
$foo = 5 * "10 Little Piggies"; // $foo is integer (50)
$foo = 5 * "10 Small Pigs"; // $foo is integer (50)
?>

Write a program to print Factorial of any number

Write a program to print Factorial of any number +

<?php

$number = 6; /*number to get factorial */
$fact = 1;
for($k=1;$k<=$number;++$k)
{
$fact = $fact*$k;
}
echo "Factorial of $number is ".$fact;


?>


Write a program in PHP to print Fibonacci series . 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Write a program in PHP to print Fibonacci series . 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 +

<?PHP

$first = 0;
$second = 1;
echo $first.'&nbsp;,';
echo $second.'&nbsp;,';


for($limit=0;$limit<10;$limit++)
{
$third = $first+$second;
echo $third.'&nbsp;,';;
$first = $second;
$second = $third;

}

?>


Write a program to find whether a number is Armstrong or not

Write a program to find whether a number is Armstrong or not +

// 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";
}
}

?>


Write a program to print Reverse of any number

Write a program to print Reverse of any 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>


To check whether a number is Prime or not.

To check whether a number is Prime or not. +

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


Write a program to find HCF of two numbers

Write a program to find HCF of two numbers +

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


Program to find whether a year is LEAP year or not

Program to find whether a year is LEAP year or not +

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


Write a program to find factor of any number

Write a program to find factor of any number +

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


Write a program to find table of a number

Write a program to find table of a number +

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


Write a Program for finding the biggest number in an array without using any array functions.

Write a Program for finding the biggest number in an array without using any array functions. +

<?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;
?>


Write a Program to swap two numbers in PHP.

Write a Program to swap two numbers in PHP. +

<?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 />";

?>


Write a Program for finding the smallest number in an array

Write a Program for finding the smallest number in an array +

<?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;


?>


Write a program to print the below format :
1 5 9
2 6 10
3 7 11
4 8 12 +

<?php


for($i=1;$i<=4;$i++)
{
$i1=$i+4;
$i2=$i+8;
echo $i." ".$i1." ".$i2;
echo "<br />";
}


?>


Write a program for this Pattern:
*****
* *
* *
* *
***** +

<?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 "&nbsp;&nbsp;";

}



}

echo "<br/>";

}

?>


How to write a Floyd's Triangle?
1
23
456
78910
1112131415 +

<?php

$a = 1;

for($i = 1; $i<=5; $i++)

{

for($j = 1; $j<=$i; $j++)

{

echo $a;

$a++;

}

echo '<br/>';

}

?>


What will be the output of the following:
$x = ture and false;
var_dump($x); +

bool(true) // and operator works as OR because = operator has the precedence over and.


Program to find the LCM of two numbers.

Program to find the LCM of two numbers. +

<?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;
}

?>


How to get the value of current session id?

How to get the value of current session id? +

session_id() function returns the session id for the current session.


Write a program for this pattern ?
* * * * *
* * * *
* * *
* *
* +

<?php
for($i=0;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo "*&nbsp;";
}
echo "<br>";
}
?>


Write a program for this pattern ? * * * * * * * * * * * * * * * +

<?php
for($i=0;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*&nbsp;";
}
echo "<br>";
}
?>


Write a program for this pattern ?
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
* +

<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " &nbsp;";
}
for($j=1;$j<=$i;$j++){
echo "* &nbsp;";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " &nbsp;";
}
for($j=1;$j<=$i;$j++){
echo "* &nbsp;";
}
echo "<br>";
}
?>


How to find whether a number prime or not ? +

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


Write a program for this pattern(number pyramid) ?
1
22
333
4444
55555 +

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


How to print a number reverse ? +

<?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;
}
?>


Swap two values without third variable ? +

<?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;
}
?>


Reverse String Program +

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


Write a program to concatenate two strings character by character. e.g : MAHI + HABIB = MHAAHBIIB +

<?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/>';
?>


Program to find the LCM of two numbers. +

<?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;
}

?>


Write a program to find second highest number in an array. +

<?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;

?>


Q. How can you declare the array in PHP? +

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


Check for Palindrome number +

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";
}

?>


Check for Palindrome String +

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";
}
?>


How to check form submission in PHP ? +

if (!empty($_POST))
if (isset($_POST['submit']))


What is meant by ?passing the variable by value and reference' in PHP? +

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;


pass by reference. +

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;


Explain type casting and type juggling. +

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

File name : index.php

<?php
$indiatimezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($indiatimezone);
echo $date->format( 'H:i:s A / D, M jS, Y' );
?>

How to show div content only android phone. not on iphone.

When you click on link then redirect to the google play store .

File name : index.php



<script>
document.addEventListener("scroll", function() {

if (window.pageYOffset > 100)
document.getElementById('anything').style.display = "block";
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#close').live('click',function(){

$('#anything').hide();
});
});
</script>

<?php
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
?>

<div class="anything" id="anything">
<div id="app-promotion-mobile-android" class="android-app">
<div class="close-block">
<a class="close-icon" id="close" href="javascript: void(0);"></a>
</div>
<div class="text-block">
<span class="panda-for-android">Celebsutra Android App</span>
<span class="panda-info">Available on Google Play</span>
</div>
<div class="install-block">
<a class="install-button" href="
https://play.google.com/store/apps/details?id=com.iwb.celebsutra">Continue in App</a>
</div>
</div>
</div>

<?php
} ?>

style.css

.anything{
display:none;
}

@media screen and (max-width: 768px)
{
.anything {
width: 100%;
display: none;
position: fixed;
top: 1px;
/* border: 1px solid;*/
background: #F5F6F7;
z-index: 99;
}
.close-block {
float: left;
height: 100%;
width: 11px;
margin: 0 10px;
}

.close-icon {
background: url(close-promotion.png) no-repeat;
background-size: 11px 11px;
display: block;
width: 11px;
height: 11px;
margin: 19px 0;
}

.text-block {
width: 48%;
}

.text-block {
display: block;
float: left;
height: 100%;
width: 150px;
padding-top: 10px;
box-sizing: border-box;
}


.panda-for-android {
font-family: Roboto;
font-weight: 700;
font-size: 13px;
}

.panda-info {
font-family: Roboto;
font-weight: 400;
line-height: 16px;
font-size: 10px;
color: #888;
}

.install-button {
/* display: block; */
height: 32px;
box-sizing: border-box;
color: #fff;
font-family: Roboto;
font-weight: 500;
font-size: 14px;
line-height: 32px;
text-align: center;
margin-top: 9px;
border-radius: 5px;
padding: 7px;
background-color: #353535;
}

}

The JavaScript
Searching the user agent string for "Android" is the quickest method:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
// Do something!
// Redirect to Android-site?
window.location = 'http://android.davidwalsh.name';
}

File name : index.php

The PHP
Again, we'll use PHP's strstr function to search for Android in the user agent:

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
header('Location: http://android.davidwalsh.name');
exit();
}

File name : index.php

.htaccess Detection
We can even use .htaccess directives to detect and react to Android devices!

RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://android.davidwalsh.name [R=301]

+


Example : print Pyramid of Stars.

<?php

if($_GET){

$n = $i = $_GET['stars'];

while ($i--){
echo str_repeat('&nbsp;', $i).str_repeat('*', $n - $i)."<br>";
}
}
?>
<html>
<head></head>
<body>
<form>
Enter number of lines: <input type="text" name="stars" />
</form>
</body>
</html>


Output :-






Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here