Friday, 10 June 2011

Lesson 9 : Post back in PHP

Retweet this button on every post blogger
The method of page post back is quite  easy concept it trip back  on the page on which our code has been placed or in another terms we can say page call itself  for further operation.
Simple example of PHP post back : 
<?php
/*
php code for handling post back.
*/
if(isset($_POST['submit']))
{
    $first = $_POST['fname'];
    $last = $_POST['lname'];
   echo "Full nmae : ".$first." ".$last;
}

?>

<html>
<head><title>Handling post back in PHP</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post" name="form">
<center>
<table>
<tr>
<td>first name</td><td><input type="text" name="fname" /></td>
<tr><td>last  name</td><td><input type="text" name="lname" /></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="go.."</td></tr>
</tr>
</table>
</center>
,</form>
</body>
</html >

 this simple program defining PHP post back as by checking in the starting of the program for posted array variable submit if it got it setted then the further script will  execute .

Another simple example on PHP post back with checkbox input : 
<?php
if(isset($_POST['submit']))
{
    $firstdigit = $_POST['fdigit'];
   $lastdigit = $_POST['ldigit'];
 
  if(isset($_POST['add']))
  {
        $sum =  $firstdigit + $lastdigit ;
  }
 
   if(isset($_POST['mul']))
  {
        $multi =  $firstdigit * $lastdigit ;
  }  
  

}


?>


<html>
<head>
<title>PHP post back</title>
</head>
<body>
<table>
<tr>
<td>First digit</td><td><input type="text" name="fdigit" /></td>
</tr>
<tr>
<td>Second digit</td><td><input type="text" name="sdigit" /></td>
</tr>
<tr>
<td>&nbsp;</td><td>Add<input type="checkbox" name="add" <?php if(isset($_POST['add'])) echo "checked"; ?> />Multiply<input type="checkbox" name="mul" <?php if(isset($_POST['mul'])) echo "checked"; ?> /></td>
</tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="process" /></td></tr>

<tr>
<td>Results</td>
<td>&nbsp;</td>
</tr>



<tr>
<td>Addition</td>
<td><?php if(isset($sum)) echo $sum; ?></td>
</tr>


<tr>
<td>Multipliaction</td>
<td><?php if(isset($multi)) echo $multi; ?></td>
</tr>


</table>
</body>
</html>

Thursday, 2 June 2011

Lesson 8 : PHP form handling

Retweet this button on every post blogger
Sending form data from one page to another for further processing in an quite simple task in PHP, By setting up only form input type name attribute we can extract them at the server side using PHP global array.
Some example global  array in PHP which we often use in form handling are :

$_POST[]
$_GET[]
$_SERVER[]
$_REQUEST[]
$_COOKIE[]

The global arrays are all associative arrays. This means the value is associated with a name that is the index key for the contents and, sensibly, describes what the value represents. For instance,$_SERVER["SCRIPT_NAME"] contains the URL path to the current page.

Sometimes you can also access the values of the array directly by variable name. This varialbe name is the same as the index key for the array. Thus, the variable name for $_SERVER["SCRIPT_NAME"] would be$SCRIPT_NAME. this can be achieve by extracting the array. 

I think this is enough for any beginner. lets look at the example.
page1.php
<html>
<head><title>PHP from handling.</title></head>
<body>
<form action = "page2.php" method="post">
<center>
<table>
<caption>General info</caption>
<tr>
<td>First Name</td><td><input  type="text" name="fname" /></td>
</tr>
<tr>
<td>Last Name</td><td><input  type="text" name="lname" /></td>
</tr>
<tr>
<td>Address</td><td><input  type="text" name="add" /></td>
</tr>
<tr><td>&nbsp;</td><td><input  type="submit" name="submit"  value="Go.."/></td></tr>

</table>
</center>
</form>
</body>
</html> 
                                                                    --Output--
page2.php
<html>
<head><title>PHP from handling.</title></head>
<body>
<center>
<table>
<caption>General info</caption>
<tr>
<td>First Name</td><td><?php echo $_POST['fname']; ?></td>
</tr>
<tr>
<td>Last Name</td><td><?php echo $_POST['lname']; ?></td>
</tr>
<tr>
<td>Address</td><td><?php echo $_POST['add']; ?></td>
</tr>
</table>
</center>
</body>
</html>   
                                       
                                                                   --Output--
 for sake of simplicity we also extract $_POST[] array as extract($_POST);, and simply use the form input names as POSTED variables in pag2.php.


































Wednesday, 1 June 2011

Lesson 7 include and require

Retweet this button on every post blogger

The include() statement includes and evaluates the specified file.

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing. The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error.

An example :-
vars.php
<?php

$color = 'green';
$fruit = 'apple';

?>

test.php
<?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?>

require()

require() is identical to include() except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.


require_once()

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

include_once()

The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include()statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.



Tuesday, 31 May 2011

Lesson 6 : Object Oriented PHP

Retweet this button on every post blogger
PHP is object oriented programming language it includes in PHP since PHP4 and improved to full object model in PHP5 which provide visibilty, absract, and final classes and methods additional magic methods ,interfaces, cloning and typehinting.

PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object.


An example of simple class : - 
<html>
<head><title>Object oriented PHP </title></head>
<body>
<?php
class example 

{
   public $var1;
  public $var2;


   function getmultiply() 
  {
     $multi = $this->var1*$this->var2;
    return $multi;
  }


}
?>



<?php
$obj = new example(); ///creting class object.
$obj->var1 = 2;
$obj->var2 = 3;
echo $obj->getmultiply();
?>
</body>
</html>



output : - 
6


To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation.


access specifiers : -
there are basically three types of access specifier in PHP
1. public - data member and methods defined as public are available to use within and outside of the class.


2. protected - data member and methods defined as protected are available only to use in child classes .


3. private -  data member and methods defined as private are available only to use within the  class itself that defines it .


in the example above define class variables $var1 $var2 are public data member. 


Inheritence : -
inheritence is a greate concept of oop programming which gives reusability and allows child classes to use existing methods and data member of the parent class in child class.


parent class :- class from which we are inheriting methods and data mmeber. methods and data members must be defined as public or protected.


child class :- class in which we are  inheriting parent class .


for inherit a class within the child class we use a keyword called extends.


An example of inheritence :- 



<html>
<head><title>Object oriented PHP </title></head>
<body>
<?php
class example 
{
   public $var1;
  public $var2;

   function getmultiply() 
  {
     $multi = $this->var1*$this->var2;
    return $multi;
  }
}

class   add extends example
{
   function addme()
      $sum = $this->var1+$this->var2; // data member form example class.
     return $sum;
}
?>

$child = new  add()
$child->var1 = 2;
$child->var2 = 3;
echo  $child->
addme();


</body>
</html>
output :- 5

Abstract class :--
 An abstract is a class incomplete class which can not be instantiated , it is required for an class to be abstract that it must contain at least one method within it defined as abstract. An abstract method is nothing but only declaration  of the method and its implementation within the second class which inherit it. 
        
<html>
<head><title>Object oriented PHP </title></head>
<body>
<?php
abstract class example 
{
   public $var1;
  public $var2;

   function getmultiply() 
  {
     $multi = $this->var1*$this->var2;
    return $multi;
  }
abstract public function getdivide(); ///only declaration of 
}

class   add extends example
{
   function addme()
      $sum = $this->var1+$this->var2; // data member form example class.
     return $sum;

   function 
getdivide()


  {

      $divide  = $this->var1/$this->var2;

       return $divide;

  }

}
?>
<?php 
$child = new  add()
$child->var1 = 2;
$child->var2 = 3;
echo  $child->
addme();

echo "<br/>";

echo $child->
getdivide();

?>


</body>
</html>








Monday, 30 May 2011

Lesson 5 : PHP Associative array

Retweet this button on every post blogger
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

syntax :-
array( key  => value ,..... )
// key may be an integer or string.
//value may be any value of any type.

e.g.

$student  =  array("English"=>90, "Math"=>77, "Physics"=>80);

Traversing and extracting  associative array : - 


<html>
<head><title>PHP associative array</title></head>
<body>
<?php
$student  =  array("English"=>90, "Math"=>77, "Physics"=>80);

foreach ($student as $key=>$value) {
 
      echo $key." : ".$value;
}
?>
</body>
</html>



output for the above simple program is :-
English : 90
Math : 77
Physics : 80


Extraction of associative array : -


PHP extract keyword changes all keys of associative array into the variables named them with the key name and assign the value.

Exmaple ;-



<html>
<head><title>PHP associative array</title></head>
<body>
<?php
$student  =  array("English"=>90, "Math"=>77, "Physics"=>80);

extract($student);
echo "English : ".$English;
echo "<br/>";
echo " Math :".$Math;
echo "<br/>";

echo "Physics : ".$Physics;

//alternatively we can aslo use this.
echo "English : ".$student['English'];
echo "<br/>";
echo "Math : ".$student['Math'];
echo "<br/>";
echo "Physics : ".$student['Physics'];


?>
</body>
</html>


Output :- 
English : 90
Math : 77
Physics : 80 
  

Sunday, 29 May 2011

Lesson 4 PHP array

Retweet this button on every post blogger
In PHP it is very simple to define array and traverse them easily.

Example : 
<html>
<head><title>PHP array</title></head>
<body>
<?php
$name = array("john", "bill", "steve"); // an simple array

for($i=0; $i<count($name); $i++ ) {
    echo "$name[$i]<br/>";
}
?>
</body>
</html>

OUTPUT :- 
john
bill
steve

though  this is a very basic type of array traversing PHP provide some advanced mean of array traversing using foreach loop
An example using foreach loop : -

<html>
<head><title>PHP array traverser using foreach loop</title></head>
<body>
<?php
$name = array("john", "bill", "steve"); // an simple array
   foreach ($name as $var) {
        echo "$var<br/>";
  }
?>

</body>
</html> 
Output :- 
john
bill
steve
working of foreach loop : -
1. first it implicitly determine the length of array
2 than it traverse array till the end.
3. at each iteration it creates a variable $var for each element of passed array.
Simple!! 
array traversing using foreach loop require less number of steps than conventional for loop.

Multi dimensional array traversing : -
multi dimension array are simply array within one array.
e.g.
$student  =  array(array("Math", "Physics", "Chemistry"), array("90","76","66"));
here is an student array with size two containing two secondary array for subject ans marks.
<html>
<head><title>PHP Multi dimensional array traversing</title></head>
<body>
<?php
$student  =  array(array("Math", "Physics", "Chemistry"), array("90","76","66"));
// an simplemulti dimesional array

$sub = 0; // index of subject array.
$marks = 1; // index of mark array.

for ($i=0; $i<count($student[$sub]); $i++)
{
    echo $student[$sub][$i]." : ".$student[$marks][$i];
    echo "<br/>";    
}
?>
</body>
</html> 


Output will be : 
Math  :  90
Physics : 76
Chemistry : 66

Adding elements to array dynamically :-
  <html>
<head><title>PHP adding  array elements dynamically</title></head>
<body>
<?php
$example = array("a","b","c");

$example[] = "d";
foreach ($example as $new) {
    echo "$new<br/>";
}
?>

</body>
</html>
Output : - 
a
b
c
d