In previous tutorials on working environments, we have shown that PHP is a dynamic scripting language interpreted and precompiled server side. It is now for us to achieve our first programs and run them on the web server (local or remote).
In computer programming, there is a “tradition” which is to generate the string (note the term the way) Hello World! standard output (in our case it is a computer screen). Let’s start with the first script shown below.
We’ll be covering the following topics in this tutorial:
First PHP script
Enter the following code in a text editor (Note pad, Wordpad or Notepad ++ are largely the case) then save the file with the name helloworld.php.
Note: all files containing PHP code are required to be registered with the .php extension (or .phpX where X is the PHP version number).
First PHP program: “Hello World”
<?php echo 'Hello World!'; ?>
Run this first script in a web browser (Safari, Firefox, Opera, Internet Explorer …). You see that the text Hello World! appears well on screen. So we get the desired result out. Time to explanations.
Code Explanation
First of the beacons. Any PHP script must be surrounded by two tags to delimit the other type of content contained in a single file (eg HTML). Here we use the <? Php and?>. There are others but they are strongly advised not to use. If you want to know what are they and why we should not use them, please see the following tutorial: Why it is not advisable to use short tags (short-tags)?.
Regardless, you should always use the markers of this first program. This is THE first good practice to adopt when you code in PHP.
The second part of the code is what is called a programming instruction. The echo () function (or rather the language structure because it is a particular function of PHP) is responsible for writing what is happening as a parameter to standard output. Here the setting is a string (type) whose value is “Hello World! “.
Important concept to remember: the PHP script is executed on the server and the result of this execution being returned (here the html code) is interpreted by the Web browser.
Improved Hello World
So far nothing difficult. Let us move to the next level. We will generate our Hello World! in the middle of an HTML document. Here the file code helloworld1.php:
Generating a minimal HTML document
<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en">
<head>
<title> First PHP program! </title>
</head>
<body>
<?php
echo 'Hello World!';
?>
</body>
</html>
First PHP program!
After running this file, we see that the result on screen is exactly the same as before. Yes, but only to the naked eye! Here we generated our string inside HTML. The principle of dynamic page begins to be felt from there. Indeed, PHP will allow us to generate pages from templates and parameters provided to it.
Suppose we want to display our Hello World! in bold. Two choices are available to us:
We place the <strong> and </strong> on either side of the script.
We place the <strong> and </strong> directly in the echo () statement.
Whatever the result is the same product. But the second example (see code below) shows while it is possible to generate HTML code to build a page. The advantage of PHP becomes evident and we can imagine all the possibilities available to us later. For example: generate HTML tables, lists, links, paragraphs, XML documents …
A “Hello World” in bold
<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en">
<head>
<title> First PHP program! </title>
</head>
<body>
<?php
echo '<strong> Hello World </strong>';
?>
</body>
</html>
The code below will effectively write on the screen: Hello World!