|
PHP Tutorial >> Syntax PHP - Syntax
Before we talk about PHP's syntax, let us first define what syntax is referring to.
PHP's syntax and semantics are similar to most other programming languages (C, Java, Perl)
with the addition that all PHP code is contained with a tag, of sorts. All
PHP code must be contained within the following...
Display:
<?php
?>
or the shorthand PHP tag that requires shorthand support to be enabled
on your server...
<?
?>
If you are writing PHP scripts and plan on distributing them, we suggest
that you use the standard form (which includes the ?php) rather than the shorthand
form. This will ensure that your scripts will work, even when running on other
servers with different settings.
How to Save Your PHP Pages
If you have PHP inside your HTML, like we do above, and you want the
web browser to interpret it correctly you must save the file as .php
instead of .html. So be sure to check that you are saving your files
correctly. Instead of index.html, it should be index.php if there
is PHP inside included in the file.
Example Simple HTML & PHP Page
Below is an example of one of the easiest PHP and HTML page that you
can create and still follow web standards.
PHP & HTML Code:
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
If you save this file and place it on PHP enabled server then you should see
"Hello World!" displayed. If not, please check that you followed our example
correctly.
We used the PHP function echo to write Hello World and we will be talking in greater depth about
this PHP function and others, later on in this tutorial.
The Semicolon!
As you may or may not have noticed in the above example, there was a semicolon after
the line of PHP code. The semicolon signifies the end of a PHP statement and
should never be forgotten. For example, if we repeated our "Hello World!" code several times, then we would
need to place a semicolon at the end of each statement.
White Space
As with HTML, whitespace is ignored between lines of code. This means it is OK to have
one line of PHP code, then 20 lines of blank space before the next line of PHP code.
PHP & HTML Code:
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
echo "Hello World!";
?>
</body>
</html>
This is perfectly legal PHP code.
|