| | | | |

Bitesize PHP – First Steps in PHP!

The best first steps in PHP is just to jump in!

Set PHP Variable

You can set a temporary storage device, called a PHP Variable or “$var” for short.

Something like:

  $my_page_title = 'My First Page Title!';

Basic Syntax

I highly recommend you read the PHP manual on Basic Syntax. This includes:


 However, here is a quick overview of…

Important Things

Here are a few important things to notice.

PHP Tags

The first is that PHP is always ( almost always! – see Template Echo below for the one exception! ) wrapped on the PHP Tags:



Each PHP Statement (PHP Instruction) ends in a semicolon “;” and when your code fails (and we all make mistakes) check first if you forgot the semicolon! This is the most common error new PHP coders make.

Variable Name – Best Practices

  • Use lower case ( and numbers if desired ).
  • Start a variable name with a lowercase alpha character ONLY! ( $a_good_variable_name )
  • Describe what is in the variable. ( $my_page_title )
  • Use 2-4 Whole Words
  • Separate Whole Words with “_” underscore characters. ( $my_page_title )
  • Don’t use a CammelCaseVariable.
  • Don’t use UPPERCASE letters in variables.
  • Don’t start with an underscore ( $_var_that_is_bad )
  • Don’t start with a number ( $2b_or_not_2b_is_bad )
  • Don’t use initials (readability is more important than length. $first_name not $fn ).
  • Don’t use abbreviations (readability is more important than length. $street_address not $st_add ).

Single Quote Literal

The single quite marks (‘) makes the string Literal. You can use a double quote (“) but it has some special features like converting $vars into its value.  I tell my students to “always use a single quote, unless you need a double quote.” Then lower their grade if they use double quotes where a single quotes will work. Sometimes double quotes will cause your thinking to scramble!

Output the Variable

To output the variable to the browser use the echo statement like this:

  echo $my_page_title;

Polyglot Restriction

Because we do Polyglot coding, we never use the PHP Short Tags. Since PHP5.3 they are turned off in servers by default.


These interfere with some XML tags used in XHTML5, and they are turned off in most servers. Use them now and you will pay later in non-portable code! Best Practice, just don’t use them!

Template Echo

There is one exception, the template echo. Since PHP 5.4.0 the template echo tag is always available. It looks like this:


This works identical to the echo command above. It is most useful in template files where the code is mostly HTML, with just the echoing of PHP variables. Most PHP will be done separately and only the answer variable will be echoed.

PHP Variable and Template Echo Example

See if you can understand this PHP Code?

<?php // located at /index.php

  $url_path = '/path/to/document/';
  $page_title = 'My First PHP Page Title';

<!DOCTYPE html>
<html lang="en-US">
<meta charset="UTF-8" />
  <link rel="stylesheet" type="text/css" href="<?=$url_path?>css/style.css" />
  <link rel="stylesheet" type="text/css" href="<?=$url_path?>css/marquee.css" />
  <div id="page">
    <p>The Title of this page is: <strong><em><?=$page_title?>!</em></strong></p>

The Source Code output to the Browser:

<!DOCTYPE html>
<html lang="en-US">
  <meta charset="UTF-8" />
  <title>My First PHP Page Title</title>
  <link rel="stylesheet" type="text/css" href="/path/to/document/css/style.css" />
  <link rel="stylesheet" type="text/css" href="/path/to/document/css/marquee.css" />
  <div id="page">
    <h1>My First PHP Page Title</h1>
    <p>The Title of this page is: <strong><em>My First PHP Page Title!</em></strong></p>

We solve a problem with this in

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *