Powered by Blogger.

Introduction to PHP -Basic Syntax

Basic Syntax
PHP is quite a simple language with roots in C and Perl, yet it looks more like Java. It
is also very flexible, but there are a few rules that you need to learn about its syntax and
structure.
Semicolons
You may have noticed in the previous examples that the PHP commands ended with a
semicolon, like this:
$x += 10;
Probably the most common cause of errors you will encounter with PHP is forgetting
this semicolon. This causes PHP to treat multiple statements like one statement, which
it is unable to understand, prompting it to produce a Parse error message.
The $ symbol
The $ symbol has come to be used in many different ways by different programming
languages. For example, if you have ever written in the BASIC language, you will have
used the $ to terminate variable names to denote them as strings.
In PHP, however, you must place a $ in front of all variables. This is required to make
the PHP parser faster, as it instantly knows whenever it comes across a variable. Whether
your variables are numbers, strings, or arrays, they should all look something like those
in Example 3-3.
Example 3-3. Three different types of variable assignment
<?php
$mycounter = 1;
$mystring = "Hello";
$myarray = array("One", "Two", "Three");
?>