Hands-on with PHP — Introduction

< HomePHP Development Environment >

Getting to Know PHP

On the pretext of knowing me and knowing you, you have been lured into writing your first PHP script in the previous session. Now that you have gotten your feet wet, there is no looking back.On the pretext of knowing me and knowing you, you have been lured into writing your first PHP script in the previous session. Now that you have gotten your feet wet, there is no looking back.

In this lesson, you will get to find out what PHP is, why PHP is use, and how PHP works.

What is PHP?

Is PHP an acronym? You bet! But don’t bother. However, if you insist, PHP is a recursive acronym for PHP: Hypertext Preprocessor.

PHP is one of the most widely used open-source server-side scripting languages for web development. Many popular websites are powered by PHP. These include WordPress, Wikipedia, Facebook, among others.

Why use PHP?

The opposite of server-side scripting languages are client-side scripting languages like the client-side JavaScript. If there is already JavaScript, why do we need PHP? You may ask. The answer lies in the distinctly different roles and characteristics that client-side scripting languages and server-side scripting languages have in web development.
Using JavaScript and PHP as examples, the main differences between them are explained below with examples:

  • JavaScript code is executed by the web browser, whereas PHP code is executed on the server and only the results produced by the PHP scripts are returned and rendered as HTML markup in the browser.

  • JavaScript code is visible and accessible by anyone, whereas PHP code is kept away from prying eyes on the server. If you want to code your business rules, you will have to code it in PHP so that unauthorized persons can neither steal nor tamper with your code.

  • The reach of JavaScript code is confined within the context of the web browser. PHP code, on the other hand, can access and manage server-side resources such as databases which are integral parts of many modern web applications.

Apart from the distinct role and characteristics as server-side scripting language, some of the other reasons to use PHP for server-side scripting include:

  • PHP is a free language with no licensing fees so the cost of building a website using PHP is cheaper.

  • PHP can be used on all major operating systems, including as Linux, Unix, Microsoft Windows, Mac OS X, and many others.

  • PHP can run on cloud computing platforms, including Azure App Services and Amazon EC2.

  • PHP supports a variety of web servers, including Apache, IIS, lighttpd, and nginx.

  • PHP supports a wide range of databases, including MySQL, MSSQL, SQLite, MongoDB, PostgreSQL,  IBM DB2, and many others.

  • Last but not least, PHP has very good online documentation, and countless forums and tutorials, which make learning and using the language relatively easy as you can always find help online easily whenever you hit a snag.

How PHP Works?

Having learned the usefulness of PHP in web development, let’s find out how PHP works through an example which also serves to illustrates some of those differences between client-side scripting languages and server-side scripting languages mentioned earlier.

Inside the example.php code section lies a mixed content of HTML markup, JavaScript, and PHP code. In reality, they are contained in a file with a file extension .php, say example.php, and be placed in the web directory of a web server. You can request this file from the web server by typing the URL of this web server in a browser. To emulate this process, copy the code under the example.php code section to the Editor of the PhpFiddle widget, then hit the Run button:

<!DOCTYPE html>
<html>
<head>
<title>JavaScript vs PHP</title>
</head>
<body>
<h2>Current Date and Time</h2>
<!-- JavaScript code -->
<script>
 now = new Date();
 document.write("<h3>Your computer date time obtained by JavaScript: "+now.toDateString()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+"</h3>");
</script>
<!-- PHP code -->
<?php
 echo "<h3>Server date time obtained by PHP: ",date("D M d Y G:"),intval(date("i")),":",intval(date("s")),"</h3>";
?>
</body>
</html>

What has happened is:

  • Server side: when the web server receives a request for example.php (emulated by hitting the Run button of the PhpFiddle widget), a PHP interpreter, which is implemented as a module in the web server, picks up the PHP code inside the pair of <?php and ?> delimiters, parse it which is to obtain and print the current date time of the server; the web server then returns the result of the executed PHP code, which is the server date time, along with the remaining HTML markup and JavaScript verbatim to the requesting browser.

  • Client side: On receiving the response, the browser executes the JavaScript code which is to obtain the local computer date time and interprets the HTML markup to render a web page that displays the local computer date time and the server date time respectively in the browser as shown in the output panel of the PhpFiddle widget.

Wait a minute, where is the PHP code? No PHP code, only the result of its processing, i.e. the date and time of the server, was returned to the client side.

You might have expected the two sets of date and time obtained from JavaScript and PHP separately to be similar. Not likely! Unless the server that hosts the PHP file has been set to the time zone that the browser computer is currently in, you are going to get different times or even different dates for that matter.

Points of Interest

The key takeaways from this lesson are:

  • PHP is a server-side scripting languages.

  • PHP code is interpreted on the server, only the result of which may be returned to the client together with any HTML markup and JavaScript.

  • PHP is used to perform business logic computation.

  • PHP can access server resources such as databases, files, web APIs, etc.

  • PHP files can contain HTML, CSS, JavaScript, and PHP code.

  • PHP files have .php extension.

Quiz

Which one of the following languages can talk to the database?
PHP
Correct!
CSS
Wrong!
 
HTML
Wrong!
JavaScript
Wrong!
Which one of the following languages is executed on the server?
PHP
Correct!
JavaScript
Wrong!
Which one of the following languages is executed on the browser?
JavaScript
Correct!
PHP
Wrong!
 
Which one of the following languages should you use to program your business logic?
PHP
Correct!
JavaScript
Wrong!
What is the correct extension of a file if it contains HTML, CSS, JavaScript, and PHP code?
.php
Correct!
.js
Wrong!
 
.css
Wrong!
.html
Wrong!

< HomePHP Development Environment >

Tagged with:

Speak Your Mind