PHP(Hypertext Preprocessor) tutorial by Rushikesh

  1. Php history
  2. Syntax and Variable
  3. Data Types and Constants
  4. Loops
  5. Features
  6. Features
  7. Oops
  8. Operators


























Features of PHP

The main features of php is; it is open source scripting language so you can free download this and use. PHP is a server site scripting language. It is open source scripting language. It is widely used all over the world. It is faster than other scripting language. Some important features of php are given below;


Features of php

It is most popular and frequently used world wide scripting language, the main reason of popularity is; It is open source and very simple.

  • Simple
  • Faster
  • Interpreted
  • Open Source
  • Case Sensitive
  • Simplicity
  • Efficiency
  • Platform Independent
  • Security
  • Flexibility
  • Familiarity
  • Error Reporting
  • Loosely Typed Language
  • Real-Time Access Monitoring


It is very simple and easy to use, compare to other scripting language it is very simple and easy, this is widely used all over the world.


It is an interpreted language, i.e. there is no need for compilation.


It is faster than other scripting language e.g. asp and jsp.

Open Source

Open source means you no need to pay for use php, you can free download and use.

Platform Independent

PHP code will be run on every platform, Linux, Unix, Mac OS X, Windows.

Case Sensitive

PHP is case sensitive scripting language at time of variable declaration. In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.

Error Reporting

PHP have some predefined error reporting constants to generate a warning or error notice.

Real-Time Access Monitoring

PHP provides access logging by creating the summary of recent accesses for the user.

Loosely Typed Language

PHP supports variable usage without declaring its data type. It will be taken at the time of the execution based on the type of data it has on its value.



Connection Using PHP Script

PHP provides mysql_connect() function to open a database connection



connection mysql_connect(server,user,passwd,new_link,client_flag);



Create a Database using PHP Script

PHP uses mysql_query function to create or delete a MySQL database.



bool mysql_query( sql, connection );



Selecting a MySQL Database Using PHP Script

PHP provides function mysql_select_db to select a database. It returns TRUE on success or FALSE on failure.


bool mysql_select_db( db_name, connection );



Deleting Data Using a PHP Script

You can use the SQL DELETE command with or without the WHERE CLAUSE into the PHP function – mysql_query(). This function will execute the SQL command in the same way as it is executed at the mysql>



DELETE FROM table_name [WHERE Clause]



PHP Functions

PHP function is a piece of code that can be reused many times. It can take input as argument list and return value. There are thousands of built-in functions in PHP.


Advantage of PHP Functions

Code Reusability: PHP functions are defined only once and can be invoked many times, like in other programming languages.

Less Code: It saves a lot of code because you don’t need to write the logic many times. By the use of function, you can write the logic only once and reuse it.

Easy to understand: PHP functions separate the programming logic. So it is easier to understand the flow of the application because every logic is divided in the form of functions.

PHP User-defined Functions

We can declare and call user-defined functions easily. Let’s see the syntax to declare user-defined functions.


  1. function functionname(){
  2. //code to be executed
  3. }



PHP Functions Example

File: function1.php

  1. <?php
  2. function sayHello(){
  3. echo “Hello PHP Function”;
  4. }
  5. sayHello();//calling function
  6. ?>


Hello PHP Function

PHP Function Arguments

We can pass the information in PHP function through arguments which is separated by comma.

PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length argument list.

Let’s see the example to pass single argument in PHP function.

File: functionarg.php

  1. <?php
  2. function sayHello($name){
  3. echo “Hello $name<br/>”;
  4. }
  5. sayHello(“Sonoo”);
  6. sayHello(“Vimal”);
  7. sayHello(“John”);
  8. ?>


Hello Sonoo
Hello Vimal
Hello John

PHP Call By Reference

Value passed to the function doesn’t modify the actual value by default (call by value). But we can do so by passing value as a reference.

By default, value passed to the function is call by value. To pass value as a reference, you need to use ampersand (&) symbol before the argument name.

Let’s see a simple example of call by reference in PHP.

File: functionref.php

  1. <?php
  2. function adder(&$str2)
  3. {
  4.     $str2 .= ‘Call By Reference’;
  5. }
  6. $str = ‘Hello ‘;
  7. adder($str);
  8. echo $str;
  9. ?>


Hello Call By Reference

PHP What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.

Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the PHP code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time



Define a Class

A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:


class Fruit {
// code goes here…

Define Objects

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.

Objects of a class is created using the new keyword.



class Fruit {
// Properties
  public $name;
public $color;
// Methods
  function set_name($name) {
$this->name = $name;
function get_name() {
return $this->name;
}$apple = new Fruit();
echo $apple->get_name();
echo “<br>”;
echo $banana->get_name();

PHP – The __construct Function

A constructor allows you to initialize an object’s properties upon creation of the object.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.



class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
function get_name() {
return $this->name;
}$apple = new Fruit(“Apple”);
echo $apple->get_name();

PHP Iterables

PHP – What is an Iterable?

An iterable is any value which can be looped through with a foreach() loop.

The iterable pseudo-type was introduced in PHP 7.1, and it can be used as a data type for function arguments and function return values.


Use an iterable function argument:

function printIterable(iterable $myIterable) {
foreach($myIterable as $item) {
echo $item;
$arr = [“a”, “b”, “c”];

PHP Namespaces

Namespaces are qualifiers that solve two different problems:

  1. They allow for better organization by grouping classes that work together to perform a task
  2. They allow the same name to be used for more than one class

Namespaces are declared at the beginning of a file using the namespace keyword:


Declare a namespace called Html:

namespace Html;
echo “Hello World!”;
namespace Html;