PHP Programming Tutorial With Example

Introduction To PHP

The PHP Stands for Hypertext Pre-processor. PHP is a programming language that allows web developers to create dynamic content that interacts with databases.

PHP is basically used for developing web based software applications. PHP is probably the most popular scripting language on the web. It is used to enhance web pages.

PHP is known as server-sided language. That is because the PHP dosen't get executed on the client's computer, but on the computer the user had requested the page from. The results are then handed over to client, and then displayed in the browser.


Syntax:

A PHP script starts with the <? php and ends with the ?> tag.

<?php
// Some code to be executed
echo "Hello, world!";
?>

Features of PHP:

  • PHP is a server side scripting language that is embedded in HTML.
  • PHP was originally developed by the Danish Greenlander Rasmus Lerdorf, and was subsequently developed as open source.
  • It is used to managed dynamic content, databases, session tracking, even built entire e-commerce sites.
  • it is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
  • PHP Suports a large number of protocols such as POP3, IMAP, and LDAP, PHP4 added support for Java and distributed object architectures(COM and CORBA), making n-tier development a possibility for the first time.
  • PHP language tries to be as forgiving as possible.
  • PHP syntax is C-Like.

Common uses of PHP

  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • PHP can handle forms, i.e. gather data from files, save data to a file, through email the user can send data, return data to the user.
  • The user can add, delete, and modify elements within the database through PHP.
  • They can accesss cookies variables and set cookies.
  • Using PHP, the user can restrict users to access some pages of the website.
  • It can encrypt data.

Working of PHP

When the client request a PHP page residing on the server, The server first performs the operations mentioned by the PHP code of the page. Then is sends the output of the PHP page in HTML format.

So when the user views the source code of the page, it will be full of HTML tags. All the work is done at the server side.

Simple Example:

<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>

Result:

  This is a $string with my $name in it.
  This is a cup with my coffee in it.