Institute of physics, mathematics and digital technologies
department of computer science and applied mathematics
Abstract
Theme: PHP - Syntax Overview
Prepered: Ermetova Iroda
6B01506-computer science
401-group
Checked: Mukeeva G
Almaty 2022.
PHP - Syntax Overview
Output in PHP
The PHP parsing engine needs a way to distinguish PHP code from other
elements on the page. The mechanism for this is known as "escape in
PHP". There are four ways to do this −
Canonical PHP tags
The most universally effective PHP tag style is
<?php...?>
If you use this style, you can be sure that your tags will always be interpreted
correctly.
Short open tags (SGML style)
Short or short open tags look like this −
<?...?>
Short tags are, as you would expect, the shortest option. In order for PHP to
recognize tags, you need to do one of two things:
Select the --enable-short-tags configuration option when building PHP.
Set short_open_tag in php.ini to on. This option must be disabled to parse
XML with PHP because the same syntax is used for XML tags.
Select the --enable-short-tags configuration option when building PHP.
Set short_open_tag in php.ini to on. This option must be disabled to parse XML
with PHP because the same syntax is used for XML tags.
ASP style tags
ASP-style tags mimic the tags that Active Server pages use to delimit blocks of
code. ASP style tags look like this:
<%...%>
To use ASP style tags, you need to set a configuration option in your php.ini file.
HTML script tags
HTML script tags look like this:
<script language = "PHP">...</script>
Commenting PHP Code
A comment is a part of a program that exists only for the reader and is removed
before the result of the program is displayed. PHP has two comment formats:
Single line comments - These are typically used for brief explanations or notes
relating to local code. Here are examples of single line comments.
<? # This is a comment, and # This is the second line of the comment
// This is a comment too. Each style comments only print "An example with
single line comments" ; ?>
Multiline printing. Here are examples of printing multiple lines in one print
statement.
<? # First Example print <<< END This uses the "here document" syntax to
output
multiple lines with $variable interpolation . note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace ! END ;
# Second Example print "This spans
multiple lines. The newlines will be
output as well" ; ?>
Multiline comments. They are commonly used to provide pseudocode
algorithms and more detailed explanations when needed. The multi-line comment
style is the same as in C. Here is an example of a multi-line comment.
<? /* This is a comment with multiline
Author : Mohammad Mohtashim
Purpose: Multiline Comments Demo
Subject: PHP
*/
print "An example with multi line comments" ; ?>
PHP is insensitive to spaces
A space is text you enter that is not normally visible on the screen, including
spaces, tabs, and carriage returns (end-of-line characters).
PHP's whitespace-insensitive means that it almost never matters how many
whitespace characters you have in a string. One space character is the same as
many such characters.
For example, each of the following PHP statements, which assigns the sum of 2 +
2 to the variable $four, is equivalent to −
$four = 2 + 2 ; // single spaces
$four <tab> =< tab2 <tab> +< tab > 2 ; // spaces and tabs
$four = 2 + 2 ; // multiple lines
PHP is case sensitive
Yes, it's true that PHP is a case-sensitive language. Try the following example −
live demo
<html> <body>
<? php
$capital = 67 ; print ( "Variable capital is $capital<br>" ); print ( "Variable
CaPiTaL is $CaPiTaL<br>" ); ?>
</body> </html>
This will give the following result −
Variable capital is 67
Variable CaPiTaL is
Statements are expressions ending with a semicolon
A statement in PHP is any expression followed by a semicolon (;). Any sequence
of valid PHP statements enclosed in PHP tags is a valid PHP program. Here is a
typical expression in PHP which in this case assigns a character string to a
variable named $reeting −
$greeting = "Welcome to PHP!";
Expressions are combinations of tokens
PHP's smallest building blocks are indivisible tokens such as numbers (3.14159),
strings (.two.), variables ($two), constants (TRUE), and special words that make
up the syntax of PHP itself, such as if , otherwise, while, for and so on
Braces make blocks
Although statements cannot be combined as expressions, you can always put a
sequence of statements anywhere a statement can go by enclosing them in a set of
curly braces.
Here both statements are equivalent −
if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.<br>");
if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.<br>");
}
Running a PHP script from the command line
Yes, you can run a PHP script on the command line. Assuming you have the
following content in your test.php file
live demo
<?php
echo "Hello PHP!!!!!";
?>
Now run this script on the command line as follows:
$phptest.php
This will give the following result −
Hello PHP!!!!!
I hope you now have a basic knowledge of PHP syntax.