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: 30 Aug 2018

Introduction

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.

Statement
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).

Comments
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:

<?php

    #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).

String
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.

Chrys

Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments