[PHP] Introduction

Syntax

When writing PHP code it is best to use the following opening and closing statements:

<?php
?>

On some websites and scripts you may see it written like this:


This is not good practice as not all servers will be set up to allow this shorthand tag.

File extension

Files with PHP in should generally have the file extension .php but depending on the server configuration, other extensions can be used such as .php3, .php4, .php5, .html and more.

Closing statements

All commands and statements in PHP have to be closed before you can make another so that PHP knows where one item ends and the next one starts.

For example, an echo statement should end with a semi-colon:

<?php
echo "hello";
?>

Where as an if statement just needs curly brackets around it:

<?php
if ($item 4) {
  echo 
$item.'<br />';
}
?>

Comments

Comments in PHP can be done in a few ways.

Single line comments

Single line comments are comments that only span one row, in our echo statement above we could add a comment after the echo to explain what our node does.

<?php
echo "hello"// prints out hello on the screen
?>

All the user will see on their screen and the source code (when viewing source from their browser) for this is:

hello

We can also stop code from showing by adding comments before the line of code.

<?php
// echo "hello";
?>

In this case the user won't see anything as we have commented out the line.

You can also use the following for single line comments:

<?php
# echo "hello";
?>

Multi line comments

Multi line comments are comments that span more than one line by using:

<?php
</pre>

You can also use this commenting way to take out blocks of code temporarilylike so:

<
pre lang="php"><?php
/*
if ($item > 4) {
  echo $item.'<br />';
}
*/
?>
</pre>

<h2>Whitespace</h2>

Much like HTML, whitespace is not important and is ignored and is purely for the presentation to the developer.  The following 2 bits of code both produce the same HTML.

<pre lang="php"><?php
echo "hello";
echo 
"hello";
?>
</pre>

<strong>Outputs:</strong>

<pre lang="html">hellohello</pre>

<pre lang="php"><?php
echo "hello";





echo 
"hello";
?>
</pre>

<strong>Outputs:</strong>

<pre lang="html">hellohello</pre>

<h2>Variables</h2>

Variables are what you can use to store bits of information to reuse them later on in your script. For example:

<pre lang="php"><?php
$variable_name 
'value';
echo 
$variable_name;
?>
</pre>

<strong>Outputs:</strong>

<pre lang="html">value</pre>

Variable names have certain restrictions that you must keep to for your script to work as expected.

<ul>
<li>Must start with either a letter or an underscore (_)</li>
<li>Must only contain alphanumerics (A-z (upper and lower), 0-9 and underscore (_)</li>
<li>Variables are case sensitive.  $myVar is different to $myvar</li>
</ul>

<h2>Outputting data</h2>

There are 2 basic ways of outputting strings and variables.  These are <strong>echo</strong> and <strong>print</strong>.  For the sake of argument they are exactly the same until you get more advanced in PHP and even then you may not have any use for the extra ability of <strong>print</strong>.

<pre lang="php"><?php
print 'hello<br />';
echo 
'hello';
?>
</pre>

<strong>Outputs:</strong>

<pre lang="html">hello
hello</pre>

I tend to stick to using <strong>echo</strong>.

<h3>Echoing strings and variables</h3>

The format of echoing strings and variables does confuse a lot of people and early on it is understandable.

<pre lang="php" line="1"><?php
echo "This is a statement with \"a quote in\".<br />";
echo 
'This is a statement with "a quote in".<br />';
echo 
'This is a statement with \'a quote in\'.<br />';
?>
</pre>

Take note of the format of both of the above lines. Line 2 is enclosed using " " whereas line 3 is using ' '.  If you have to use quotation marks within a string then you should either enclose the string with the alternative quotes or escape them with the backwards slash (\).

Echoing strings can have it's moments too.  Example:

<pre lang="php"><?php
$myVar 
'this is a string';
echo 
'Variable: $myVar<br />';
echo 
'Variable: '.$myVar.'<br />';
echo 
"Variable: $myVar<br />";
echo 
"Variable: ".$myVar."<br />";
?>
</pre>

<strong>Outputs:</strong>

<pre lang="html">Variable: $myVar
Variable: this is a string
Variable: this is a string
Variable: this is a string
</pre>

From the output we can see the last 3 echo statements are correct and we get the result we expect.  But the first one does not.  This is because variables can't be called from within ' ' quotes but they can be called from within " " quotes.

To get around this we close the ' ' and use a . (dot, full stop, period) to concatenate the variable to the string.

Double quotes are also more helpful if using certain escaped characters.

<pre lang="php"><?php
echo "this\nis\na\nnew\nline";
echo 
'this\nis\na\nnew\nline';
?>
</pre>

<strong>Outputs:</strong>

<pre lang="html">this is a new line
this\nis\na\nnew\nline</pre>

<strong>Source code:</strong>

<pre lang="html">
this
is
a
new
line<br />this\nis\na\nnew\nline
</pre>

Other enclosed characters include:

<ul>
<li><strong>Newline:</strong> \n</li>
<li><strong>Carriage return:</strong> \r</li>
<li><strong>Tab:</strong> \t</li>
<li><strong>Dollar:</strong> \$</li>
<li><strong>Double quote:</strong> \"</li>
</ul>

In the case of a backwards slash being used before a character not shown in the list above the backwards slash will be shown as well as the letter succeeding it, example:

<pre lang="php"><?php
echo "\s";
?>
</pre>

<strong>Outputs:</strong>

<pre lang="html">\s</pre>?>