Thursday, 1 March 2018

Reverse Number

<?php
$number= 123;
$rev = 0;
while($number> 1){
    $rem = $temp % 10;
    $rev = ($rev * 10) +$rem;
    $temp = $number/ 10;
}
echo $rev;

#########################################

$num = 1234;
$reverse = 0;
$split = str_split($num);
for($i = count($split)-1; $i >= 0; $i--){
  $reverse .= $split[$i];
}
echo $reverse;
?>

Wednesday, 3 January 2018

Print Table of numbers without Loop

<?php

function table($num, $i) {
    if ($i > 10) {
        return false ;
    }
    else {
        echo $num * $i ;
        echo "<br/>" ;
        table($num, ++$i) ;
    }
}
table(5, 1) ;
?>

Saturday, 18 November 2017

Diamond shape pattern program.

<?php

$num = 10;

for($a = 1; $a <= $num; $a++)
{
    for($b = 1; $b <= $num-$a; $b++)
    {
        echo "&nbsp;&nbsp;";
    }
    for($c = 1; $c <= $a; $c++)
    {
        echo "&nbsp;*&nbsp;";
    }
    echo "<br/>";
}

for($x = 1; $x <= 9; $x++)
{
    for($y = 1; $y <= $x; $y++)
    {
        echo "&nbsp;&nbsp;";
    }
    for($z = $num; $z > $x; $z--)
    {
        echo "&nbsp;*&nbsp;";
    }
    echo "<br/>";
}

?>

How to swap two numbers without using temporary variable.

<?php
$a = 10; $b = 15;

$a = $a+$b; // $a becomes 25.
$b = $a-$b; // $b becomes 10.
$a = $a-$b; // $a becomes 15.

echo $a;
echo $b;
?>

Sunday, 22 October 2017

Pyramid pattern in PHP

<?php

$num= 10;
for($i = 1; $i <= $num; $i++)
{
   for($j = 1; $j <= $num- $i; $j++)
   {
      echo "&nbsp;&nbsp";
   }
   for($k = 1; $k <= $i; $k++)
   {
      echo "*&nbsp;&nbsp";
   }
echo "<br/>";
}

?>

Monday, 21 August 2017

Palindrome (Reverse String)

<?php
$str = "Hello";

$rev = "";

$length = strlen($str);
for($i = $length-1; $i>=0; $i--){
    $rev .= $str[$i];
}

if($str == $rev){
    echo "$str is Palindrome";
}
else{
    echo "$str is not Palindrome";
}
?>

Thursday, 3 August 2017

Insert string into Database

<?php
$str = "New Delhi@@Delhi,Lucknow##U.P,Patna&&Bihar";
$records = explode(",",$str);
foreach($records as $row){
     list($city, $state) = preg_split("/[@{2}#{2}&{2}]+/", $row);
     $sql = "INSERT INTO country(city, state) VALUES('".$city."', '".$state."')";
     echo $sql."<br/>";
    //mysql_query($sql);
}
?>

Thursday, 1 December 2016

Remove multiple whitespaces

<?php

$str = "I      am a good  boy";
$str_length = strlen($str);
$str_arr = str_split($str);
for ($i = 0; $i < $str_length; $i++) {
    if (isset($str_arr[$i + 1])
        && $str_arr[$i] == ' '
        && $str_arr[$i] == $str_arr[$i + 1]) {
        unset($str_arr[$i]);
    } else {
        continue;
    }
}

echo implode("", $str_arr);

?>

Sunday, 1 May 2016

Leap Year Programming

<?php

$year = 2020;
if(($year % 4 == 0) && ($year % 100 !=0) or ($year %400 == 0))
{
    echo $year." Is leap year";
}
else
{
    echo $year ." is not a leap year";
}

?>

Friday, 29 April 2016

Fibonacci Sequence in PHP

<?php

$num =20;
$a = 0;
$b = 1;

echo $a."</br>";
echo $b."</br>";

for($i = 1; $i <= $num; $i++)
{
    $c = $a+$b;
    echo $c."</br>";
    $a = $b;
    $b = $c;
}

?>

Sorting Array without sorting function in PHP

<?php

$array=array(8,4,9,11,2);

for($i=0;$i < count($array);$i++)
{
    for($j=0;$j < $i;$j++)
    {
        if($array[$i] < $array[$j])
        {
            $temp=$array[$i];
            $array[$i]=$array[$j];
            $array[$j]=$temp;
        }
    }
}
print_r($array);

?>

Friday, 18 December 2015

Make square pattern with star in PHP

<?php

for($i=1;$i<=5;$i++){
    for($j=1;$j<=5;$j++){
        if(($i==1 || $i==5) || ($j==1 || $j==5)){
            echo "*";
        }
        else{
           
            echo "&nbsp;&nbsp";
        }
    }
    echo "<br/>";
}


?>

Friday, 11 December 2015

Merge arrays without array_merge function using PHP.

<?php

$a = array(2,3,4,5);
$b = array(6,7,8,9);

$count = count($a);
for($i = 0; $i<count($b); $i++)
{
    $a[$count] = $b[$i];
    $count++;
}

print_r($a);

?>