Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Syntax

Basic PHP Syntax

A PHP script can be placed anywhere in a document and begins with <?php and ends with ?>.

<?php
// PHP code goes here
?>

The default file extension for PHP files is “.php”.

A typical PHP file includes HTML tags along with PHP scripting code.

Here is an example of a simple PHP file with a PHP script that uses the built-in “echo” function to display “Hello World!” on a web page:

Example

An example of a simple .php file containing both HTML and PHP code:

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

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

PHP Case Sensitivity

In PHP, keywords (such as if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.

In the example below, all three echo statements are equivalent and valid:

Example

ECHO is equivalent to echo.

<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Note: However, all variable names are case-sensitive!

In the example below, only the first statement will display the value of the $color variable! This is because $color, $COLOR, and $coLOR are considered three distinct variables.

Example

$COLOR is not the same as $color.

<!DOCTYPE html>
<html>
<body>
<?php
$color
= "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>