A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php
and ends with ?>
:
<?php// PHP code goes here?>
The default file extension for PHP files is ".php
".
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo
" to output the text"Hello World!" on a web page:
Example
A simple .php
file with both HTML code and PHP code:
<!DOCTYPE html><html><body><h1>My first PHP page</h1><?phpecho "Hello World!";?></body></html>
Note: PHP statements end with a semicolon (;
).
PHP Case Sensitivity
In PHP, keywords (e.g. if
, else
, while
, echo
, etc.), classes, functions, and user-defined functions are not case-sensitive.
In the example below, all three echo statements below are equal and legal:
Example
ECHO
is the same as echo
:
<!DOCTYPE html><html><body><?phpECHO "Hello World!<br>";echo "Hello World!<br>";EcHo "Hello World!<br>";?></body></html>
Note: However; all variable names are case-sensitive!
Look at the example below; only the first statement will display the value of the $color
variable! This is because $color
, $COLOR
, and $coLOR
are treated as three different variables:
Example
$COLOR
is not 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>
PHP Exercises
W3schools Pathfinder
Track your progress - it's free!
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.