PHP - My SQL (Chapter - 2: PHP Syntax)

Opening/Closing PHP Tags, PHP Comments, PHP Whitespace

1. Opening/Closing PHP Tags


The PHP syntax is a set of rules that define how a program should be written. PHP is, in that sense, no different from any other language. If I wrote this sentence backward and upside down, it would not be following the rules, and it is doubtful that anyone would understand it. The PHP interpreter expects certain rules to be followed, and will spit errors at you if they are not followed.

All code written in PHP must be identified as PHP code. A set of tags are used to mark the beginning and end of a block of code, in between which any amount of code can be written.

The standard opening tag is:

<?php

The standard closing tag is:

?>

These tags can be used to jump in and out of "PHP mode" any number of times in a PHP file, including a PHP file containing HTML elements.

<?php /* Code Can Go Here */ ?>
<html>
<head>
  <?php /* Code Can Go Here */ ?>
</head>
<body>
  <?php /* Code Can Go Here */ ?>
</body>
</html>
<?php /* Code Can Go Here */ ?>

One other set of tags is readily available, but not practical when there is the shorter alternative. This set is:

<script language="php"> </script>

Also, several sets of tags are available if PHP's configuration is set to allow their usage, which make them less portable and not desirable for general use:

<?   ?>
<?   ?>
<%   %>
<%=   %>

All examples in this tutorial will use , as it is doubtful that you will ever need an alternative.

2. PHP Instruction Termination


Each statement made in PHP needs a method of indicating the end of the statement so that each instruction can be carried out without mix-up. The semicolon ";" is used for this purpose.

<?php
$variable = "Why can you never swindle a snake?";
echo $variable;
?>

3. PHP Comments


If the urge ever hits to write something down in the middle of a block of PHP code, you can do this one of two ways.

1. You can type in your comment and move on, causing errors in your program.
2. You can label your comment as a comment and move on without causing errors (assuming, of course, that all of the other code is correct).

Hopefully you will choose the second option, in which case you once again have two options.

1. You can comment out a single line of code.
2. You can comment out a large block of code.

Comments are most often used for two purposes:

1. To note what is happening at certain points throughout a block of code. (You will probably be thankful for properly used comments years later when you return to your own long-untouched code, or try to understand another programmer's code!)
2. To temporarily keep a line or multiple lines of code from being interpreted, without deleting it completely (a useful option during testing).

Comments are never seen by anyone who cannot see the php code. Three different methods can be used to create comments:

//
#
/*     */

They can be used in the following manners:

<?php
// This comment only spans one line.
// It can, however, be used on as many lines as necessary.

# This comment only spans one line.
# It is not used as commonly as the previous type.

/* This comment can span one line. */
/* This comment can also span as many lines as needed.
It is useful when commenting out large chunks of code at a time.
This type of comment cannot be nested, or errors will occur.
*/
?>

4. PHP Whitespace


Whitespace is not significant to the PHP interpreter. You can make indentations and have multiple blank rows anywhere in your code without creating problems.

The extra whitespace in the following examples is all ignored by the interpreter:

<?php echo 'Teacher: We will have only half a day of school this morning.'; ?>

<?php
echo 'Class: Hooray!';
?>

<?php


echo 'Teacher: We will have the other half this afternoon!';


?>

Continue to Index

Welcome to HoneyBee - The Learning Platform

By Binit Patel

HoneyBee is a learning platform which is an integrated set of informative online services that enable learners involved in education with information, tools and resources to support and enhance Teaching, Learning and Management.

Contact With Me