Thursday, May 4, 2017

What is use of header() function in php?

1. Header is used to redirect from current page to another:

header("Location: newpage.php");

2. Header is used to send HTTP status code.
header("HTTP/1.0 404 Not Found");

3. Header is used to send Send a raw HTTP header
header('Content-Type: application/pdf');

Saturday, April 29, 2017

Difference betwwen Single quotes vs Double quotes in PHP

Strings with double quotes

You can insert variables directly within the text of the string. The PHP parser will automatically detect such variables, convert their values into readable text, and place them in their proper places (this is called concatenating, funny word).  
<?php
 $name = "Peter";
 $age = 42
 echo "Hi, my name is $name and I am $age years old.";
?> 
Output : Hi, my name is Peter and I am 42 years old.

Strings with single quotes

The text appears as it is. When it comes to enclosing strings in PHP, single quotes are much stricter than double quotes. When you enclose a string with single quotes, PHP ignores all your variables. Plus, you are able to write double quotes inside the text without the need of the escape slash.

<?php
 $name = 'David';
 echo 'Hi, my name is $name "The Crazy" Jones.';
?>
 Output : Hi, my name is $name "The Crazy" Jones.

double quotes VS single quotes 

<?php
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
?>



 

Monday, April 24, 2017

PHP script for printing first 20 Fibonacci numbers .

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

Display Prime Numbers Between two Intervals in C language.

#include <stdio.h>
int main()
{
    int low, high, i, flag;
    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);

    printf("Prime numbers between %d and %d are: ", low, high);

    while (low < high)
    {
        flag = 0;

        for(i = 2; i <= low/2; ++i)
        {
            if(low % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
            printf("%d ", low);

        ++low;
    }

    return 0;
}

Check prime no using function.

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

Sunday, April 23, 2017

Program to find prime numbers in between a range

<?php
//error_reporting(E_ALL);
//Program to find prime numbers in between a range

$num =23;

for( $j = 2; $j <= $num; $j++ )
{
for( $k = 2; $k < $j; $k++ )
{
if( $j % $k == 0 )
{
break;
}

}
if( $k == $j )
echo "Prime Number : ", $j, "<br>";
}
?>

PHP Program to find Largest & Smallest numbers in an Array

<html>
<head>
<title>Find Largest & Smallest numbers in an Array - PHP Programming</title>
</head>
<body>
   
    <h3>PHP Program to find Largest & Smallest numbers in a Array</h3>
    Enter the Numbers separated by Commas <br />
    (eg: 12,23,56)
    <br /><br />
    <form method="post">
        <input type="text" name="numbers"/>
        <button type="submit">Check</button>
    </form>
</body>
</html>

<?php
   
    if($_POST)
    {
        //get the post value from form
        $numbers = $_POST['numbers'];
       
        //separate the numbers and make into array
        $numArray = explode(',', $numbers);
       
        //assign the first value of the above array for the Largest & Smallest variables
        $largest  = $numArray[0];
        $smallest = $numArray[0];
       
       
        //now loop through each numbers and find which is Largest & Smallest numbers in a PHP Array
        foreach($numArray as $num){
           
           
            if($num > $largest){
                $largest = $num;
            }
            else if($num < $smallest){
                $smallest = $num;
            }
           
        }
               
        //print the output

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

    }    

?>

Friday, April 21, 2017

Sort an array of integers without using PHP built-in sort functions

<?php

$srtArray=array(2,8,9,5,6,3);
 for ($i=0; $i<count($srtArray); $i++) {
    for ($j=0; $j<count($srtArray); $j++) {
      // Compare two elements of array
      if ($srtArray[$j] > $srtArray[$i]){
        $tmp = $srtArray[$i];
        $srtArray[$i] = $srtArray[$j];
        $srtArray[$j] = $tmp;
      }
    }
 }
//Print an array after sorting
 for($i=0;$i<count($srtArray);$i++){
   echo $srtArray[$i]."<br>\n";
 }
   
?>