PHP Programming examples for beginners


History of PHP :

Rasmus Lerdorf
               PHP is a popular general-purpose scripting language that together constitutes the name of one of the world’s most popular programming languages for Web development, the PHP Hypertext Preprocessor. PPHP is a server side scripting language that can be written easily in HTML <tags>. It was developed by Danish-Canadian programmer Rasmus Lerdorf in 1994. All implementations of PHP are now produce and built by the PHP group.

               PHP is used for building e-commerce web applications, dynamic content, databases and sites.PHP is integrated with many popular databases that allow you to create and manage all types of dynamic web applications, including MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server. MySQL servers are one of the more preferred databases that, once launched, also perform very complex queries with a heavy result set in record-setting time.

               A large number of major protocols support POP3, IMAP and LDAP in PHP. PHP4 added support for Java to create mobile apps in which complex databases were easily configured and distributed to object architectures (COM and CORBA), creating the potential for n-tier development for the first time.


Released Versions of PHP 

Versions NumberRelease Dates
PHP 1.0
8 June 1995
PHP 2.0
1 November 1997
PHP 3.0
6 June 1998
PHP 4.0
22 May 2000
PHP 4.1
10 December 2001
PHP 4.2
22 April 2002
PHP 4.3
27 December 2002
PHP 4.4
11 July 2005
PHP 5.0
13 July 2004
PHP 5.1
24 November 2005
PHP 5.2
2 November 2006
PHP 5.3
30 June 2009
PHP 5.4
1 March 2012
PHP 5.5
20 June 2013
PHP 5.6
28 August 2014
PHP 7.0
3 December 2015
PHP 7.1
1 December 2016
PHP 7.2
30 November 2017
PHP 7.3
6 December 2018
PHP 7.4
28 November 2019
PHP 8.0
December 2020


PHP Features 

  • Performance 
  • Open Source 
  • Familiarity with syntax 
  • Embedded 
  • Platform Independent 
  • Database Support 
  • Error Reporting 
  • Loosely Typed Language 
  • Web servers Support 
  • Security 
  • Control 


What is PHP ?

              PHP is an open-source, interpreted, and object-oriented and server side scripting language. It is stand for Hypertext Preprocessor (earlier called, Personal Home Page). PHP can be easily embedded in HTML <tag> as it executes on the server-side. It is used to develop Static,Dynamic web applications.


PHP OOPs (Object Oriented Programming System) Concepts

  • Class : A class is a  prototype from which objects are constructed.A class is a type of class such as a vehicle. If seen, there are many types of vehicles in it. It is made by adding many tools and materials to make it. As soon as classes are created from methods and properties. A class is defined in the program by using the class keyword, and followed by the name of the class and uses curly braces ({}).
          Basic Syntax - Example :

          <?php
              class Vehicle {
              // Properties
              public $name;
              public $color;

             // Methods
             function set_name($name) {
             $this->name = $name;
              }
              function get_name() {
              return $this->name;
                 }
               }
           ?>

  • Object : Object is most important concept of OOps. Defines a class and then constructs all objects under it. Classes without objects do nothing because the object is needed when the class is to run. Which you create using the new keyword. The objects are also known as an example.
          Basic Syntax - Example :

          <?php
              class Fruit {
         
               public $name;
               public $color;


              function set_name($name) {
              $this->name = $name;
               }
              function get_name() {
              return $this->name;
                  }
               }

              $apple = new Fruit();
              $banana = new Fruit();
              $apple->set_name('Apple');
              $banana->set_name('Banana');

              echo $apple->get_name();
              echo "<br>";
              echo $banana->get_name();
              ?>

  • Inheritance : It lets subclass inherits characteristics of the parent class. Parent class decides what and the way its properties/methods to be inherited by declared visibility.Re-usability– variety of kids, can inherit from a similar parent. this is often very useful once we need to provide common functionality like adding, updating and deleting data from the database.
          Basic Syntax - Example :

          <?php
              class Vehicle {

                 public $name;

                 public function start() {
                 echo $this->name. " - Engine start...<br/>";
                 }
              public function stop() {
                   echo $this->name. " - Engine stop...<br/>";
                  }
               }
              class Car extends Vehicle {
     
                  public function drive() {
                       echo "I am " . $this->name . "<br/>";
                       echo "Lets go on a drive...";
                       } 
                 }
               class Motorcycle extends Vehicle {
     
               // motorcycle specific properties
              // and methods 
               }
 
              $car = new Car();
              $car->name = "Mercedes benz";
 
              $car->start();
              $car->drive();
              ?>


  • Data Abstraction : Abstract is a way to hide and keep  information secure. In abstraction, there must be at least one method to be declared shake but not defined. The class that inherits this abstract class needs to define the method that must be an abstract keyword that must be returned before this class for it to be an abstract class. This class cannot be accelerated. Only the class that implements abstract class methods can be instantiated. There may be more than one method that can be left undefined.
          Basic Syntax - Example :

          <?php
             abstract class Animal
               {
                   public $name;
                   public $age;
                   public function Describe()
                      {
                         return $this->name . ", " . $this->age . " years old";   
                       }
                  abstract public function Greet();
                }
             class cat extends Animal
                 {
                     public function Greet()
                          {
                              return "Lion!";   
                            }
                     public function Describe()
                         {
                               return parent::Describe() . ", and I'm a cat!";   
                          }
                    }
               $animal = new cat();
               $animal->name = "Seru";
               $animal->age = 5;
               echo $animal->Describe();
              echo $animal->Greet();

              ?>


  • Polymorphism : This word is can from Greek word poly means  "many" and morphism  means "property". Polymorphism is of two types, compile time and run time. Method overloading is the same name, but different types of properties and method overriding are the same name defined with the same properties in the parent and child class. 
          Basic Syntax - Example :

          <?php
              class Shap
                 {
                     function draw(){}
                  }
               class Circle extends Shap
                   {
                     function draw()
                       {
                          print "Circle has been drawn.</br>";
                        }
                     }
               class Triangle extends Shap
                  {
                     function draw()
                        {
                            print "Triangle has been drawn.</br>";
                         }
                      }
               class Ellipse extends Shap
                  {
                     function draw()
                       {
                          print "Ellipse has been drawn.";
                        }
                   }
 
                 $Val=array(2);

                 $Val[0]=new Circle();
                 $Val[1]=new Triangle();
                 $Val[2]=new Ellipse();

                 for($i=0;$i<3;$i++)
                    {
                       $Val[$i]->draw();
                     }
               ?>


  • Encapsulation : The wrapping up of data methods into is called Encapsulation. It is a fundamental concept in Object-Oriented Programming Language (OOP).
         Basic Syntax - Example :

         <?php
            class PHP { 

           private $userId; 
           private $pwd; 
          public function updatePwd($userId, $pwd) { 

echo("Function to update password ''. $pwd . "' for user " . $userId); 
echo "<br>"; 
         } 

         public function courseName($userId) { 

echo("Function to check course name of user ". $userId); 
echo "<br>"; 
         } 
             } 

            $obj = new PHP(); 
            $obj -> updatePwd('PHP12', 'developer54321'); 
            $obj -> courseName('PHP06'); 

           ?> 

  • Constructor :  Constructor is special type of function of a class which is automatically executed in PHP programs.
          Basic Syntax - Example :

          <?php
           class MyBaseClass
          {
function __construct()
           {
echo "I am in MyBaseClass constructor<br/>";
            }
         }
         class OtherClass extends MyBaseClass
            {
               function __construct()
                {
parent::__construct();
echo "OtherClass constructor<br/>";
                }
             }
          class AnotherThirdClass extends MyBaseClass
             {
// This will inherit the constructor of MyBaseClass
         }
         $objct = new MyBaseClass();
         $objct = new AnotherClass();
         $objct = new AnotherThirdClass();
           ?>


  • Destructor : PHP Destructor is handled by the garbage Collector which always focus over object when there are not any needed of object then it automatically destroyed. The destructor method are going to be called as soon as all references to a specific object are removed or when the item is explicitly destroyed in any order in shutdown sequence.
          Basic Syntax - Example :

         <?php
           class MyDestructableClass
               {
function __construct()
                   {
echo "I am in MyDestructableClass constructor<br/>";
$this->name = "MyDestructableClass";
                    }
function __destruct()
                    {
echo "Destroying... ".$this->name."<br/>";
                    }
               }
          class OtherClass extends MyDestructableClass
              {
         function __construct()
           {
parent::__construct();
echo " OtherClass constructor<br/>";
                 }
          }
          class OtherThirdClass extends MyDestructableClass
         {
// This will inherit the constructor of MyDestructableClass class
          }
           $objct = new MyDestructableClass();
           $objct = new AnotherClass();
           $objct = new AnotherThirdClass();
           echo "<hr/>";
               ?>



The Best PHP Frameworks for Web Development 


  • Laravel
  • CodeIgniter
  • Symfony
  • Zend
  • Phalcon
  • CakePHP
  • Yii
  • FuelPHP
  • PHPixie
  • Slim

PHP Best Editor Tools

  • Dreamweaver          
Download : Click Here

  • Notepad++          
Download : Click Here

  • NetBeans
 
Download : Click Here
  • Atom         
Download : Click Here

  • Sublime Text 3          
Download : Click Here

  • Zend Studio
Download : Click Here

  • Komodo
Download : Click Here

  • Rapidphpeditor
Download : Click Here

  • Codeanywhere          
Download : Click Here