PHP Syntax (original) (raw)
Summary: in this tutorial, you’ll learn basic PHP syntax, including case sensitivity, statements, and whitespaces.
As a programming language, PHP has a set of rules that govern how you write programs.
PHP code #
Like HTML, you need to have the opening tag to start PHP code:
<?phpCode language: PHP (php)
If you mix PHP code with HTML, you need to have the enclosing tag:
?>Code language: PHP (php)
For example:
`
PHP Syntax `Code language: PHP (php)However, if a file contains only PHP code, the enclosing tag is optional:
<?php echo 'PHP Syntax';Code language: PHP (php)
Case sensitivity #
PHP is partially case-sensitive. Knowing what is case-sensitive and what is not is very important to avoid syntax errors.
If you have a function such as count, you can use it as COUNT. It would work properly.
The following are case-insensitive in PHP:
- PHP constructs such as if, if-else, if-elseif, switch, while, do-while, etc.
- Keywords such as
trueandfalse. - User-defined function & class names.
On the other hand, variables are case-sensitive. e.g., $message and $MESSAGE are different variables.
Statements #
A PHP script typically consists of one or more statements. A statement is a code that does something, such as assigning a value to a variable or calling a function.
A statement always ends with a semicolon (;). The following shows a statement that assigns a literal string to the $message variable:
$message = "Hello";Code language: PHP (php)
The above example is a simple statement. PHP also has a compound statement comprising one or more simple statements. A compound statement uses curly braces to mark a block of code. For example:
if( $is_new_user ) { send_welcome_email(); }Code language: PHP (php)
You don’t need to place the semicolon after the curly brace (}).
The closing tag of a PHP block (?>) automatically implies a semicolon (;). Therefore, you don’t need to place a semicolon in the last statement in a PHP block. For example:
<?php echo $name ?>Code language: PHP (php)
In this example, the statement echo $name doesn’t need a semicolon. However, using a semicolon for the last statement in a block should work fine. For example:
<?php echo $name; ?>Code language: PHP (php)
Note that it’s OK if the code does not make sense to you now because you’ll learn more about it in the upcoming tutorial.
Whitespace & line breaks #
In most cases, whitespace and line breaks don’t have special meaning in PHP. Therefore, you can place a statement in one line or span it across multiple lines.
For example, the following code snippets are equivalent:
login( <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>s</mi><mi>e</mi><mi>r</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">username, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mpunct">,</span></span></span></span>password );Code language: PHP (php)
And:
login( $username, $password );Code language: PHP (php)
Summary #
- PHP is partially case-sensitive.
- PHP constructs, function names, and class names are case-insensitive, whereas variables are case-sensitive.
- A statement ends with a semicolon (;).
- Whitespace and line breaks don’t matter in PHP; leverage them to make the code more readable.
Did you find this tutorial useful?