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

Saturday 28 May, 2011

Lesson 3: Operators in PHP

Retweet this button on every post blogger
There are four basic operators in PHP.

1.Arithmetic operators    +    -   *    %.
2.Logical operator         &&   ||    !
3.Conditional operator   (exp1) ? exp2 : exp3.
4.Unary operator        ++/-- (pre/post)

these operator are very useful and used frequently in  programming.

comment style in PHP :- 
1. simple style for single line comment
   //write your comment
2. multi line comment   for large documentation
   /*this blog is for those who want to learn PHP for free*/

Generating output in PHP : - 
a simple PHP program :-
<html>
<head>
<title>My first PHP programming.</title>
<body>
<?php  //opening tag

 echo "My first PHP programming!";

 ?> // closing tag
</body>
</head>
.
Output : My first PHP programming!

the above program simply embed a small PHP code echo "My first PHP programming!";
we use either echo or print for output something from PHP code as we have done above.

for outputting a variable , e.g.
$var = "My first PHP programming!";
echo $var;

simply output My first PHP programming!


string concatenation in PHP : - 
use dot(.) to concatenate to strings , e.g.
echo "My"."Blog";

would output :- My Blog

Thursday 26 May, 2011

Lesson 2 : Some core PHP basics

Retweet this button on every post blogger
1. PHP is a case sensitive server side programming language.
2. PHP provide  browser native language support.
3. PHP is truly dynamic , object oriented programming language.
4. like browser script PHP provide value type variables, means you do not have to declare any data type declaration.
5. extension for PHP file is .PHP.

Memory locations and operators :-
1. All memory locations start with $ sign e.g. $_age, $age, $name1, $name2.
2. There is no datatype in PHP , it takes its datatype at runtime automatically and count them in bytes.
an example for  above  exlplain concept
<html>
<title>PHP</title>
<body>
<?php
$v1=20; $v2=30;

$sum = $v1+$v2;

echo $sum;
?>
</body>
</html> .  
PHP allocates 2 bytes of memory for $v1=20 and same 2 bytes for next $v2 variable.



Lesson 1 : Setting up the enviroment

Retweet this button on every post blogger
Download XAMPP from http://www.apachefriends.org/en/xampp.html
1. Using the installer version is the easiest way to install XAMPP.


2. After the installation is complete, you will find XAMPP under Start | Programs | XAMPP. You can use the XAMPP Control Panel to start/stop all server and also install/uninstall services.




from the above control panel  you can start or stop any  of the four services, this installer will assign port 80 of your computer as apache service for http server if the port is not null (assign to other services such MS IIS server) then you have to manually change the port of http server to the port of your interest the procedure fro that is as follows : 
1. go into in your XAMPP installation derive (your derive):\xampp\apache\conf 
2. locate httpd.conf and do the following
    a. change Listen 80 to Listen 8080 OR any open port your PC.
    b. change ServerName localhost:80 to ServerName localhost:8080.
    c. save the file .
3. stop the apache services once for a while a restart it again.
4 and the last check type in browser localhost:your port number e.g localhost:80 localhost:8080  


these steps has changed the port as you specified in the above operation.


                                            after selecting your language it will look like this 

   
there you are you have just completed installation  XAMPP on your PC and your are ready to build some interesting applications. 
 PHP version is 5.3.5
Mysql mysql 5.5.11.

Wednesday 25 May, 2011

Interacting with PHP

Retweet this button on every post blogger
PHP stands for Preprocessed Hypertext.
PHP is a open source web scripting  language developed by rasmus lerdorf at home, the scripting can be added within the HTML and provide a superlative performance.
PHP is a fastest growing technology today with billions of user world wide, one example of power of PHP is the FACEBOOK is developed entirely on PHP and serving millions of user. one main thing about PHP is its community developed freely available to use and develope commercial product.