getting started with PHP :: part 1

An introduction to writing PHP and working with variables.

(For a start, I’m going to assume you have some sort of FTP thing set up and have a server (online or local) which you can upload files to. If you’d like to set up a site in Dreamweaver and aren’t really sure how, then kindly read ‘creating a site in Dreamweaver’.)

PHP is a server-side scripting language which means that to play along with this, you’ll need a server that has PHP running on it. If you’re unsure and want to do a simple test, then upload a page with nothing but this in the code;

<?php echo ("hello!"); ?>

That should display a simple ‘hello’ when you view the page in your browser.

simple php echo of hello

If it doesn’t, you’ve either copied that wrong or not got PHP installed. Alas.
If it does, then read on..

For ease of use and because I’m used to the way it colours things, I use Dreamweaver to write my code but you can use any text editor and it’ll produce the same result.

Before I carry on, I should mention that ALL PHP code should appear within opening and closing brackets like so:

php brackets

Including the letters ‘php’ after the first question mark is optional and does not affect the code itself.

variables

Like ActionScript (and countless other programming languages), PHP makes good use of variables, which are basically written like this;

$number=5;
$text="hello";

The dollar sign is the crucial bit! Variables are case sensitive and can contain numbers (integers), strings (usually of text) or a boolean value (true or false).

As with all PHP, you can declare variables at any point within your code (and also modify them at any point) and then pull them in as required. For instance, something like:

<html>
<title>php is fun!!</title>
<body>
<h1>my wonderful php page!</h1>
<p>Welcome to my test php page..</p>
<?
$name = "matt";
$fave_number = 7;
?>
<p><? echo ("My name is $name and my favourite number is $fave_number."); ?></p>
</body>
</html>

produces a page looking like this:

using variables inside html

As you can see, the ‘echo’ function basically places content into your html when it gets rendered. So, we’ve echoed some simple text which incorporates the variables defined further up the code and the final html output is seamless. You can also achieve this by placing an ‘echo’ call within some html; for instance, you could replace the line from the code above with:

<p>My name is <? echo ($name); ?> and my favourite number is <? echo $fave_number); ?>.</p>

and you would get the same result.

You can also write extended variables which consist of multiple lines (and even contain html within them!)

$extendedText = "This is first line<br />";
$extendedText .= "This is second line<br />";
$extendedText .= "This is third line<br />";

They can also can be constructed using other variables using concatenation. For instance, writing

$first_name = "matt";
$second_name = "northam";
$full_name = $first_name . ' ' . $second_name;

will allow you to utilise a complete $full_name variable with a space between the two words.

And variables can also contain paths to other files such as images, but you must ensure you insert a backslash at the correct point so that your variable does not close itself too soon..

$img="<img src=\"images/pic.jpg\">";

using comments

Personally, I find it really helpful to use comments in my PHP code, to remind me what a particular piece of code relates to for instance. Anything that’s in a comment wont get rendered as part of your PHP code. There’s a couple of ways to include a comment within your PHP brackets, either on a single line or as multiple lines. (The multiple lines method is also good for ‘commenting out’ chunks of code for testing purposes when you don’t want to actually remove the code completely).

// Single line comments start with 2 backslashes
# Or even a hash symbol
/* Whereas comments across several lines are bookended by
a backslash and an asterisk
and then an asterisk and a backslash */

php commenting

Stuff.