Php Tutorial 31-46/46

Php Tutorial


  1. PHP is a script language and interpreter that is freely available and used primarily on Linux Web servers. PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym.
Object OverloadingSubclass and parent classThree levels of inheritancePermutation GeneratorDelete Existing Session VariableDelete Session VariableSession - Storing Complex Data TypesBubble SortInsertion SortQuick sort for associative arraysShell SortThis function returns the position of string s1 within string s2

Name Code

Object Overloading


  < ? php
     class Data
     {
       private $data = array();
       public function __set($name, $value)
       {
         $this->data[$name] = $value;
       }
       public function __get($name)
       {
         if (isset($this->data[$name])) { return $this->data[$name]; }
       }
       public function __isset($name)
       {
         return isset($this->data[$name]);
       }
       public function __unset($name)
       {
         unset($this->data[$name]);
       }
     }
     $data = new Data();
     $data->name = 'F';
     echo "The data value of 'name' is {$data->name}";
     unset($data->name);
     echo 'The value is ', isset($data->name) ? '' : 'not ', 'set.';
  ? > 

Subclass and parent class


  < ? php
     class Employee
     {
       private $name;
       function setName($name)
       {
         if ($name == "")
           echo "Name cannot be blank!";
         else
           $this->name = $name;
       }
       function getName()
       {
         return "My name is ".$this->name.;
       }
     }
     class Executive extends Employee
     {
       function pillageCompany()
       {
         echo "hi!";
       }
     }
     $exec = new Executive();
     $exec->setName("Joe");
     echo $exec->getName();
     $exec->pillageCompany();
  ? > 

Three levels of inheritance


  < ? php
     class Employee
     {
       private $name;
       function setName($name)
       {
         if ($name == "") echo "Name cannot be blank!";
           else $this->name = $name;
       }
       function getName()
       {
         return "My name is ".$this->name.;
       }
     }
     class Executive extends Employee
     {
       function methodB()
       {
         echo " my yacht!";
       }
     }
     class CEO extends Executive
     {
       function methodC()
       {
         echo "tuck";
       }
     }
     $ceo = new CEO();
     $ceo->setName("Joe");
     $ceo->methodB();
     $ceo->methodC();
  ?> 

Permutation Generator


  < ?
     function permutations($letters,$num)
     {
       $last = str_repeat($letters{0},$num);
       $result = array();
       while($last != str_repeat(lastchar($letters),$num))
       {
         $result[] = $last;
         $last = char_add($letters,$last,$num-1);
       }
       $result[] = $last;
       return $result;
     }
     function char_add($digits,$string,$char)
     {
       if($string{$char} <> lastchar($digits))
       {
         $string{$char} = $digits{strpos($digits,$string{$char})+1};
         return $string;
       }
       else
       {
         $string = changeall($string,$digits{0},$char);
         return char_add($digits,$string,$char-1);
       }
     }
     function lastchar($string)
     {
       return $string{strlen($string)-1};
     }
     function changeall($string,$char,$start = 0,$end = 0)
     {
       if($end == 0) $end = strlen($string)-1;
       for($i=$start;$i<=$end;$i++)
       {
         $string{$i} = $char;
       }
       return $string;
     }
  ? >
  To use this Generator you can do something like this :
  < ?
     $Array = permutations("ABC",3);
     for($i=0 ; $i < count($Array) ; $i++)
     {
       echo "$i." . $Array[$i] . "
";
     }
  ? > 

Delete Existing Session Variable


  < ? php
     session_start () ;
     session_register ( "A" ) ;
     session_register ( "B" ) ;
     session_register ( "C" ) ;
     $A = "AAA";
     $B = "BBB";
     $C = "CCC";
     $result = session_destroy () ;
     if ($result=1)
     {
       echo "The session is destroyed! " , "\n " ;
     }
     else
     {
       echo "The session could not be destroyed. " , " \n" ;
     }
     echo $pop;
  ? > 

Delete Session Variable


  < ? php
     session_start();
     $_SESSION['username'] = "Joe";
     echo "Your username is: ".$_SESSION['username'];
     /* Delete the session variable. */
     unset($_SESSION['username']);
     /* Demonstrate that session variable is indeed gone. */
     echo "Username now set to: ".$_SESSION['username'].".";
  ? > 

Session - Storing Complex Data Types


   < ? php
     session_start();
     class myclass
     {
       protected $myvalue;
       public function setmyvalue ($newvalue)
       {
         $this->myvalue = $newvalue;
       }
       public function getmyvalue ()
       {
         return $this->myvalue;
       }
     }
     $_SESSION['myclass_value'] = new myclass ();
     function outputsessions ()
     {
       $_SESSION['myclass_value']->setmyvalue ("Hello World");
       echo $_SESSION['myclass_value']->getmyvalue ();
     }
     outputsessions();
   ? > 

Bubble Sort


  < ? php
   function bubblesort($a1,$a2)
   {
     for($i = sizeof($a1); $i >= 1; $i--)
     {
       for($j = 1; $j <= $i; $j++)
       {
         if($a1[$j-1] > $a1[$j])
         {
           $t = $a1[$j-1];
           $t2 = $a2[$j-1];
           $a1[$j-1] = $a1[$j];
           $a2[$j-1] = $a2[$j];
           $a1[$j] = $t;
           $a2[$j] = $t2;
         }
       }
     }
   }
  ? > 

Insertion Sort


  < ? php
     function insertion_sort(&$a)
     {
       $count = count($a);
       for ($i = 0; $i < $count; $i++)
       {
         $value = $a[$i];
         for ($x = $i - 1; ( ($x >= 0) && ($a[$x] > $value) ); $x--)
         {
           $a[$x + 1] = $a[$x];
         }
         $a[$x + 1] = $value;
       }
     }
     $values = array(7, 3, 4, 6, 1);
     insertion_sort($values);
     foreach ($values as $v) { echo "{$v} "; }
  ? > 

Quick sort for associative arrays


  < ?
     function qsort($a,$f)
     {
       qsort_do(&$a,0,Count($a)-1,$f);
     }
     function qsort_do($a,$l,$r,$f)
     {
       if ($l < $r)
       {
         qsort_partition(&$a,$l,$r,&$lp,&$rp,$f);
         qsort_do(&$a,$l,$lp,$f);
         qsort_do(&$a,$rp,$r,$f);
       }
     }
     function qsort_partition($a,$l,$r,$lp,$rp,$f)
     {
       $i = $l+1;
       $j = $l+1;
       while ($j <= $r)
       {
         if ($f($a[$j],$a[$l]))
         {
           $tmp = $a[$j];
           $a[$j] = $a[$i];
           $a[$i] = $tmp;
           $i++;
         }
         $j++;
       }
       $x = $a[$l];
       $a[$l] = $a[$i-1];
       $a[$i-1] = $x;
       $lp = $i - 2;
       $rp = $i;
     }
  ? > 

Shell Sort


  < ? php
     function shell_sort(&$a)
     {
       $count = count($a);
       $columns = 1;
       while ($columns < $count)
       {
         $columns = $columns * 2 + 1;
       }
       $columns = ($columns - 1) / 2;
       while ($columns > 0)
       {
         for ($c = 0; $c < $columns; $c++)
         {
           for ($i = $columns; $i < $count; $i += $columns)
           {
             $value = $a[$i];
             for ($x = $i - $columns;( ($x >= 0) && ($a[$x] > $value) );$x -= $columns)
             {
               $a[$x + $columns] = $a[$x];
             }
             $a[$x + $columns] = $value;
           }
         }
         $columns = ($columns - 1) / 2;
       }
     }
     $values = array(7, 3, 4, 6, 1);
     shell_sort($values);
     foreach ($values as $v) { echo "{$v} "; }
  ?> 

This function returns the position of string s1 within string s2


  < ? php
     /* This function returns the position of string s1 within string s2.
     The position is 1 based. If s1 is not in s2, 0 is returned.
     */
     function InStr($s1, $s2)
     {
       //Check for valid input
       if(!(is_string($s1) && is_string($s2))) return 0;
       $s1len = strlen($s1);
       $s2len = strlen($s2);
       //Check if s1 in s2 at all
       if(!ereg($s1, $s2)) return 0;
       //Resolve simple case
       if($s1 == $s2) return 1;
       //Set initial search limits
       $begin = 0;
       $end = $s2len - $s1len;
       //Initialize position
       $position = 0;
       //Do binary search of s2 for s1
       //Check left side first to find first occurance of s1
       //Check right side first to find last occurance of s1
       while($end > $begin + 1)
       {
         $middle = ceil(($begin + $end) / 2);
         $leftBegin = $begin;
         $rightBegin = $middle + $s1len;
         $leftEnd = $middle;
         $rightEnd = $end + $s1len;
         //Check left first
         if(ereg($s1, substr($s2, $leftBegin, $rightBegin - $leftBegin)))
         {
           $end = $middle;
         }
         else //(ereg($s1, substr($s2, $leftEnd, $rightEnd - $leftEnd)))
         {
           $position += $middle - $begin;
           $begin = $middle;
         }
       }
       //Resolve 1 off problems introduced by ceil
       if(ereg($s1, substr($s2, $end, $s1len))) $position++;
       //Return position 1 based
       return $position + 1;
     }
  ?>              
Php Tutorial 31-46/46 Php Tutorial 31-46/46 Reviewed by Abdul hanan on 13:42:00 Rating: 5

No comments:

Powered by Blogger.