Introduction To PHP
PHP is a recursive algorithm that stands for Hypertext Preprocessor developed by Rasmus Lerdorf and it is widely used server-side scripting language which is, of course, open source and easy to learn. The objective of this is to facilitate web pages to generate content in the web pages dynamically and to have a logical exchange of data and web pages between the client and the server. The scope of this is far and beyond. PHP embeds into HTML easily.
What is PHP?
Whenever you type in a URL or request by clicking on any link (which again is nothing but indirectly supplying an URL,) the request is forwarded by your web browser (Firefox, Chrome, etc.) over the internet to a ‘server’. Now this server is where all the pages you code for are actually stored. Also, all the user accounts will obviously be present on the server. Now think about this: applications like Facebook, emails etc. will have lots of user accounts, right? So managing such data is a little tricky. Hence computer programmers have come up with something called “databases”. These help in efficiently handling (usually large amounts of) data, especially when it’s something that can be tabulated.
So, when the request hits the server, the server processes the requests and responds with the data (as a plane response or embedded inside a webpage). For these purposes, it is used. Basically, it is a server-side scripting language. Currently, Version PHP7 is the latest version.
Following is a simple example of this Script inside an HTML page.
See how the PHP code is embedded in the HTML code to print sentence Hi, Welcome to PHP in the body. The web server can be configured to process all the HTML files with PHP.
What can PHP do?
With this, we can
- Generate dynamic web pages.
- Collect form data from the web page.
- Send or receive cookies.
- And, anything which any other CGI (Common Gateway Interface) programming language can do.
There are three main fields where these scripts are used:
- Server Side Scripting – This is the traditional purpose of why it is built & used, for generating dynamic content. For this, you need a PHP Parser, a web server, and a web browser.
- Command line Scripting – This is where PHP acts as just any other scripting language wherein you can run your PHP scripts using a PHP Parser.
- Client-side GUI – This is where you can build application software (with GUIs) using PHP-GTK. It is not the best-fit language for the purpose though.
What PHP Supports?
- This can be run on almost all major Operating Systems.
- It supports most web servers like Apache, Microsoft IIS, etc.
- It also supports a wide range of Databases like MySQL, Front-base, Inter-base, Oracle, PostgreSQL, ODBC (Open Database Connection Standard), etc.
- It does not only generate HTML pages but also can output images, PDF files and also Flash movies.
- It also supports talking to other services using protocols like HTTP (Hypertext Transfer Protocol), LDAP (Lightweight Direct Access Protocol), IMAP (Internet Message Access Protocol), COM, etc.
- It supports procedural programming and Objects Oriented Programming methodologies.
Working with PHP
Let’s now understand PHP by learning a few syntax.
1. How to embed a PHP code in HTML file?
Embed the code enclosed within <?php…?> and whatever is inside this will be treated to a PHP code by the interpreter. The code would look like this.
2. Working with Datatypes and variables in PHP
It supports eight primitive data types.
Four scalar types namely,
Two compound or derived datatypes,
Two special types, namely,
This is how one can dynamically create a variable and use it in this.
3. Working with Arrays
An array in PHP is an ordered map that maps keys to corresponding values. It is transformed in so many ways that one can use it as a real array, list (a heterogeneous array or as a vector), hash tables or dictionaries, collection, etc. Of course, in PHP also like any other programming languages, the arrays are zero indexed.
This is how one can create and access arrays in this.
4. Predefined variables
It comes with several predefined variables which are as follows –
- $_GLOBALS – This has reference to all the variables that are currently available in the global scope of the script.
- $_SERVER – This contains the variables that are related to the execution environment and variables that are set by a web browser.
- $_GET – This contains all the variables sent by the GET HTTP request.
- $_POST – This contains all the variables sent by the POST HTTP request.
- $_COOKIE – This has variables given by the HTTP cookies.
- $_FILES – This has variables given to the script by HTTP file uploads.
- $_ENV – This has variables given by the environment to the script.
- $_REQUEST – This has variables given to the script employing any user input request method.
- $_SESSION – This stores the variables that are registered in the current session of the script.
5. Decision Making
Following snippet shows the way to deal with if-else statements.
It has both for loop and do-while loop whose syntax goes like this.
This is ‘for’ loop.
This is ‘for each’ loop.
This is a do-while loop.
7. Switch Statements
We also have switch statements which help us do decision making based on fixed choices of values. Below code shows how to use it.
In order to modularize the program and also to have the code re-usability, we can write functions in PHP. Every function will have a definition with a set of parameters defined along with a return value. Following code snippet shows how one can write a function in this.
It supports dealing with Cookies. Cookies are the mechanism for storing data inside the web browsers for quicker responses and tracking of returning user. One can use the set-cookie() function to set cookies in PHP. Below code shows it.
10. Working with GET method
A GET method is the one in which the request variables are embedded in the request URL and forwarded to a server. We use the $_GET global variable to receive and access the GET request variables. The data from the web page is submitted to the server script via a form in the web page. So, the form action must have the link to the server script that will be handling the form data. The following snippet will show how to access the GET variables in PHP.
11. Working with a POST request
In a POST HTTP request, the data is wrapped inside the body of the request and sent to the server. To access the variables sent over a POST request, we use the global variable $_POST. The following code snippet will show how to handle the POST request.
There is still more in PHP than these which you can explore with these strong basics.
Some extra skills you need to work with PHP
- It is an add-on if you understand the HTTP Protocols, Database Management for better interaction.
- Knowledge about servers like Apache and tools like LAMP or XAMP.
Top companies that use PHP
Though there are tens of languages for server-side scripting, PHP has remained one of the widely used languages for the purpose. Top companies like Facebook, Yahoo, Google, Wikipedia, WordPress, etc. employ PHP for their server-side programming.
Advantages of PHP
Following are the advantages are as follow:
- It is easy.
- It is lightweight in nature.
- It comes with a lot of libraries and packages.
- Even though it is open source and free of cost, there has been no compromise in terms of quality.
- As it runs behind an Nginx or Apache kind of servers, it is easy to scale.
- There are plenty of good PHP developers out there. Hence, it is easy to hire a resource to get things done.
- There is good community support for PHP.
Future of PHP
Well, ask after 3 years and the answer will still be ‘YES’.
- Most websites are powered by this and they probably will not think of changing the underlying server scripts.
- Laravel has become the best PHP Framework that is adopted widely and on high demand in the market.
- Version 7 comes with a higher level of security and faster speed that makes it one of the prioritized options for organizations and developers.
So yes, the relevance of this is going to remain intact.
Career growth in this technology
If you are looking for a career in web development then yes, expertise in PHP is going to elevate your success.
PHP has come a long way; growing to be the most prominent language on the web, of the web and for the web.
This has been a guide to What is PHP. Here we discussed the working and advantages of PHP and top companies that implement this technology. You can also go through our other suggested articles to learn more –