PHP Exercise and solutions: PHP Brief – codzhub

PHP stands for PHP: Hypertext Preprocessor or personal home page which is server side script and used to work with creating a web application or development . 

It is programming language as c or c++ etc . We can make code with using HTML , css , javascript for combination of these language as such .

PHP has more benefits as compare to other language ,

  •  It is open source and easy to download with.
  • It can run any server like Apache , IIS or etc .
  • It is much faster than other language .
  • It is easy to maintain by any one .
  • It have huge powerful liabrary support.
  • It could be run in many platform like window , linux or etc
  • There is two way to write php code <?php ?> or <? ?> ( Knwn as shortcode).

For Example :


<?php 
echo "Hello PHP";
?>

Read More

PHP Exercise and solutions: An Array Brief – codzhub

An array is a continuous memory allocation in memory . It will accept the key and value in pair . An Array can store the value and key in simple variables .

An array is a special type which can hold the multiple value .

For Example :

<?php

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

echo "<pre>";

print_r($arr);

exit;

?>

There are many types of an array those are listed belows :

  • Numeric Array
  • Associative Array
  • Multi Dimentional Array

Click me to see the solution

PHP Exercise and solutions: Displaying array with sort – codzhub

PHP - An Array 

An array is continuous memory allocation with key or value based . Array is a special type in php where

Sample Solution:

PHP Code:

<?php
class array_sort
{
    protected $_asort;
    
    public function __construct(array $asort)
     {
        $this->_asort = $asort;
     }
    public function alhsort()
     {
        $sorted = $this->_asort;
        sort($sorted);
        return $sorted;
      }
}
$sortarray = new array_sort(array(11, -2, 4, 35, 0, 8, -9));
print_r($sortarray->alhsort())."\n";
?>

Click me to see the solution

PHP Exercise and solutions: Displaying calculates the factorial – codzhub

Sample Solution:

PHP Code:

<?php
class factorial_of_a_number
{
  protected $_n;
  public function __construct($n)
   {
     if (!is_int($n))
	   {
	      throw new InvalidArgumentException('Not a number or missing argument');
       }
    $this->_n = $n;
	}
  public function result()
    {
     $factorial = 1;
     for ($i = 1; $i <= $this->_n; $i++)
	  {
	    $factorial *= $i;
      }
	   return $factorial;
	 }
 }

$newfactorial = New factorial_of_a_number(5);
echo $newfactorial->result();
?>

Click me to see the solution

PHP Exercise and solutions: Displaying difference between two dates – codzhub

Sample Solution:

PHP Code:

<?php
$sdate = "1981-11-04";
$edate = "2013-09-04";

$date_diff = abs(strtotime($edate) - strtotime($sdate));

$years = floor($date_diff / (365*60*60*24));
$months = floor(($date_diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($date_diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days", $years, $months, $days);
printf("\n");
?>

Click me to see the solution