Basic Syntax of PHP

The PHP code is enclosed in special start and end processing instructions <?php and ?> that allow you to jump into and out of "PHP mode."

PHP code is executed on the server, generating HTML which is then sent to the client. The client would receive the results of running that script, but would not know what the underlying code was.

Example

<!DOCTYPE html>
<html>
	<body>
		<h1>My first PHP page</h1>
		<?php
			echo "Hello World!";
		?>
	</body>
</html>

Note: PHP statements end with a semicolon (;).


Comments in PHP

A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code.

Use # to write single-line comments

<?
	# this is a comment in PHP, a single line comment
?>

Use // to also write single-line comments

<?
	// this is also a comment in PHP, a single line comment
?>

Use /* .……*/ to write multi-line comments

<?
	/* this is a multi line comment
		Name: Algorithm Room
		Type: Website
		Author: Rajiv Bikram Shah
	*/
?>

Case Sensitivity

The names of user-defined classes and functions, as well as built-in constructs and keywords such as echo, while, class, etc., are case-insensitive. Thus, these three lines are equivalent:

echo("hello, world");
ECHO("hello, world");
EcHo("hello, world");

Variables, on the other hand, are case-sensitive. That is, $name, $NAME, and $NaME are three different variables.


Statements and Semicolons

A statement is a collection of PHP code that does something. It can be as simple as a variable assignment or as complicated as a loop with multiple exit points. Here is a small sample of PHP statements, including function calls, assignment, and an if statement:

echo "Hello, world";
myFunction(42, "O'Reilly");
$a = 1;
$name = "Elphaba";
$b = $a / 25.0;
if ($a == $b) {
	echo "Rhyme? And Reason?";
}

PHP uses semicolons to separate simple statements. A compound statement that uses curly braces to mark a block of code, such as a conditional test or loop, does not need a semicolon after a closing brace. Unlike in other languages, in PHP the semicolon before the closing brace is not optional:

if ($needed) {
	echo "We must have it!"; // semicolon required here
} 	// no semicolon required here after the brace

The semicolon, however, is optional before a closing PHP tag:

<?php
	if ($a == $b) {
		echo "Rhyme? And Reason?";
	}
	echo "Hello, world" // no semicolon required before closing tag
?>

It’s good programming practice to include optional semicolons, as they make it easier to add code later.


Whitespace

Spacing characters such as newlines (carriagereturns), spaces, and tabs are known as whitespace. As you probably already know, browsers ignore whitespace in HTML. So does the PHP engine. Consider these two HTML fragments:

<h1>Welcome to Bob’s Auto Parts!</h1><p>What would you like to order today?</p>
and
<h1>Welcome	to Bob’s Auto Parts!</h1>
<p>What would you like to order today?</p>

These two snippets of HTML code produce identical output because they appear the same to the browser. However, you can and are encouraged to use whitespace sensibly in your HTML as an aid to humans to enhance the readability of your HTML code.

The same is true for PHP. You don’t need to have any whitespace between PHP statements, but it makes the code much easier to read if you put each statement on a separate line. For example,

echo "hello "; echo "world";
and
echo "hello ";echo "world";

are equivalent, but the first version is easier to read.