PHP 101

By Margaret May

Organized by Elena Levine

Partially based on a GDI class created by Sylvia Richardson

CLASS 4: Best Practices and More...

What We Will Cover Today

  • Go over homework
  • Best Practices
  • Time permitting: bonus material

----- Best Practice: Templates ------


  • Considered a best practice...
  • ...because they help us avoid inline PHP

So what is bad?

Mixing different types of code horribly

So what is good?

Templates!! What Templates do for us:

  • Code is easier to read, maintain, and reuse
  • The same content can be displayed in multiple ways
  • More secure

When to Use Templates

  • When working on larger, multi-page websites
  • Almost all of the time

When Not to Use Templates

  • When writing one-off scripts
  • When writing scripts to test small pieces of code

Okay you sold me on Templates! Now where to start...

Template Options

  • Various template engines
  • Building a template of your own from scratch

Building a Template from Scratch

What we are given: a large, unwieldy multi-page website. What we want: a website that is easier to maintain, code that is easier to read, etc. - all while learning exactly how templates work.


How to Get Started...


----- Best Practice: Planning -----

Planning Techniques

  1. Analyse what we have
    • What we have: large multi-page website
  2. Define what we have to do
    • What we have to do: make it easier to maintain and change
  3. Analyse our code for duplication - design arises from the elimination of code duplication
  4. Mock or simulate
  5. Diagramming
  6. Probably more...

Planning Techniques we are going to use

  1. Analyse what we have - multi-page website
  2. Look at where we have code duplication

An Index Page: index.php

Scrolling down index.php

An "About" Page: about.php

Scrolling down about.php

Let's put duplicated content into one file: variables.php


	$heading = "1stWebDesigner PHP Template";

	$footer = "Copyright © 2010 1stWebDesigner PHP Template";



<?php include('variables/variables.php'); ?>



<?php include('variables/variables.php'); ?>






Putting it all together - a Template we can use!

		<link rel="stylesheet" type="text/css" href="style.css" media="screen" />

		<div id="wrapper">

			<?php include('includes/header.php'); ?>
			<?php include('includes/nav.php'); ?>

			<div id="content">
				<h3>Paragraph Element</h3>
				<p>Quisque pellentesque sodales aliquam. More content here..</p>
			</div> <!-- end #content -->

			<?php include('includes/sidebar.php'); ?>
			<?php include('includes/footer.php'); ?>

		</div> <!-- End #wrapper -->


Exercise 1

  1. Create a file called template.php and copy and paste the code from the slide "Putting it all together - a Template We Can Use!" into template.php.
  2. Open template.php in a browser.
  3. Notice all of the errors that you get!
  4. Start fixing the errors one-by-one. Use will need to use the exercise files as a reference. Hint: you will have to create a number of new files. As you create files, reload template.php frequently to watch your errors disappear.
  5. If you have time, start fixing the broken links in your site.

Click here to run my answer!

Exercise 2

  1. Change the name of your website to "FILL YOUR NAME IN HERE's Awesome Website"
  2. Change the "About Us" link to "About Me"
  3. Continue customizing your website in other, different ways.
Click here to run my answer!

Best Practice: Comments


/* This is a multi line comment
   yet another line of comment */

// Assign value to number x
$x = 5;

// Add two numbers x and y and the sum is z
$z = $x + $y;

// Output the sum to the browser
echo $z;

Source: Source:

Best Practice: Always clearly start and end PHP code blocks

Bad form

//this is your PHP code

Good form

//this is your PHP code

Best Practice: understand single versus double quotes

 $name = 'David';
 echo 'Hi, my name is $name "The Crazy" Jones.';

 $name = 'David';
 echo 'Hi, my name is ' . $name . ' "The Crazy" Jones.';

Hi, my name is $name "The Crazy" Jones.

Hi, my name is David "The Crazy" Jones.

Thank you!!

Please take our class survey!

------ Bonus Material! ------

PHP at the Command Line

Getting Started with the PHP Command Line

  1. Let's create a directory where scripts like this should go.
  2. Locate the directory where XAMPP is installed
  3. Find the php folder
  4. Create a folder called 'gdi'
  5. Write a script called helloworld.php echoes "Hello World!"

Our Finished Script!

echo 'Hello World!';


Running our Script!

  1. Open an XAMPP command shell.
  2. Change directories to the XAMPP php directory
  3. Type: php helloworld.php

Command-line Scripts

  • are a very good habit of getting into in order to truly figure out how parts of PHP work