PHP Lesson 3 – How does PHP work? First PHP program.

Last updated on December 8th, 2023

Why do I need to know HTML before writing PHP code?

Before starting to learn PHP, as already mentioned, it is highly recommended that you have at least a basic knowledge of HTML/CSS. You need HTML because PHP code can be written directly in the HTML code, i.e. between HTML tags (HTML elements). Also, HTML tags can be included in PHP code. This makes the PHP language an extremely flexible tool for creating web pages.

You will need HTML/CSS to create the user interface, i.e. what the user will see on the page (this part of the software is called the front-end). With PHP, you will create that part of the software that is invisible to the user (it is called the back-end).

For example, let’s imagine that you need to create a table with several numbers. The numbers need to be sum up. The sum of the numbers should be displayed below the table in red color.

The size and color of the table, the red color of the final sum of numbers, the background of the page – all this is the final result that is visible to the user (front-end). This end result is designed using HTML and CSS.
Mathematical operations with numbers (calculations) are performed by PHP code, which remains hidden from the eyes of users (back-end).

What do I need to run PHP code?

To write an HTML document you need nothing more than a simple text editor and a browser. After you write the code and save it as an HTML file (for example test.html), the browser will immediately display the HTML page. By clicking on View Page Source you will immediately be able to see the HTML code of the page (for Mozilla Firefox browser – right-click on any open page, then click on View Page Source).

In order to execute a PHP script, however, in addition to a browser, 2 more components are needed: a server and special software – a PHP interpreter. This is because, unlike HTML, which runs in the browser, PHP runs on the server. So if you only have a browser you won’t be able to run any PHP script.

What is the working scheme of the PHP language?

The working scheme of the PHP language is as follows: a visitor opens some PHP page on the Internet and the browser sends a request to the server. The PHP script is executed on the server and returns the result to the browser. As a final result, the visitor sees on his monitor what is displayed on the PHP page. What the visitor sees, however, is a simple HTML document. If the visitor opens the source of the page, he will not see a single PHP element there, nothing but HTML code. The PHP code that generated the page in the browser remains completely hidden from the visitor’s eyes. Here is a simple example to illustrate this.

What are the basic elements of the PHP language?

As you know, HTML documents have a start tag <html> and an end tag </html>, and all the content of the HTML page is between these two tags. In HTML, comments can be written that do not affect the code and are enclosed between <!– and –> tags.

In a similar way, PHP scripts have a start tag (opening tag) and an end tag (closing tag), between which the content of the script is placed. PHP also has a tag for comments that do not affect script execution.

The start tag in PHP is <?php
The end tag in PHP is ?>
A single line comment tag in PHP is two right slashes: //
A multi-line comment is enclosed between the /* and */ tags.

One of the most important elements of PHP is the echo command. This command displays text and other elements on the monitor screen. The text to be displayed must be enclosed in quotation marks (“ ”) and the line must end with a semicolon (;). Inside the PHP code, as mentioned, HTML tags can be inserted. So, after you use the echo command to display text on a screen, you can also set effects on that text via HTML tags that are written directly into the content enclosed in quotes, like this:

echo “<b>Bold text to be displayed on the screen.</b>”;

What is the first and simplest PHP program?

By old tradition, the first program in any language displays the message “Hello, World!”. So, let’s display a bold “Hello World!” text on the screen. Open some simple text editor, for example Notepad or Notepad++. Notepad is part of the Windows operating system. So if you have Windows on your computer, you also have Notepad. Find your Notepad (search your computer for the keyword notepad), open it and write the following code in it:

<html>
<head>
<title>Test</title>
</head>
<body>

<?php
// My first PHP script
echo "<b>Hello, World!</b>";
?>

</body>
</html>

Now save the file on your computer (File -> Save us from the top menu) with a .php extension, for example test.php, and then upload the page somewhere on the Internet to a hosting that supports PHP (almost all hostings support PHP). When you load the page in the browser, you will see on the screen a bold text “Hello, world!”.

Don’t worry if you don’t have hosting to upload your file – in the tutorial What is XAMPP and how to use it? you will learn how to install PHP software on your personal computer. You will then be able to run all PHP files on your machine.

What’s in the source of a PHP page?

When you load the test.php page, right-click on it and select “View Page Source” from the pop-up panel (if you use Mozilla Firefox browser). In the source of the page, you will see nothing but the following HTML code:

<html>
<head>
<title>Test</title>
</head>
<body>

<b>Hello, World!</b>

</body>
</html>

As explained, the PHP code that runs on the server remains hidden and is not displayed. Only the HTML code is visible in the source of the PHP page.

You don’t necessarily have to write HTML code in which to put PHP code. You can only write this

<?php
// My first PHP script
echo "Hello, World!";
?>

and save the page as test2.php – when you upload it to the hosting and load it in the browser, the words “Hello, World!” will appear on the screen again. In this case, only the phrase “Hello, world!” will be visible in the source of the page.

How to put HTML/CSS commands inside PHP code?

For example, to make a title in red color you need to write the following HTML/CSS code:

<h1 style="color:red;">This Title Is Red</h1>

In this case, the property (color) and the value (red) of the style attribute are enclosed in quotes.

In order for this code to work when you put it in a PHP script, it must be written like this:

<?php

echo "<h1 style=\"color:red;\">This Title Is Red</h1>";

?>

I.e. quotes in the style attribute construction must be preceded by a left slash (\”).