codescracker


php

PHP Basic Syntax



« Previous Tutorial Next Tutorial »


As you know, a PHP script is executed on the server, and then the plain HTML result is sent back to the browser.

PHP 5 Syntax

You can place your PHP script, anywhere in an HTML document. All PHP script must start with <?php and ends with ?>. In other words all PHP script must be placed inside <?php and ?>. Here is the general form of a PHP script:

<?php
// PHP code goes here
?>

Example

Let's look at the following PHP Example:

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
	echo "Hello PHP!";
?>

</body>
</html>

It will produce the following result:

PHP Hello World

Note - PHP statement ends with a semicolon (;)

Comments in PHP

Comments are not read or executed as part of the PHP script. Comments helps in reminding the code later. You can use comments to describe you PHP scripts. Comments also helps in debugging the PHP script later. Here are the three ways of comments in PHP script, which are:

Here is a small PHP script, containing all the above three ways of comments in PHP:

<!DOCTYPE html>
<html>
<body>
  
<?php

echo "<h1>Below are the comments</h1>";

// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-line comment block,
can span over multiple
lines as required
*/

// You can also use comments to leave out parts of a code line

echo "<h1>Below are the paragraphs</h1>";

echo "<p>This is paragraph 1</p>";
echo "<p>This is paragraph 2</p>";
?>
  
</body>
</html>

Since comments are not run/executed, so it will produce the following result:

PHP Comments Example

PHP Keywords Case Sensitivity

In PHP, all the keywords such as if, else, while, or echo, and classes, functions, or user-defined functions are not case-sensitive. Therefore, here in the below small PHP script, all the three echo statements are valid in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello Browser!<br>";
EcHo "Hello PHP!<br>";
?>

</body>
</html>

Here is the output produced by the above PHP script:

PHP Keywords Case Sensitivity

PHP Variables Case Sensitivity

However, in PHP, all the variable names are case-sensitive. Therefore, here in the below PHP example, only first statement will display the value of the variable named $color. This is because $color, $COLOR, and $coLOR are treated as three different variables.

<!DOCTYPE html>
<html>
<body>

<?php
$color = "blue";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>  

</body>
</html>

Here is the output of the above PHP script:

PHP Variables Case Sensitivity

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test