0% found this document useful (0 votes)
3 views3 pages

3-PHP Code Syntax

The document explains the syntax of PHP, including the necessity of opening and optional closing tags, case sensitivity rules, and the structure of statements. It highlights that PHP is partially case-sensitive, with constructs and function names being case-insensitive while variables are not. Additionally, it notes that statements end with a semicolon and that whitespace and line breaks do not affect the code's functionality.

Uploaded by

harleensingh985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

3-PHP Code Syntax

The document explains the syntax of PHP, including the necessity of opening and optional closing tags, case sensitivity rules, and the structure of statements. It highlights that PHP is partially case-sensitive, with constructs and function names being case-insensitive while variables are not. Additionally, it notes that statements end with a semicolon and that whitespace and line breaks do not affect the code's functionality.

Uploaded by

harleensingh985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP code Syntax

Like HTML, you need to have the opening tag to start PHP code:

<?php
If you mix PHP code with HTML, you need to have the enclosing tag:
?>

For example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Syntax</title>
</head>
<body>
<h1><?php echo 'PHP Syntax'; ?></h1>
</body>
</html>
However, if a file contains only PHP code, the enclosing tag is optional:

<?php
echo 'PHP Syntax';

Case sensitivity
PHP is partially case-sensitive. Knowing what are 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 true and false.
• 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, e.g., assigning a value to a variable and 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";

The above example is a simple statement. PHP also has a compound statement that
consists of 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();
}

You do not need to place the semicolon after the curly brace (}).

The closing tag of a PHP block (?>) automatically implies a semicolon (;). Therefore,
you do not need to place a semicolon in the last statement in a PHP block. For example:

<?php echo $name ?>

In this example, the statement echo $name does not need a semicolon. However, using
a semicolon for the last statement in a block should work fine. For example:

<?php echo $name; ?>

Note that it is OK if the code may not make any sense to you now because you will
learn more about them in the upcoming tutorial.

Whitespace & line breaks


In most cases, whitespace and line breaks do not 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( $username, $password );

And:

login(
$username,
$password
);

Summary
• PHP is partially case-sensitive.
• PHP constructs, function names, class names are case-insensitive, whereas
variables are case-sensitive.
• A statement ends with a semicolon (;).
• Whitespace and line breaks do not matter in PHP; do leverage them to make the
code more readable.

You might also like