PHP Basic Tutorial
MySQL Connection
PHP (Hypertext Preprocessor) is a popular server-side scripting language primarily used for web development. When writing PHP code, it’s essential to understand the basic syntax that dictates how PHP scripts are structured.
A PHP script can be placed anywhere in the document.
The opening tag <?php
tells the server to interpret the enclosed code as PHP.
The closing tag ?>
ends the PHP code. This closing tag can be omitted if the file is pure PHP code to avoid unwanted output, like whitespace.
These tags are also called ‘Canonical PHP tags’. Everything outside of a pair of opening and closing tags is ignored by the PHP parser.
Note
Every PHP command ends with a semi-colon (;).
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:
Try it yourself
PHP keywords (e.g., if
, else
, while
), functions, and classes are not case-sensitive, but variable names are case-sensitive.
Try it yourself
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:
Try it yourself