PHP is a programming language most widely used for building websites. As I am developer and lecturer both, have prepare article suitable to student and developer.
In this tutorial, I will explain basic about php listed here:
- History of PHP
- What is PHP
- Why PHP?
- What can PHP do?
- How PHP Works?
Let’s start from the first topic:
History of PHP:
In 1994, Rasmus Lerdorf developed PHP for personal use and then in 1995, he has released the first version of PHP with the name of “Personal Homepage”. In 1997,PHP was being used by more than 50,000 sites.
What is PHP?
PHP is an ‘OPEN SOURCE TECHNOLOGY’ which means the source is openly available.There is no requirement to pay for it.
Any technology(software) is open source,it is free of cost but if it is free of cost, it does not mean it is an open source technology.
PHP is a recursive acronym of “Hypertext PreProcessor” because it preprocesses the hypertext before displaying to the client.
PHP is a combination of C and C++. PHP is written in both C and C++.As we know C is a “Procedural language” which support procedure(function) whereas C++ is an “Object Oriented Language”.
Why PHP?
There is mainly 2 reasons to use PHP and here I am going to explain both the reasons:
Programmer’s Point of view: PHP has shorter learning curve means it doesn’t take much time to learn PHP.PHP is flexible,robust,compatible,stable,easy to use and cross-platform.
Business Point of view: If we see it from the business point of view,it is all about Money because PHP is open source.
PHP is mainly useful for web development.Web application support client-server architecture in which client sent the request to server and server serve the request sent by the client.The server is one machine which has operating system,Database server,web server,FTP server and Telnet server etc.
There is two type of programming languages
1.Compilation
An interpreted language generates the .exe file from the source file after compilation
2. Interpretation:
An interpreted language is one where the source file interpreted before generating output and it does not generate the .exe file.
PHP does not compile the file or generate .exe so we can say it is using Interpretation concept.The PHP interpreter (or parser) is the program that reads the PHP program file and executes the commands it understands
What can PHP do?
PHP is widely used general purpose scripting language that was originally designed for web development to produce dynamic web pages.
There are 3 main areas where PHP scripts are used:
1. Server Side Scripting
Server side scripting means a user can not view source code because the script was written at the server side.Every time a request is made to a Web server for a particular PHP file and the PHP interpreter must process the file and return the response to the web browser. PHP interpreter interprets a PHP program every time runs the program is known as a scripting language.
For Server Side Scripting we need:
PHP Parser
Web Browser
Web Server
2. Command line Scripting
With command line scripting,You can run PHP script without any server or browser.You only need PHP Parser to use this.
3. Desktop Application
PHP is probably not the very best language to create a Desktop application but you can develop with the use of PHP-GTK if you know very well and would like to use some advanced PHP features in your application. PHP-GTK is an extension used for graphical user interface.You can also write cross-platform application with this way.
In Short, PHP can convert the static website that content has to be changed by hand into a dynamic one that can display content based on any criteria.
How PHP Works?
Unlike HTML files, PHP files are routed through a special process that performs the PHP commands within the file before it is returned to the browser or client.
Let’s see the following image to understand the code:
SYNTAX:
PHP code must be enclosed within the PHP tags
Method 1: Short Open Tag
1 2 3 | <? … ?> // Not Recommended |
You can also use a short open tag as the PHP open tag if you enable the short_open_tags in php.ini file.However, this usage is not recommended. Method 2:
Each PHP sentence is terminated with semi-colon (;)
EXAMPLE:
1 2 3 | <?php echo 'Hello World'; ?> |
How to write COMMENTS In PHP?
Comments are a way to explain to other developers how you program code.When you write a code,It is good to use comment which will explain others or to you also that how your code works and why it is developed.
Now we will learn about how to write comments in PHP because You can write comments three different ways in PHP
Single line Comment :
1 2 3 4 5 | <?php //This is a comment ?> |
Single line Shell Comment
1 2 3 4 5 | <?php #This is a comment ?> |
Multi line comment : PHP provides a mechanism for detailed comments that is used for more than one line comment
1 2 3 4 5 6 7 8 9 | <?php /* This is a comment block */ ?> |
This is the example of PHP comments:
EXAMPLE:
1 2 3 4 5 6 | <?php //This is comment echo 'Hello World'; ?> |
Let’s see the example and understand how php syntax works:
Example:
1 2 3 4 5 | <?php echo '<p>Hello World</p>'; ?> |
In this example, you see that PHP code is embedded within PHP open tag . PHP then replaces that PHP code with its output.
Hello world! will be displayed in the Web browser. Interestingly, the single line statement enclosed within PHP tag.That’s it and It is your complete program.Isn’t it simple?
If you want to test configuration in PHP,insert the following code into a file and save the file as phpinfo.php
1 2 3 4 5 | <?php php_info() ?> |
Run this file into a browser and you can see the list of configuration in PHP.
Literals in PHP
All strings must be enclosed in single quote or double quote
There are two differences between this two methods:
1. variables in a double-quoted string will be replaced with desired values whereas the single-quoted strings will be interpreted exactly as is.
2. A double quoted string recognizes all available delimiters whereas a single-quoted string recognizes only the delimiters “\\” and “\”.
Example:
1 2 3 4 | <?php echo '<p>Hello World</p>'; ?> <?php echo "<p>Hello World</p>"; ?> |
Numbers are not enclosed in quotes
Example:
1 2 3 | <?php echo 123; ?> |
Booleans can be directly written as true or false
Language Construct in PHP
PHP have two language constructs which are used to display data. I have explained differene between echo and print here.
- echo()
- Print()
Language construct can be used with or without brackets
Example:
1 2 3 4 5 6 7 8 | <?php echo '<p>Hello World</p>'; print '<p>Hello World</p>'; echo ('<p>Hello World</p>'); print ('<p>Hello World</p>'); ?> |
Hope this tutorial will be useful to you. If you have any query,please write in comment section.