What is basic PHP syntax?
Basic PHP syntax is the set of rules that make up the structure of the PHP computer language.
PHP, which stands for Hypertext Preprocessor, is a popular, open-source, general-purpose scripting language that can be used with HTML. The .php
extension is used to save files that use PHP.
There are four ways that the parser can tell where PHP code starts and stops:
- PHP Canonical Tags
- SGML-style (Short-open) tags
- ASP Style tags
- HTML script tags
PHP Canonical Tags
The canonical opening tag of the PHP script is <?php
and the closing tag is ?>
.
Additional information from PHP Official Documentation, when PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tells PHP to start and stop interpreting the code between them.
Example:
<?php
# This function is used to print
echo "Welcome to PHP Tutorial!"
?>
Output:
Welcome to PHP Tutorial!
PHP SGML-Style (Short Open) Tags
These tags are the shortest and easiest way to represent PHP code. The opening tag of SGML-Style is <?php
and the closing tag is ?>
.
However the SGML-styled tags are harder to set up than the canonical option, which is almost a standard.
As you might expect, short tags are the shortest choice. To make PHP recognize the tags, you must do one of two things.
- When setting up PHP, choose the
enable-short-tags
configuration option.
- In your
php.ini
file, turn on the setting for a short open tag. To parse XML with PHP, this option must be turned off because XML tags use the same syntax.
Example:
<?
# This function is used to print
echo "Welcome to PHP Tutorial!"
?>
Output:
Welcome to PHP Tutorial!
PHP ASP Style Tags
ASP-style tags look like the tags that are used to separate code blocks in Active Server Pages. Different parts of the code are marked with signs. Here’s how they are used in PHP:
For this code block to work, we need to change the php.ini
file.
<%
# This asp tag can be used if you allow php.ini to use %
echo "Welcome to PHP Tutorial!"
%>
Output:
Welcome to PHP Tutorial!
PHP HTML Script Tags
They are commonly used by web programmers and developers, especially when putting in scripts from other files like JavaScript or CSS. HTML is the first thing most people who build websites learn.
But in the most recent version of PHP 7, this option was taken away, so you can no longer use it.
This is how HTML script tags look:
Example:
<script language="php">
# This echo is used to print
echo "Welcome to PHP Tutorial!"
</script>
Output:
Welcome to PHP Tutorial!
Case Sensitivity in PHP
Below explains the case sensitivity of PHP:
PHP is insensitive to whitespace
This includes tabs, spaces, and carriage returns, which are not visible on the screen. One space is the same as any number of spaces or carriage returns.
This means that PHP will ignore any spaces, tabs, carriage returns, or carriage returns in more than one row. If PHP doesn’t see a semicolon, it thinks of multiple lines as a single command.
Example:
<?php
$x =
50;
$y = 45;
$add = $x
+
$y;
// This code below prints the sum of x and y variables
echo $add."\n";
// This code below is the same as the code above but without spaces
$add1 = $x + $y;
echo $add1;
?>
Output:
95
95
Both of them give the same, correct results.
PHP is case-sensitive
Except for variables, all PHP keywords, functions, and class names (while
, if
, echo
, else
, etc.) are case-insensitive. Only variables that have different cases get different treatments.
Let’s take a look at this:
<?php
// The code below is executed the same but in different cases of echo
$var = "Hello PHP!";
echo $var."\n";
ECHO $var."\n";
EcHo $var."\n";
// The code below prompts an error called
// "Undefined Variable" because of the case sensitivity to variable
echo $VAR
?>
Output:
Hello PHP!
Hello PHP!
Hello PHP!
PHP Notice: Undefined variable: VAR in HelloPhp.php on line 11
Comments in PHP
When the code is reviewed again after a while, comments help the developer remember what it does.
The purpose of a comment is to make the code easier to read and understand. These are utilized to assist other users and developers in describing the code and its purpose.
It may also be used to document a collection of codes or program components. Surely you saw this in the preceding sample applications.
PHP supports two types of comments:
- Single Line Comment
- Multi-Line or Multiple Line Comment
Single Line Comment
As the name implies, these are single lines or brief relevant explanations that can be added to one’s code. To include this, start the line with (//
) or (#
).
Example:
<?php
// This is a single line comment
// These cannot be extended to more lines
echo "PHP Single Line Comment"; // This is a comment also
# This is also a single line comment
?>
Output:
PHP Single Line Comment
Multi-line or Multiple line Comment
These are used to allow several lines with a single tag and can be expanded to the number of lines desired by the user.
The line must begin with (/*
) and ends with (*/
) to accommodate this addition.
See the example below:
<?php /* This is a multi line comment You can use this to comment a code or describe a code */
echo "PHP Multiple Line Comment"; ?>
Output:
PHP Multiple Line Comment
PHP – Blocks
Using curly brackets ({}
), multiple PHP statements can be performed simultaneously (under a single condition or loop) in PHP.
This creates a block of concurrently performed statements.
<?php
$x = 123;
if ($x<150){
echo ("Variable x is ");
echo ("lower than 150");
}
?>
Output:
Variable x is lower than 150
Related Articles
- PHP Ucwords (With Advanced Examples)
- PHP Add Days To Datetime With Example
- PHP Trim Function With Examples
- Types of Operators In PHP (With Examples)
- PHP Echo Array with Example
Summary
In summary, we’ve gone over the PHP Basic Syntax. We’ve explained each detail and shown you some examples for better understanding.
Lastly, if you want to learn more about the basic syntax of PHP, please leave a comment below. We’ll be happy to hear it!
Dear sir:
I wish you a long life of prosperity for providing such valuable lessons to the people thanks for your effort.
best regard