PHP Syntax

This means a PHP statement is terminated by semicolon; uses curly-bracket to indicate the start and end of a control structure statement and keywords (such as. if, else, for, while, print, echo, etc.) and functions name are not case-sensitive, we will see examples soon. But first, PHP statement or code must reside with the within the PHP; start <?php and end ?> directive.

// PHP code appears here
The default file extension for PHP files is .php and PHP file often contains HTML tags and some PHP code.

<TITLE> Testing PHP </TITLE>
print "Today's date is: ". date('d-m-Y');

PHP Variable

PHP are containers or name for holding information. in PHP a variable name must start with a dollar sign $, for example $firstName.

Variable Naming Rule
Variable name must start with a letter or the underscore but NOT a number
Variable name can only contain alpha-numeric characters and underscores (A to z, 0 to 9, and _ )
Variable names are case-sensitive in PHP, for example $msg is not the same as $MSG

PHP is loosely typed language, that is, it determine the data type of a variable at run time when you request the PHP page or function, this is a feature that languages like C, C++, Java, C# and  others, strongly discourage, as they insist you must declare every variable before usage, hence they are called strongly typed language. This PHP feature has it advantage and disadvantage, but just as I said earlier Rasmus Lerdorf (creator of PHP) must have taught that most web developer or programmers are also busy with other things therefore saving them from this hustle of declaring all variable with type before usage.

Variable Example

<title> PHP Variables </TITLE>
//Variables Declaration
$name = "Ben Onuorah";
$age = 35;
$height = 5.6;

//Variables Output
print "$name is $age years old, and he is $height";


More Variable Example

$x = 100;
$y = 250;
echo "The addition is ".$x + $y;

Note: the period (.) in between the echo (output statement) is called concatenation, it is use to join “plain text” to a $variable when doing output.

Although the variable described above is what most people refer to as variable but PHP also some special types of variables for different types of operation, see the list below

$_POST: holds variables provided through the form POST method
$_GET: holds variables provided through the GET method
$_REQUEST: is a PHP super global variable which is used to collect data with POST or GET method
$_SESSION: holds any variables that are currently registered in a session
$_SERVER: holds server related information such as headers and file paths
$_FILES: holds any variables provided through file uploads.
$_ENV: handles server environment related variables.

PHP Comment

Comment is a line that is not executed as a part of the program. Its purpose is to let others understand our code better or to remind the programmer what he/she did as programming larger software can easily get complicated. Single line comment is initiated by //, while multiline comment start with /* and end with */

Single line comment examples

//declare two variables
$x = 100;
$y = 250;
//add both variables and store them into another variable
$sum = $x+$y;
//display the output or result
echo "The sum of ".$x." and ".$y." addition is ".$sum;

Note: comments don't end with the semi-colon terminator because they are non executable lines of code

Multiline comment example

/* this program process Ben
personal record... */
$x = 100;
$y = 250;
echo "The addition is ".$x + $y;

Next »

Home    |    Learn Programming   |    Learning Management System   |    Computer Based Testing System    |    Contact Us