PHP: Getting Started

PHP: Getting Started

Tutorials >  PHP >  Getting Started

Getting Started with PHP

PHP is one of the most simple and popular web programming languages around. It is mainly used in web sites as an alternative to many CGI functions, and can be very powerful.

The following tips will help you to get started with PHP. Learn these at the start, and it will make your PHP journey a whole lot easier.

  1. Use the proper opening and closing tags at all times.
  2. Always make sure that each declaration ends with a semi-colon.
  3. Learn a code structure, and stick to it.
  4. Know the language of variables.
  5. Know how to join strings and variables together.
  6. Know PHP's basic special characters and strings.

1. Use the proper opening and closing tags at all times.

There are two ways to start a PHP script: <? and <?php. Many people use the simpler of the two (<?) which is not good because of the similarities between PHP and XML tags (namely the first two characters <?). Always using <?php removes the possibility for any complications.

PHP scripts always end with ?>.

e.g.

<?php // Start PHP

Your script

?> // End PHP

2. Always make sure that each declaration ends with a semi-colon.

It is absolutely vital that each declaration within a PHP script ends with a semi-colon (;). If you fail to do this, your script WILL fail. Many script failures are a result of a missing semi-colon, and it can be an absolute pain to trawl through a script looking for the absence of one. This is where step 3 comes in.

e.g.

<?php
somecode();
?>

3. Learn a code structure, and stick to it.

Learn a code structure, and stick to it! This is a major issue with many programmers: they fail to stick to a set way of structuring their code. Although this is usually not a problem for the server, it can lead to difficulties. The main point of keeping to a structure is for clarity and debugging. If your code is all messy, and someone else tries to read it (or even yourself after some length of time), they will find it very difficult and annoying (voice of experience!). Learn this vital concept now, and you will save yourself hours of anger and frustration.

Please note that there is no set way to structure code. If you're new to PHP, just start by giving each declarative a separate line and use indentation; just doing this can save hours of time in the future. Do not be intimidated by how long a PHP script is either, in most cases it is a collaboration of simple PHP with a few more complicated parts.

Rather than trying to find a perfect style, stick with one you know. Consistency is always best!

<?php

switch($id) {
  case "1":
    echo "Value 1 has been selected.";
    break;
  case "2":
    echo "Hello, you have selected value 2";
    break;
}

?>

Note the indentations and separate lines.


4. Know the language of variables.

Almost every single script out there will use a variable of some kind. Variables are used to define and hold a set value; this can be a string, number, array, or anything really. To start out, you will probably only use the simplest of variables, defined with a dollar sign ($) before the variable name. A variable is set with the equals (=) sign. There are many more ways of defining and using variables, but for now, the simple ones will do. The main thing to remember with variables is their differences: you cannot define a variable with a string and use it in a function requiring a number.

e.g.

<?php
$variable = "this is a string variable";
?>

5. Know how to join strings and variables together.

Knowing how to join strings and variables together is very simple, but can save a lot of time and excess code. It can also help to make your code easier to read as you can separate one long line onto several shorter lines.

The simplest way to include a variable in a string is just to add it in. To do this though, the string must be within double quotes. Using single quotes in effect tells PHP that it the string should be taken exactly as is written – with no variable replacement.

e.g.

<?php
echo "Hello $name. Today is $day.";
?>

result: Hello James. Today is Friday.

Above: $name and $day are taken as variables.

<?php
echo 'Hello $name. Today is $day.';
?>

result: Hello $name. Today is $day.

Above: $name and $day are output as literal strings.


You can also join variables to strings by closing the quotation, adding a full stop (or period), and then the variable. You can carry the string on by adding another full stop after the variable, followed by another quoted string or variable.

e.g.

<?php
echo 'Hello ' . $name . '. Today is ' . $day;
?>

result: Hello james. Today is Friday

Above: output is something like "Hello Bob. Today is Tuesday.".

Using the full stops (periods) also allows for line separation:

e.g.

<?php
echo "Well, this is a very, very long sentence " .
     "that needs to be split over several " .
     "lines to ensure that it can be easily read.";
?>

6. Know PHP's basic special characters and strings.

The final piece of helpful information is about PHP's special characters. These characters are reserved by PHP. I have only included a very small amount, but they should be enough to get you going.


\ Backslash – the escape character. Used to tell PHP that a special character is coming next.
\n Backslash and the letter n. Starts a new line.
\r Backslash and the letter r. Equivalent of MS carriage return.
// Comments. Stops the following text from being parsed. Applies to line only.
# Comments. Same as //.
/* ... */ Block Commenting. Stops the encased text from being parsed. Can be used on multiple lines.

e.g.

<?php

/* This text will not
be parsed at all. You can
take up several lines. */

echo "<a href=\"index.php\">Index</a>\n" . // Homepage
     "<a href=\"about.php\">About</a>"; # About Link

?>

Above: output within the html file created looks as follows:

<a href="index.php">Index</a>
<a href="about.php">About</a>

Notice that the href value quotes have been escaped to stop PHP from thinking that they were closing the string. \n has been used to create a new line in the HTML file.



  • Celox
  • fx.vimixx
  • iPhotoshop
  • Colin Smiley
  • Best SOTW
  • T-Tutorials

Page Date: Fri 21 Nov 2008 | Page Time: 13:06:11 GMT | User IP: 38.103.63.56
Page created in 1.854 seconds