By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

PHP General Syntax Rules

Tutorial on the Syntax of Strings in PHP, covers usage of double, and single quotes.

Edited: 2011-03-16 02:35

Before i explain more about variables, lets go through some basic syntax rules.


A Syntax is basic the rules of a language, in this case PHP. The syntax of PHP is very strict, if you make the slightest typo or mistake, your script will throw out an error, usually explaining what might went wrong. The basic syntax of strings is as follows:

echo 'My First String';

Extended Syntax

Other (perhaps better) ways of echoing the result from last Tutorial, would be as follows:

$RandomNumber = rand(1, 4);

  // supposely faster because php dosn't need to check the intire string for variables
  echo 'The number was '. $RandomNumber;
  // you can add a dot at the end, and continiue the string.
  echo 'The number was '. $RandomNumber . 'Continiued String';
  // Same syntax when using double quotes
  echo 'The number was '. "as high or low as $RandomNumber" . 'Continiued String';
  // perhaps most useful when dealing with multiple variables?
  echo 'The number was '. "$RandomNumber The $RandomNumber is generated using the rand function" . 'Continiued String';


Using single quotes when appropriate, will increase the speed of your applications.

For instance:

echo 'This is a very long String';

Is faster then:

echo "This is a very long String";

This is because PHP is checking the string for variables when using double quotes.

Finally the semicolon at the end, tells PHP that the string ends. When something goes wrong, its usually a missing semicolon at the end, so always check for the most obvious typos first.

Before you move on, you should read about String Concatenation, in the Reference entryVariables in PHP