Advertisement

Advertisement


PHP Tutorial

As we have already studied in the previous page, PHP is a hugely popular open source server side scripting language. We can create dynamic webpages in it which enhances user experience on the website.

In JavaScript we talked about client-side scripting language but here we are using the term server-side scripting language. Both JavaScript and PHP are scripting language but the difference comes in their execution place. Yes JavaScript runs on user's local machine but a PHP script will always get executed on the server. So, the server which anyone would use to run their PHP script has to PHP enabled. It should have the capability to run a PHP script on it. A browser doesn't know how to interpret PHP Script so the server first interprets the PHP Script and then sends the interpreted script to the browser in the form of plain HTML code.

PHP tutorial

A new catch in using PHP is that because it is a server side scripting language so, it needs a server's environment even when you are developing the script in your local machine. No need to worry about this as there are many softwares which you can use to create a server environment in the local host, which is your PC, and then start writing PHP code. But when your website is live then all the code will be interpreted by the server itself and the client's system need not to have a server installed, only a browser is enough.

PHP can also be integrated with many popular databases, which includes MySQL, Oracle, Microsoft SQL Server, etc. The latest major version of PHP is PHP7, it has .php file extension.

This PHP tutorial will help you to learn PHP in the most simple and easy way with proper examples in each concept. We also have an online PHP editor which you can use to develop PHP applications right from our websites and can also see the output of your code. Stay with us and follow this PHP course to start developing PHP applications and become a PHP developer.


What is a Scripting Language?

A scripting language is a set of instructions that is interpreted line by line at runtime. Unlike any programming language, scripts are usually embedded within other languages code and enhances their capabilities and features.

The main objective of using a script is to enhance the user experience and application's performance. Server Side and Client Side are the two types of scripting languages. Server side are interpreted on the server while the other one is interpreted on the browser installed on client's system.


Programming Language Vs Scripting Language

Programming language Scripting language
It is used to develop complete applications. It mostly handles routine tasks.
The code is compiled by a compiler. The code is interpreted by an Interpreter.
It in an independent language and not need to be embedded into other languages. It is embedded with other languages to run.
It creates an executable file after compiling. It does not create a file type.
It is used to develop various applications such as desktop, web, mobile etc. Mostly used for web development.
Examples: C, C++, etc. Examples: JavaScript, PHP, Python, etc.

History of PHP

Rasmus Lerdorf, the creator of PHP, once said-

"I don't know how to stop it, there was never any intent to write a programming language. I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way."
Sounds easy right, well we all know it would have been quite a hectic process. In 1994, Rasmus Lerdorf wrote the very first version of PHP. It was written in C language to replace the PERL code he has benn using on his personal homepage. In 1995, he released the first version of PHP for public use and called it 'Personal Homepage'. After this, the 3rd version was released in 1998 and also got renamed to 'Hypertext Preprocessor'.

PHP was slowly becoming popular day by day and also the newer versions got released frequently which enhanced its features even more. The latest version was released in 2015 which is called PHP7.


What You Can Do with PHP

PHP can do many things because of its big library and inbuilt functions and statements. It is the most widely used language for web development globally right now. Some applications of PHP are:

  • It can develop GUI based Applications.
  • Dynamic web pages an be created using PHP.
  • It can create and manage Cookies efficiently using its pre-built functions.
  • Data representation is also possible in PHP using tools like Image_Graph.
  • PHP can develop Web Content Management System, one biggest example is Wordpress.
  • It supports various databases like MySQL, Oracle, etc. and can manage them accordingly.
  • PHP supports file handling on the server i.e., it can create, open, read, write, delete, and close files on the server.
  • It provides encryption of data which provides safe transmission of the data.

Other than these, PHP can create flash movies, can do image processing and graphic design also. It can also create PDF files by using PDFLib library. We will see many applications of PHP in the tutorial futher.


Advantages of PHP over Other Languages

If you're familiar with other server-side languages like ASP.NET or Java, you might be wondering what makes PHP so special. There are several advantages why one should choose PHP.

  • Simple and easy to learn: It is similar to C and JAVA to an extent and that's why people find it easier to learn PHP as their first scripting language. It is considered as a preferable choice for the beginners.
  • Open source: It is free to use, their are no licenses, restrictions, or royalty fees involved which helps in developing a website at minimal cost. As it is open source, developers from all around the world can maintain and develop it further.
  • Portability: PHP supports different platforms such as Windows, Linux, Mac OS, etc. and it is also compatible with majority of popular servers such Apache, IIS, etc.
  • Frameworks: It has a wide variety of Frameworks which can be used to develop PHP applications easily. Examples : CodeIgniter, Laravel, CakePHP, etc.
  • Quality Support: Since PHP is an Open Sourced project many blogs and forums are available online which can solve queries. Finding help related to PHP is very easy.
  • Integration: It can easily gets integrated with many web applications and that's why any industry you can think of like fashion, banking, health, government, etc uses PHP applications and its modules and API's.

Famous Websites build in PHP:

PHP is right now unambiguously the most used Scripting language to develop dynamic websites. Some popular websites developed in PHP are:

  • Social Websites: Facebook, Digg
  • Blogs/CMS: Wordpress, Drupal
  • E-commerce: Magento, Opencart
  • Social Websites: Yahoo, Wikipedia, Paytm, Flickr

Creating a PHP File

The first question that arises is that, what would be the extension of a file containing PHP code? let's see. File extension are very important as it helps the system to identify the type of a file so that it could run that file in a suitable program. In PHP also if we want the server to identify our PHP files and scripts, the file should be saved under .php extension.

The next thing is how to embed PHP code within other languages' code. Well, if we talk about web development, PHP was designed to work with HTML, it can easily be embedded into the HTML code like this:

<HTML> <PHP Code> </HTML>

The PHP code will always be written inside HTML tags if you want to embed it.

A PHP file without HTML tags can also be created, these are called Pure PHP files because these files only contain PHP's code.

Always remember that whenever you write PHP code it will be written inside <?php ...... ?> tag. It is same like writing JavaScript inside <script> tags. Even when you embed the PHP code with HTML you have to write it inside the php tags otherwise the server will not identify the PHP code. Only the code enclosed within php tags will be interpreted by the server.

Note : PHP is case sensitive language, “$NAME” is a different variable from “$name”. But PHP tags are not case-sensitive, they can be written on any case but it is strongly recommended to use lower case only.

.

"Hello World" Script in PHP:

Let's see the very first script of PHP, the 'HELLO WORLD' script. The most simple and the first script/program any programmer creates on any language. For this, the PHP code has to be embedded with the HTML code. Look at the example below:


<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!">
?>
<body>
<html>

Output

Hello World!

As you can see the php script is written inside the dedicated PHP tags. Now the server will interpret this code and return it in plain HTML format to the browser. Our website is also written in PHP so if you will go through the source code in console you will only find the HTML code because the PHP code is already interpreted by our servers and only the plain HTML code is returned to your browser after the interpretation. Check it out.

There are more than one syntax in which you can write the PHP script but the one we used in the 'HELLO WORLD' example is used widely. Let's see other syntax:

Syntax
< ?php PHP code goes here ?>
<? PHP code goes here ?>

From next chapter we will start setting up the server environment in your local machine. Stay with us.









Become a Patron!

Note: If the updated content is not visible to you please refresh the cookies by pressing CTRL + Shift + R.