A PHP script runs on the server, and then sends the resulting plain HTML back to the browser.
Basic PHP Syntax
You can embed a PHP script anywhere within your document.
The beginning of a PHP script is marked by <?php
and it's concluded with ?>
:
<?php
echo 'This is a PHP code.';
?>
The standard file extension for PHP files is ".php".
Typically, a PHP file consists of HTML tags alongside PHP scripting code.
Here's a simple PHP file example. It contains a PHP script utilizing the built-in function echo
to display the text "This is a PHP Page." on a web page:
<!DOCTYPE html>
<head>
<title>My PHP Page</title>
</head>
<body>
<?php
echo 'This is a PHP page.';
?>
</body>
</html>
PHP Case Sensitivity
In PHP, keywords (e.g., if, else, while, echo), classes, functions, and user-defined functions are not case-sensitive.
In the example below, all three echo statements are equivalent and valid.
<?php
ECHO 'This is a PHP page.<br>';
echo 'This is a PHP page.<br>';
EcHo 'This is a PHP page.<br>';
?>
However, all variable names are case-sensitive!
In the example below, only the first statement will display the value of the $age
variable! This is because $age
, $AGE
, and $agE
are treated as three different variables.
<?php
$age = 23;
echo 'My age is ' . $age . '<br>';
echo 'My age is ' . $AGE . '<br>';
echo 'My age is ' . $agE . '<br>';
?>
PHP Statements
PHP statements are used to calculate and output data, which is then presented to the user by a web browser. All PHP statements end with:
;
Examples
The following PHP statement saves the text "This is a PHP page." to a PHP Variable $hello
.
<?php
$textline = 'This is a PHP page';
?>
Every piece of text in PHP statements need to be wrapped in ' ' or " ".
A numerical value should not be wrapped in ' ' or " ", otherwise it will be treated as text.
The following PHP statement outputs the value of variable $textline
. Variables should be used without ' ' or " " in PHP statements.
<?php
echo $textline;
?>
Text and variables can also be combined in a single PHP statement like
<?php
echo 'The value of variable is ' . $textline . '. I saved it for testing.';
?>
In this example, variable and text statements are combined by the notation (.).
- Text: 'The value of variable is '
- Variable:
$textline
- Text: '. I saved it for testing.'
The output of the above PHP statement will be:
The value of variable is This is a PHP page. I saved it for testing.