Tag Archives: Learning PHP

A very gentle introduction to PHP

So what is PHP?
A Server side scripting language – code that is executed before the page loads (the opposite being client-side, JavaScript for example).  PHP can generate HTML and is often embedded directly with HTML to generate dynamic web pages. 

$question = ‘Can we go on break now?;
$time = 1045;
$breaktime = 1100;

echo “<h1>$question</h1>”;

if ($time >= $breaktime) {
echo “<p>Good call, see you in five</p>”;
else {
echo “<p>Computer says no</p>”;

The server will then compute this script. Any ideas on what the output is?

Why PHP?
The snippet of code above demonstrated a conditional – You can automate what HTML is displayed based on certain conditions. You can also store variables, which allows for greater flexibility when writing web pages.

Who’s using PHP?
Facebook, Twitter, Youtube, WordPress, Google, Yahoo, Wikipedia. Alernatives to PHP are asp.net (Microsoft Corp) JSP (Oracle) and Ruby (open source)

The amount of data stored in websites these days is impressive – a typical website these days has usernames and passwords. To authenticate a user, you must match their password with the username. It is pretty easy to match two values together (hint: the conditional). Easy right? Yes! So how is it done…?

Members’ email addresses are stored in a database and we’ll grab this information using PHP and store it in a variable (I’ll explain this in more detail later) The code below demonstrates how to detect if the user actually exists:

$members = array(‘John’, ’Mary’, ’Umi’, ’Sarah’, ‘Lucy’);
$login = ‘John’;

foreach ($members as $member) {
if ($member == ‘$login’) {
echo “Hello $login”;

Now if you understood what went on above, well done! You’ve just mastered the loop – the most essential part of programming! The syntax  for PHP arrays is: $members = array(‘John’, ’Mary’, ’Umi’, ’Sarah’, ‘Lucy’);

Items in an array are called elements and if I wanted to output the second element in this array:

<?php echo $members[1]; ?>

That’s no mistake. Array element [0] is the first element, [1] = second and so forth.

To print the entire array:

<?php print_r($members); ?>
echo and print_r are both functions. They do something to the arguments passed to them. The PHP language has many built-in functions and a developer can also write their own. Functions make code easier to read and allows for code re-use. Functions are a lot like people – you feed them something and then they spit something out in one way or another:

function a_human_being($food) {
if ($food == tasty) {
return ‘burp’;
else {
return $food;

If you are confused at this point then give yourself a pat on the back for keeping up. It definitely takes time for the concepts to sink in and I guarantee that by the time you enrol on to the next PHP training course you will be using these concepts as though you knew them all your life. You’ll be making awesome websites in no time!