image source:

PHP Building Blocks

Every building is made up of blocks which eventually forms and define the structure of the final building. In this tutorial we will be covering certain concept that makes building web applications with PHP effective and efficient.

Comparing PHP with other Web Languages or technology

Below are the following backend languages we will be making comparison with:


· PHP V/s ASP.NET: Speed & Performance

To begin with, one common misconception about website performance and speed is that the language you choose to code in determines your website’s overall performance.

In reality, however, there is very little difference between the performance of PHP websites and ASP.NET websites.

While many expert PHP coders out there will guffaw at the prospect of building a site in PHP the fact that humongous sites such as MailChimp and Facebook are written in PHP should be enough evidence to dispel any notion that PHP sites run more poorly than ASP.NET sites.

· PHP V/s ASP.NET: Scalability

Both ASP.NET and PHP web applications are extremely scalable languages.

· PHP V/s ASP.NET: Support

PHP is open source and its pool of developers is far larger than ASP.NET (which is windows based). That being said both boast vibrant communities that post regularly to online forums, so if you are looking for answers to problems, you’re likely to find both communities helpful.

· PHP V/s ASP.NET: Cost

This is the only area where PHP owns a distinct advantage over ASP.NET. PHP is an open source, and thus, completely free, while ASP.NET is owned by Microsoft and comes with a web hosting fee.

· PHP V/s ASP.NET: Popularity

PHP is more popular and in wide use than ASP.NET


· JSP is a server-side programming technology while PHP is a server-side scripting language.

· JSP is an abstraction of Java class so it can be garbage collected whereas PHP does not support garbage collection.

· JSP execution requires a servlet container like Tomcat since it is a servlet in disguise whereas PHP can run on its own as a CGI engine.

· JSP support for APIs is very huge since it’s based on Java programming language whereas PHP has limited access to APIs.

· JSP execution requires more time since it is translated into Servlet, compiled and executed whereas PHP execution requires less time than JSP.

· Mastering JSP requires knowledge of Java and HTML syntax whereas PHP being a scripting language is easier to learn and understand.

· JSPs are very good at maintaining user sessions whereas PHP destroys the user’s sessions every time.

PHP V/s Python

· Ease of Learning

Python is much easier to learn than PHP although PHP is simple to learn.

· Community support

Python and PHP, both have excellent and large community support. PHP has been in the market for quite a while, particularly for developing web applications. As a result, there is a huge community of PHP developers which is ready to provide support.

· Documentation

Extensive documentation is available for both the programming languages.

· Cost

Python and PHP are both completely free and open source.

· Library Support

Both languages have support for large libraries. PHP uses

· Speed

PHP 7.** is almost three times faster than Python.

· Debugging

Python provides a powerful debugger called PDB (Python Debugger). PDB is well documented and is easy to use, even for the beginners. PHP, on the other hand, provides XDebug package for debugging. Both PDB and XDebug provide the most commonly used debugging features — breakpoints, stacks, path mapping, etc.

PHP V/s Ruby

· Learning curve is very easy and simple to follow.

· Both languages have lot frameworks that are popular and widely used but PHP framework is more popular.

· Both languages have a large community of developers and support.

· Open source

Installation of PHP

Set Up PHP on Your Own PC

However, if your server does not support PHP, you must:

· Install a web server

· Install PHP

· Install a database, such as mysql

The official PHP website ( has installation instructions for PHP:

There are two major ways to install PHP namely Manual and all in one package (XAMPP)

Manual Installation

Step 1: Download the files

Download the latest PHP 5 ZIP package from

Step 2: Extract the files

We will install the PHP files to C:\php, so create that folder and extract the contents of the ZIP file into it.

Step 3: Configure php.ini

Copy C:\php\php.ini-development to C:\php\php.ini. There are several lines you will need to change in a text editor (use search to find the current setting). Where applicable, you will need to remove the leading semicolon to uncomment these setting.

Define the extension directory:

extension_dir = “C:/php/ext”

Enable extensions. This will depend on the libraries you want to use, but the following extensions should be suitable for the majority of applications:







If you want to send emails using the PHP mail() function, enter the details of an SMTP server (your ISP’s server should be suitable):

[mail function]

; For Win32 only.


smtp_port = 25

; For Win32 only.

sendmail_from =

Step 4: Add C:\php to the path environment variable

To ensure Windows can find PHP, you need to change the path environment variable. Open Settings, type ‘environment variables’ into the search field and open the result. Select the “Advanced” tab, and click the “Environment Variables” button.

Scroll down the System variables list and click on “Path” followed by the “Edit” button. Click “Edit text” and add ;C:\php to the end of the Variable value line (remember the semicolon).

Step 5: Configure PHP as an Apache module

Ensure Apache is not running (use net stop Apache2.2 from the command line) and open its confhttpd.conf configuration file in an editor. The following lines should be changed:

On line 239, add index.php as a default file name:

DirectoryIndex index.php index.html

At the bottom of the file, add the following lines (change the PHP file locations if necessary):

# PHP5 module

LoadModule php5_module “c:/php/php5apache2_2.dll”

AddType application/x-httpd-php .php

PHPIniDir “C:/php”

Save the configuration file and test it from the command line (Start > Run > cmd):

cd Apache2bin

httpd -t

Step 6: Test a PHP file

Create a file named index.php in Apache’s web page root (either htdocs or D:WebPages) and add this code:

<?php phpinfo(); ?>

Ensure Apache has started successfully, open a web browser and enter the address http://localhost/.

PHP Delimiters

Delimiters ¶

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~). The following are all examples of valid delimited patterns.

/foo bar/




Variable initialization with PHP

Initialization happens when a value is assigned to a variable for example a = 5;

What is Variable in PHP

Variables are used to store data, like string of text, numbers, etc. Variable values are temporary if not declared to be constant and can change at any time. Here’re some important things to know about variables:

In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.

After declaring a variable it can be reused throughout the code.

The assignment operator (=) used to assign value to a variable.

In PHP variable can be declared as: $var_name = value;

Naming Conventions for PHP Variables

These are the following rules for naming a PHP variable:

· All variables in PHP start with a $ sign, followed by the name of the variable.

· A variable name must start with a letter or the underscore character _.

· A variable name cannot start with a number.

· A variable name in PHP can only contain alpha-numeric characters and underscores (A-z, 0–9, and _).

· A variable name cannot contain spaces.

PHP Data Types

Data type specifies or defines the type of value that can be stored in a variable and what kind of operation can be performed on it and with it.

The values assigned to a PHP variable may be of different data types including simple string and numeric types to more complex data types like arrays and objects.

PHP supports total eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource and NULL. These data types are used to construct variables. Now let’s discuss each one of them in detail.


//PHP integer

$a = 123; // decimal number


echo “<br>”;

//PHP string

$a = ‘Hello world!’;

echo $a;

echo “<br>”;

//PHP Float

$a = 1.234;


echo “<br>”;

// Assign the value TRUE to a variable

$show_error = true;




An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together, for example a set of country or city names.


$colors = array(“Red”, “Green”, “Blue”);


echo “<br>”;

$color_codes = array(

“Red” => “#ff0000”,

“Green” => “#00ff00”,

“Blue” => “#0000ff”



//Classes and Object

// Class definition

class greeting{

// properties

public $str = “Hello World!”;

// methods

function show_greeting(){

return $this->str;



// Create object from class

$message = new greeting;


//PHP NUll

$a = NULL;


echo “<br>”;

//PHP Resources

$handle = fopen(“note.txt”, “r”);


echo “<br>”;


PHP Constants

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren’t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly:


It is possible to define() constants with reserved or even invalid names, whose value can (only) be retrieved with constant(). However, doing so is not recommended.


// Valid constant names

define(“FOO”, “something”);

define(“FOO2”, “something else”);

define(“FOO_BAR”, “something more”);

// Invalid constant names

define(“2FOO”, “something”);

// This is valid, but should be avoided:

// PHP may one day provide a magical constant

// that will break your script

define(“__FOO__”, “something”);


Look up on PHP documentation for more info

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

Arithmetic operators (+, -, /, *, % )

Assignment operators ( = )

Comparison operators ( ==, ===, !=, =>, <= )

Increment/Decrement operators ( ++, — )

Logical operators ( and, or, xor, &&, ||)

String operators ( +, .,)

Array operators (+, ==, ===, !=, <>)

References by Craig Buckler



Software developer |writer | librarian | (Tech is easy to Learn)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store