Introduction to PHP

Introduction to PHP

Hi Guys

Let me teach about PHP in easy steps. First of let me explain what is Programing Language.

“ A Programing language is artificial language used to express  computer that can be performed with a machine, particularly Computer.”

  1. PHP is recursive acronym stands for  Personal Home Page, it is now said to stand for PHP: Hypertext Preprocessor
  2. PHP Is used for developing 3 types of applications
    • Command Line scripting
    • Desktop (or) GUI
    • Web Applications
  3. The main purpose of introducing PHP is to develop Web applications.
  4. PHP programs saved with an extension of .PHP
  5. Actually all the web applications runs in an request and respond mechanism (or) client server mechanism.
  6. The Programs which present in the client machine are sending requests to the server and getting response from server is called client side programs.
    examples: Firefox, Chrome, Internet explorer
  7. All the PHP programs gets executed in server and will give a response back to the client (Browser) in pure HTM format.
  8. Every PHP program contains HTML Tags plain text and PHP scripts.
  9. A web server is an application running in the server machine.
  10. PHP programs can be Placed in most of web servers.
    Example: IIS, ApacheNGINX
  11. PHP can run almost any Database.
  12. PHP is platform independent.
  13. At the time of PHP script execution in webserver the PHP script converts into “opcode” (Operational code) and send it to interpreter.
  14. The PHP interpreter is ZEND it is also called PHP parser.
  15. This Interpreter converts the “opcode” to HTML.
  16. For more details about History Click here for Wikipedia.
  17. On Next Class we will discuss about Instalation of PHP.
Installing PHP in  local system with XAMPP

Installing PHP in local system with XAMPP

HI guys

If we want work with PHP we need following products in our system .

  1. PHP
  2. Database server
  3. mail Server
  4. FTP client (Used to upload and download the content to the server)
  5. A tool for remote Database administration.

The following installers can be used to install all the above products in our system.



The above listed are different installers different operating systems.

in our case we are selecting XAMPP in this X means any operating system. (i am using windows operating system)

To download go the following URL

and select the installer, by the time of publishing the XAMPP version is 1.8.0

Installing XAMPP

Double click on Downloaded file

Select language  Press OK

Select NEXT

Select the installation Folder, by default it will create a folder “XAMPP” in your operating system drive as shown above

In the next screen it will shows the options as shown like above

  1. Create desktop Icon (it is recommended )
  2. Create Apache Friends XAMPP folder in Start menu (It is also recommended )
  3. Next service section (SERVICE SECTION) If you select any one of them they will start automatically at the time of windows boot it is not preferable better don’t select
  4. Press Install

Now the XAMPP is going to install. it will take little time as per your system environment.



After the Installation complete XAMPP will ask for to start the Control Panel

Press yes you will get screen like above.


Ok Guys now the XAMPP has installed successfully on your system. to check this

start the APACHE  server by clicking on the “Start” button shown in the below image 


Now go to your browser and type as below


now you will get the XAMPP default page.


If you have problem to start the apache server

Check your System has Skype, Teamviewer, Oracle, or any testing tool softwares , if you found any softwares try to uninstall those

because apache by default runs in in port 80, i want to change it to 8082

If you dont want to uninstall and the softwares then change the port number for apache.

to do this follow this simple steps

Open c:\xampp\apache\conf\ open “httpd.conf” file with text editor (notepad)

search for the word which mentioned “Listen :80”

change the “listen 80” to another number Like “listen:8082

and also change the “ServerName localhost:80″ to “ServerName localhost:8082”

now save the “httpd.conf” file and restart the apache server

go to your web browser type “localhost:8082

now you will get XAMPP page it will ask for language selection

select your language and you will get xampp page it will shows the xampp version, PHP version , … it was installed


Conditional Statement

Conditional Statement

Everybody knows how the Statement works in programing languages as PHP developed from C language lets directly go for sample programs.

We have 4 Conditional Statement in PHP they are

  1. Switch statement
  2. IF statement
    1. Simple If
    2. IF – else
    3. IF – elseIF – else
    4. Nested IF

Switch Statement:-



switch ($x)


Case “a”;

echo “A”;


Case “B”;

echo “B”;


Case “c”;

echo “C”;






output will be


Control statements (or) Loops

  1. while loop
  2. do while loop
  3. for loop
  4. for each loop

While Loop:-

Write a program for to display the first 10 natural numbers ?


echo “The first 10 natural numbers are”.”<br>”;




Echo $x.”<br>”;




The first 10 natural numbers are











and now just check the below program what will be the out put.




echo $x.”<br>”;


predict what will be the output of the Program ????

Output will be



  1. Write a program to to display first 10 even numbers ?
  2. Write a program to to display the multiplication table as shown below ?

    5 * 1 = 55 * 2 = 10


    5 * 10 = 50

  3. Write a program to to display odd numbers between x and y ?
  4. Write a program to to display to display reverse of a number ?

    ex: if we taken input “1234” the out has to be “4321”

  5. Write a program to to display numbers in words ?

    ex: if we taken number as “1234” the out has to be “ one two three four”

Local and Global variables in PHP

Local and Global variables in PHP

Local variable:

  1. A variable which is specified in a function is known as local  variable.
  2. local variable is accessible only with in the function where it was declared.
  3. the life time of a local variable is with in the function.

Global Variable:

  1. A variable which is specified outside of a function is known as global variable.
  2. Global variable can be accessed anywhere in the program.


It was an built in multi dimension array in php that stores GETDATA,POSTDATA, cookies, uploader files, error messages occurred in execution and also stores global variables.

The variables present inside $_GLOBAL is called global variables.

Syntax:  $_GLOBALS[‘x’]


Global is keyword of php used for declaring a global variable , a variable declared by using keyword global cant be initialized.


function fun()
global $z;
echo $x.”—”.$y.”<br>”;
echo <br>;
echo $x.”—”.$y.”—”.$z;
PHP Class abstraction and Interfaces

PHP Class abstraction and Interfaces

Class Abstraction

PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method’s signature they cannot define the implementation.

When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.

abstract class AbsClass
public $a=10,$b=20;
function addNum($x=0,$y=0)
echo "Sum = ".($x+$y)."<br>";
abstract function subNum($x=0,$y=0);
class AbsImpl extends AbsClass
function subNum($x=0,$y=0)
echo "Diff = ".($x-$y);
echo "<h1>";
$obj=new AbsImpl();


Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.

Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

All methods declared in an interface must be public, this is the nature of an interface.

To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.

interface Inter1



PHP String handling functions

PHP String handling functions

we have a lot of php predefined constants for string handling. lets have a look on few of them

  1. strlen()
    this function was used to find the length of the string.
    Syntax: strlen(variable name)
  2. str_replace()
    used to replace a part of string with another string.
    Syntax: str_replace(search,replace,[number of replaces])
  3. str_ireplace()
    It was same as str_replace but it was case sensitive.
    Syntax: str_replace(search,replace,[number of replaces])
  4. trim()
    it was used to remove the empty space on both sides of a string
    Syntax: trim(variable name)
  5. strrev()
    It is used to reverse the string
    Syntax: strrev(variable name)
  6. strstr()
    it is used to search the first occurrence of string inside another string.
    Syntax: strstr(search, replace)
  7. stristr()
    same as strstr() but it was case sensitive.
    Syntax: strstr(search, replace)
  8. nl2br()
    Used to add line breaks (<br>) inside a string in place of  \n or \r
  9. str_split()
    used to split the string into an array
    Syntax: str-split(string [,length])
    lenghth is an optional, each character is stored as an array element default is 1.
  10. substr()
    used to collecting a part of String.
    syntax: substr(string, start index,[length])
  11. substr_count()
    for count occurrence of substring in main string
    syntax: str_count(string,substring,[start position],[length])
  12. str_repeat()
  13. used to repeating a string number of times.
    syntax: str-repeat(string, repeat count)
  14. strtolower()
    convert the string into lower case of alphabet.
    syntax: strtolower(string)
  15. strtoupper()
    convert the string into upper case of alphabet.
    syntax: strtoupper(string)
  16. strcasecmp()
    it was used to case insensitive comparison. this function returns “0” if both strings are equal otherwise it will return the numerical difference between first non matching characters.
    The value wil be +ve or -ve
    syntax:  strcasecmp(string1,string2)
  17. strcmp
    It was same as strcasecmp but it was case sensitive.
    it will return 0 if both are equal
    it will return 1 if string1>string2
    it will return -1 if string1<string2
  18. str_pad()
    it was used to add extra padding with specified length to the string.
    Syntax: str_pad(string,length,[pad string][pad_type])
    where PAD_TYPE is a constant of php it will be either of the php
Example :

$x="psd2web ";

echo $x."<br>";
echo strlen($x)."<br>";
//echo str_replace(k,kk,$x)."<br>";
$str=" sunil \n kumar";
echo trim($str);
echo strlen($str)."<br>";
echo strrev($str)."<br>";
echo $z=$x.$str."<br>";
echo strstr($x,"i")."<br>";
echo nl2br($str)."<br>";
echo print_r($k)."<br>";
echo substr($x,3)."<br>";
echo substr_count($x,"u")."<br>";
echo str_repeat($x, 10)."<br>";
echo strtoupper($x)."<br>";
echo strtolower($x)."<br>";
echo strcmp("Sunil", "sunil")."<br>";
echo strcasecmp("Aunil", "Cunil")."<br>";
$new= str_pad($x,5);
echo $new.$x;


more string functions…