You Are Here: Home »Tutorials»Php_mysql »   Php basics Tuesday January 6th 2009

PHP basics

Introduction

This wont be a very long tutorial, just a quick look at a few things.

  • What is PHP?
  • What can i do with it?
  • How do i use it?
  • Using PHP on your pages
  • Variables

What is PHP?

PHP is a server side scripting language that's used (mostly) within HTML, being a server side language it means the code is processed or "parsed" and sent to the browser as simple HTML, so people don't need any sort of addons to read the pages, like flash plugins etc.

If you don't have a reasonable understanding of the basics of HTML then really you're starting in the wrong place, but if you do, then the basics of PHP at least are not difficult to learn, you'll also find that experience of any other kind of scripting language is a benefit.

What can i do with it?

The first time i looked at a PHP tutorial i thought why do i want to mess around with all this echo and print stuff when i can just type what i want on a blank page and it'll appear? well i soon changed my mind, PHP gives you so many more choices and options than client-side languages like Javascript, you'll soon be adding things to your pages that you probally never thought possibile, or at least didn't expect them to be so simple.

Basically PHP makes it very simple to:

  • Collect, process and store data
  • Create dynamic page content
  • Create a more "interactive" website

And much more ...

Using PHP with one of the many databases that it supports (mysql, dbase, oracle etc) is when it really starts to get fun, but onto that some other time.

How do i use it?

Well obviously first of all you'll need a host that supports PHP, any paid hosting package should give you PHP support these days and there's alot of free hosts that offer PHP now also, though the source itself is free so if you're confident enough you can even look at running PHP and a basic webserver like Apache on your own PC, saving you some hosting fees. Any pages that include PHP scripting need to be given a .php extension, instead of .htm or .html. There are exceptions to this as webservers can be configured to parse PHP in any pages but usually they wont be by default.

First of all, a little PHP syntax.

Tabs, spaces and returns don't really matter for example:

<?php echo "blah" ?>

would be the same as:

<?php
echo "blah"
?>

Each PHP statement that's followed by another must end with a semicolon (;) You can make comments or notes inside your PHP tags in three ways:

<?php

/* comments here */
//comments here
#comments here

 
?>

With the last two, the comments would extend to the end of the line.

Using PHP on your pages

Any PHP that you want to include in your pages is contained within it's own tags, which are:

<?php ?>

You may see some people using just this:

<? ?>

Although you'll probally never come across the problem, the shorter form tags are not supported on all servers so it's probally better to get in the habbit of using the first so your code is always portable.

Now you can add your PHP code at whatever points you like within your normal pages, the first example here being how to output text.

"print" and "echo" are most commonly what we use to display text on the page when we're using PHP, for example:

<?php echo "i like bananas" ?>

Would display the text "I like bananas" (without the quotes) on your page, but you can also use "print" to give the same output:

<?php print "i like bananas" ?>

Would display the same thing. So what's the difference?

Well so far the only differences i know of between using print and echo are that echo gives you the option to output multiple lines, like echo ("do one thing", "do another thing"); print doesn't support using a comma to seperate lines like that, echo is also supposedly a little quicker but as far as i know there's no other differences, most examples and tutorials you'll see tend to use echo 90% of the time.

One other basic example i'll give is setting a variable with PHP, because it's something that eventually you'll use quite often.

Variables

We use variables for values that are likely to change, so we always have something familiar to relate to when we want to display or use that value.

Here's a basic example, it's probally not something you'd use but it just gives the general idea how variables work.

<?php
$title 
"Welcome To My Webpage";
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>

That would set your page title to "Welcome To My Webpage" , so basically we set the variable with $variable = "value"; then we can just echo or print it to our page wherever we like.

Some other basic points about variables in PHP:

  • They can be any size, e.g everything on this page could be set as one variable called $tutorial.
  • All PHP variables are defined by the dollar sign $
  • They must always begin with an alphabetic character (a-z) or an underscore (_) NOT with a number.
  • They are CASE SENSITVIVE so setting a variable named $blah would not be the same as a variable named $BLAH.

I said this wouldn't be a long tutorial, and that's about it, but i hope it helps you to understand a couple of things.

Make a note of this link, it's the PHP manual, basically the guide to everything, functions, variables, cookies, includes and so on ... whatever you need to know is there.