Broad Network

PHP Basic Syntax

Basics of PHP with Security Considerations - Part 2

Foreword: In this part of the series, I give you the basic syntax of PHP.

By: Chrysanthus Date Published: 18 Jan 2018


This is part 2 of my series, Basics of PHP with Security Considerations. In this part of the series, I give you the basic syntax of PHP. You should have read the previous part of the series before reaching here, as this is a continuation.

A statement in PHP is a short piece of code that ends with a semicolon. An example is:

        echo "Hello World!";

This statement displays or prints "Hello World!" without the quotes, at the browser (web page).

You should have comments in your code. Comments are not executed. Comments are to remind you later of why you typed a particular piece of code. In PHP, you generally have a comment in a single line; something like:

        #This is a comment.

You begin each comment with the hash, # character. Your code is executed by the interpreter to perform a task, such as to print a piece of text. When the interpreter sees the # character, it ignores everything that is on its right. That is, it does not execute what is on the right of the # character. If you do not like #, you can comment a line with, // as in:

        //This is a comment.

Comment Example
In the previous part of the series, you produced the file named, temp.php. Go to the file and replace all its content with the following:


    #Talking about a man.
    echo "I am a man.<br>";
    #Talking about a boy.
    echo "He is a boy.<br>";


Save the file (temp.php) in the web server home directory (C:/Program Files/Apache Software Foundation/Apache2.2/htdocs).

Open your browser and call the script (program) with, http://localhost/temp.php (type in address bar and click Go).

The following should be printed:

I am a man.
He is a boy.

In the temp.php script, you have two comments, which are #Talking about a man. and #Talking about a boy.. Since these are comments in the PHP code, they do not appear at the web page (browser). The PHP interpreter in the computer does not send them to the browser.

Note: the purpose of "<br>" is to send the printing (display) to the next line, on the screen.

Paragraph as Comment
If you have a paragraph as comment, then instead of using # or //, delimit (mark) the comment as in:

    sentence1 sentence2 sentence3 sentence4 sentence5 sentence6 sentence7 sentence8 sentence9 sentence10 sentence11 sentence12

Note the start delimiter, /* and the end delimiter */ .

# or // works for single line, while /* paragraph */ works for a paragraph (which is not evaluated or executed or printed).

If you see any text in double or single quotes in PHP code, that text is called a string. In this and the previous part of the series, you have seen strings only in double quotes. You have not yet seen strings in single quotes. You will see that soon.

Let us end here for this part of the series. We continue in the next part.


Related Links

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



Become the Writer's Follower
Send the Writer a Message