This chapter will show you some of PHP\’s very basic syntax, which is very important for building a strong PHP foundation.
Syntax Overview of PHP
Escaping form PHP
The PHP parsing engine needs a way to tell PHP code apart from other page elements. \”Escaping to PHP\” is the term for the way to do this. This can be done in four ways.
Canonical PHP tags
The PHP tag style that works best for everyone is.
<?php…?>
If you use this style, you can always be sure that your tags will be read correctly.
Short-open (SGML-style) tags
Tags that are short or open quickly look like this:
<?…?>
As you might expect, short tags are the shortest choice. To get PHP to recognise the tags, you must do one of two things.
When building PHP, choose the —enable-short-tags configuration option.
In your php.ini file, turn on the short open tag setting. 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 used by Active Server Pages to delineate code blocks. ASP-style tags look like this −
<%…%>
To use ASP-style tags, you will need to set the 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 the portion of a programme that exists only for the human reader and stripped out before displaying the programmes result. There are two commenting formats in PHP −
Single-line comments − They are generally used for short explanations or notes relevant to the local code. Here are the 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\”;
?>
Multi-lines printing − Here are the examples to print multiple lines in a single 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\”;
?>
PHP doesn\’t care about whitespace
Whitespace is everything you type that doesn\’t show up on the screen. This includes spaces, tabs, and carriage returns (end-of-line characters).
PHP is \”whitespace insensitive,\” which means that it almost never matters how many whitespace characters are in a row.
One character for white space is the same as many other characters.
For example, each of the following PHP statements that assigns the sum of 1+1 to the variable $fr is the same
$fr = 1 + 1;
// single spaces$fr <tab>=<tab1<tab>+<tab>1 ;
// spaces and tabs$fr =
1+
1;
// multiple lines
PHP is case sensitive
Yeah, PHP is a language that cares about case. Check out the following example:
<html> <body>
<?php $amount= 100; print(\”Variable is $amount<br>\”); print(\”Variable is $amount<br>\”); ?>
</body></html>
The following will give this result:
Variable is 100
Variable is
Statements are sentences that end with a semicolon.
Any phrase that comes after a semicolon in PHP is a statement (;).
A valid PHP programme is any set of valid PHP statements that are wrapped in PHP tags. Here\’s a typical PHP statement that gives a string of characters to a variable called $greeting:
$greeting is \”Welcome to PHP!\”;
Expressions are made up of several tokens.
PHP\’s smallest building blocks are indivisible tokens like numbers (3.14159), strings (.two. ), variables ($two), constants (TRUE), and special words like if, else, while, for, and so on that make up PHP\’s syntax.
Braces make blocks.
Even though statements can\’t be put together like expressions, you can always wrap a group of statements in curly braces and put them anywhere a statement can go.
In this case, both statements are true.
if (3 == 2 + 1) print(\"Good - I have totally understand it.<br>\");
if (3 == 2 + 1) {
print(\"Good - I havetotally\");
print(\"understand it.<br>\");
}
Command prompt to run a PHP script
Yes you can run your PHP script on your command prompt. If you have the following in your test.php file:
<?php \”Hello, PHP! \”;?>
Now, type the following at the command prompt to run this script:
$ php test.php
The following will happen as a result:
Hello PHP!!!!!
I hope you now understand the fundamentals of PHP Syntax.