Broad Network


Introduction to PHP

Basics of PHP with Security Considerations - Part 1

Foreword: In this part of the series, I explain what you need to start programming in PHP.

By: Chrysanthus Date Published: 18 Jan 2018

Introduction

PHP is a scripting (programming) language, which is used at the server of the web. PHP can be used in a stand-alone program or it can be embedded (typed) into an HTML document. PHP stands for Hypertext Preprocessor. The ‘P’ in front of HP was (just) added. PHP is an interpreter language. This is the first part of my series, Basics of PHP. In this part of the series, I explain what you need to start programming in PHP.

The knowledge in this volume is applicable to PHP version 7 and higher (most of it is also applicable to previous versions).

You should read the different parts of the series in the order given.

Security Considerations
You must have heard that important politicians emails have been hacked. Today there is ransom-ware. So when programming today, you have to take precautions that limit cheating (leaks) on the user of the program (application). In this volume (a number of tutorial series), you are also taught how to prevent insecurities.

Pre-Knowledge
In order to study PHP from this volume (these tutorial series), you need to know how to use the Internet. Having basic knowledge in another language called HTML is an advantage, but not required. Having succeeded in Middle School (British O'Level) mathematics is also an advantage but not required. The little mathematics you need to complete the course (volume) is taught to you along with the course, in a simple way (have confidence).

A programming language needs a lot of logical reasoning: that will be taught to you in a simple way, as we go along. You can learn PHP as your first computer language.

Requirements
Here, I give you the requirements to study PHP in this series (volume).
- PHP Binary (application that actually executes PHP program)
- Apache Web Server (application that sends PHP result to the browser)
- Browser
- Text Editor
- Your Personal Computer (with its operating system)

You can download PHP 7 binary and Apache HTTP Server 2.4, both free from the Internet. Just search the Internet and download. They work with many operating systems. Look for the ones for your operating system.

In this series there are many code samples that you will be trying (re-typing or copy and paste and then executing).

Installation
They are both easy to install in your personal computer. Just follow the instructions. When you install the Apache web server (software), make it to work with PHP. When you install PHP, make it to work with the server (if necessary). To make the web server work with PHP, in the configuration file of the server, at the bottom of the page, you have to type something like:

    LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
    AddHandler application/x-httpd-php .php

For the server, you put your PHP files (code samples) in the htdocs directory. In your computer, the path is something like:

    C:/Program Files/Apache Software Foundation/Apache2.2/htdocs

I use the Windows Operating System. You can use a different operating system, if you know how it works.

PHP Script
A PHP script goes into the tag,

                    <?php - - script - - ?>

So, if you have a stand-alone script (program) then the beginning of the file would be <?php and the end of the file would be ?>.

If your script is embedded in an HTML document, then the beginning of the script would be <?php and the end of the script would be ?>. In the case of the HTML document, you effectively have a single HTML tag element, which is <?php - - script - - ?>. You can have many PHP scripts in an HTML document; that is, many <?php - - script - - ?> tags. This tag is better written as

    <?php
    - - script - -
    ?>

The web page with the PHP script (or scripts) must be in the server.  If the program (script) is alone (not in an HTML document) as most scripts in this volume are, the script still has to be in the server. The script is executed by what is called the PHP parser, at the server.

The PHP script in the HTML document at the server, is not seen when the browser is displaying the HTML document. However, after execution, the PHP script can produce HTML elements or text at the server, to fit in the position of the PHP script, replacing the PHP tag. When the HTML document is downloaded by the browser, the HTML elements or text produced at the server would be seen at the browser, in the position where the PHP script (PHP tag) was.

When one or more PHP scripts are embedded in an HTML document at the server, the file name extension of that HTML document has to become .php instead of .htm or .html. If the PHP script is stand-alone (not embedded in HTML document), the extension of the file name still has to be .php .

Your First PHP Script
Copy and paste (or re-type) the following code in your text editor:

<?php

    echo 'Hello World';

?>

Save the file as temp.php in your server’s htdocs directory or root (home) directory. Open your browser. Type the URL, http://localhost/temp.php in your browser (address bar) and click Go. Your browser should display “Hello World” as the only content of the web page. If it were in the Internet (web), you would replace “localhost” with the site address and path to the PHP file, temp.php.

If you tried this example and you did not see “Hello World” at the browser, or it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured properly or you have not install PHP connecting it to the server properly.

Explanation of Code
There is just one line in the tag, which is:

    echo '<p>Hello World</p>';

The word echo means output or type what is in quotes. The quotes may be single or double. What we have in the quotes is the string, “Hello World”. So, the word “echo” replaces the PHP single tag with the string, in the supposed HTML document. It is not all PHP scripts that output text to the browser. Some scripts (or part of a script) just do some execution without outputting anything.

Conclusion
In this part of the series, you have learned how to produce a PHP file and run (execute) it: Use your text editor to type the file. Place the file in the home directory of the web server. Call the file using the URL at the browser.

We stop here and continue in the next part of the series.

Chrys

Related Links

Basics of PHP with Security Considerations
More Related Links
Pure PHP Mailsend - sendmail
PurePHP MySQL API
Using the PurePHP MySQL API
Cousins
JavaScript/ECMAScript Course
Perl Course

NEXT

Comments

Become the Writer's Follower
Send the Writer a Message