Here are the rules that you have to follow to avoid any errors:

Always end any command with “;”

In PHP space and return character don’t have a significant meaning instead for ending each line of code you must use semicolon “;” to tell the processor that this line is finished.

It is a good feature because it allows you to format the code the way that you want it to look for example you can have something like this:

$a=1;    $b=2; $c=”

some text

“;

echo $a+$b.$c;

the above code will execute without any problems as each statement has finished with a “;” and the second and third  line will be continuous part of the first line [$c=”] and it will only end at [“;] . So in the above example I could add 10 or more line of text and as long as it does not have ” I wouldn’t get any errors(look at the variable for explanation of this error) .

Start and end with <?php ?> or for shorthand <? ?>

PHP code is always start with “” and it’s called a block of code that you can put anywhere inside the “.php” file. If your server has shorthand coding enabled you can also use “” please look at good practices for more detail.

so here is an example of php and html code combined:

<html>

<header><title><?php echo “This is a Hello World Page”; ?></title></header>

<body>

<?php

echo “Hello World”;

?>

</body>

The result of the above script will be a web page that says “This is a Hello World Page” on the Title and “Hello World” will be in the body of this HTML file(the file that php created to be shown to the client).

use extension “.php”, “.php3”

PHP uses extension “.php” and “.php3” and if you make a file and you call it anything.html and you put some php code inside it will be ignored and will not run however if you are including this file(we will get there soon) you can put your code inside a file with ANY extension but ONLY for the file that you are including so:

If you have a file called “a.php” and a file called “b.tmp” and both have PHP code inside if you call “a.php” the script will be run and if you include “a.tmp” inside “a.php” the script inside both will be run but the opposite does not apply.

Commenting // and /**/

Any elegant and professional piece of code will have some comments inside weather it is to help others understand what you have done or to mark the places that you need to make changes to yourself it is always a good practice to comment your code. PHP have two main ways that you can comment a piece of code and here is how:

for one line comments you can use

// and then the comment

for multiple line comments all you have to do is put them inside “/* ” and ” */” like:

/*

This is one comment for example

this can be a description of the code at the top or

Pseudo code of the script anywhere in the PHP block

*/

output is echo and print or you can just perform a task

A PHP code will have different uses as you will see.

It can create an HTML file to be served to the client’s browser. PHP does this using different functions but the main ones that you need to learn are “echo” and “print” and also it allows you to put HTML code outside the PHP block that will allow you to create the HTML and edit it inside WYSIWYG editors much easier.

Also It can perform a specific task like backing up files and databases.

%d bloggers like this: