Basic Syntax

PHP is quite a simple language with roots in C and Perl, yet 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.


Semocolons are the one of the basic construct, every php statement is terminated by semicolons. as :

$x = 10;

Probably the most common cause of errors you will encounter with PHP is to forget this semicolon, which causes PHP to treat multiple statements like one statement, find itself unable to understand it, and 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 use $ symbol as :

$counter = 1;
$mystring = "Hello";
$myarray = array("One", "Two", "Three");

And really that’s pretty much all the syntax that you have to remember. Unlike languages such as Python, which are very strict about how you indent and lay out code, PHP leaves you completely free to use (or not use) all the indenting and spacing you like. In fact, sensible use of what is called whitespace is generally encouraged (alongwith comprehensive commenting) to help you understand your code when you come back to it. It also helps other programmers when they have to maintain your code.


You may also like...