PHP scripts are executed on the server, and the result is sent back to the browser as plain HTML.
Basic PHP Syntax
PHP scripts can be embedded anywhere within an HTML document. They start with <?php
and end with ?>
.
<?php
// Your PHP code goes here
?>
By default, PHP files have the extension .php
. Typically, these files contain a mixture of HTML tags and PHP code.
Example
Here’s a simple PHP file containing both HTML and PHP code:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to My PHP Page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Note: PHP statements must end with a semicolon
;
.
PHP Case Sensitivity
While PHP keywords (e.g., if
, else
, while
, echo
, etc.), classes, and functions are not case-sensitive, variables are case-sensitive.
In the example below, all three echo
statements are valid and equivalent:
Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
However, variable names are case-sensitive. Consider the following example where only the first statement displays the value of the $color
variable correctly:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "The car is " . $color . "<br>";
echo "The motorcycle is " . $COLOR . "<br>";
echo "The bicycle is " . $coLOR . "<br>";
?>
</body>
</html>
In this instance, $COLOR
and $coLOR
are considered different variables from $color
.