PHP Tutorial - Part 1

No, these are not photoshop tutorials!

Moderators: Community Moderators, Tutorial Writers

Post Reply
Fergie1
Posts: 257
Joined: Sun Jul 15, 2007 2:22 pm
Location: UK

PHP Tutorial - Part 1

Post by Fergie1 » Sun Nov 25, 2007 11:09 pm

By phantom

-------------------------------------------------------------------------------------------------------------------------------------------------

I know there is already a php tutorial on here, but I don’t know any other languages so I thought I’d write a php one. Please bear with me as this is the first tutorial I’ve ever written, and I welcome any criticism you may have on this tutorial.

NB. I won't be able to show some formatting of source code in the output as i am not able to use bbcode inside [ code] and [ /code].

Overview
-------------------------------------------------------
This tutorial is intending for someone who is new to PHP, but can aid someone who already knows some PHP to help with their knowledge and understanding of PHP. Try to make up your own examples based on those in this tutorial, this will help you to learn php as you are thinking about the php and not just reading it. Try not to go through the whole tutorial at once, have a few breaks and let what you have learned sink in before continuing with the tutorial.


What is PHP?
-------------------------------------------------------

PHP could be thought of as back stage code, the user doesn’t see it (even if they look at the source or your page in some form of internet browser). This is possible because, unlike HTML where the code is processed in the browser, PHP is processed by the server and the output is sent to the user. PHP allows you to add more functionality to your website and can speed up website creation.


What you will need
-------------------------------------------------------

In order to work with PHP you will need to be able to access a web hosting account that supports the use of PHP and has access to a MySQL database. Alternatively, if you don’t have access to this, or would prefer to work with php on your computer before uploading to the internet you can install PHP and MySQL on you computer. If you want to install MySQL and/or PHP then read the next section for some details on how to do this.

A MySQL database isn’t necessary but MySQL and PHP work very well together, and some interaction between the 2 will be covered in this tutorial.


Installing PHP
-------------------------------------------------------
You can download PHP from here and you can probably install this yourself.

If you are unsure of what to do then you can use the following guides for installing PHP:
Windows Installation Guide
Mac Installation Guide
Linux Installation Guide


Installing MySQL
-------------------------------------------------------

As I said earlier MySQL is not required to use PHP, but it is a good idea to have it installed so you can use it when coding with PHP. If you want to use a guide then there is one here (you probably want to read the section for Community Server if you are installing on your home computer).


Syntax
-------------------------------------------------------

The syntax of a programming language is the rules that must be followed for the code to be properly structured.

All PHP code must be surrounded with <?php and ?> in the following way:

Code: Select all

<?php
//PHP code goes in here.
?>

<?
//Or if shorthand is supported on your server you can just use these instead, but they wont work if shorthand isn’t supported on your server.
?>
If you plan on distributing your php code then it is a good idea to use the standard form (<?php and ?>) as not all servers will support shorthand.


Saving a PHP page
-------------------------------------------------------

If you have already used HTML then you will be used to saving your files with the .html extension, however if there is any PHP included in the file then you will need to use the .php extension. That means that instead of a file being called index.html it should be called index.php assuming there is PHP code inside the file.


A simple example of a PHP and HTML page
-------------------------------------------------------

Now that I have told you about the basic syntax of PHP I will show you a very basic example of implementing PHP inside a HTML layout, and I will also introduce the echo command.

Source:

Code: Select all

<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo “Hello World”;
?>
</body>
</html>
Output:

Code: Select all

Hello World
When you save this with the .php extension and open it in an internet browser with php installed on the machine the file is stored on, you should see the words “Hello World” (without the “ “) appear. If this doesn’t appear then there is a problem with the PHP installation.

As you can see the PHP code was in between the body tags of the html and inside the php tags I talked about earlier. You also will have probably noticed the colon at the end of the PHP line. Almost every line in PHP has to end in this as it signifies the end of a PHP statement. We could have indented the line of PHP, or even had several blank lines, this is because PHP also allows white space the same way as HTML and many other programming languages allow.

The following code is still valid PHP:

Source:

Code: Select all

<html>
<head>
<title>Example</title>
</head>
<body>

<?php
echo “Hello World”;
?>

</body>
</html>
Output:

Code: Select all

Hello World
As you can see this will give the same output as the first code, but the PHP has been split up from the HTML and the line of code indented to make it clearer to read.

You will have noticed that I used the echo command in the PHP code, don’t worry about that now as I will be talking about it very soon.


Variables
-------------------------------------------------------

If you have done any programming then you will probably know what variables are, but if you haven’t then this will be a new concept to you, but no need to worry you will learn what they are and will become comfortable with using them quickly.

To put it simply a variable is a way of storing a value, like “Hello World” as we had earlier. Instead of having to type the same thing out over and over again you can use a variable instead as you can use them throughout your code. In PHP a variable always begins with the dollar symbol follow by the name, a space, an equal’s sign, then the value, then the colon sign to show the end of the line.

Source:

Code: Select all

$variable = Value;
Variables in PHP:
• Must start with a letter or an underscore.
• Must only contain alpha-numeric characters and underscores.

Also note that variables are case sensitive so $Variable and $variable will be 2 different variables.


A variable example
-------------------------------------------------------

Source:

Code: Select all

<html>
<head>
<title>Example</title>
</head>
<body>

<?php
$hello = “Hello World”;
$number = 2;
$different_number = 10;
echo $hello;
echo $number;
echo $different_number;
echo $different_number + $number;
?>

</body>
</html>
Output:

Code: Select all

Hello World21012
As you can see in the above example, variables can be used several times, and if the variables are integers then you can use maths operations on them. You must remember that a text variable, also called a string, must be contained inside quotes, but an integer must not. Have a play around with variables and different maths operations on those variables; get yourself familiar with them as you’ll be using them a lot.


Strings
-------------------------------------------------------

A string is just text. Like we have done previously you can use the echo function to output a string. If you are making a variable containing a string, or echoing a string, then you need to surround the string using quotes; but if you are echoing a variable that contains a string then you don’t need the quotes.

That may be a bit tricky to get your around at first so ill give you an example to show what I’m talking about.

Source:

Code: Select all

<?php
//here we will define a variable containing a string, notice the quotes around the string
$letter = “abc <br />”;
//here we will echo abc and make a new line, notice the quotes around the string
echo “abc <br />”;
//here we will echo the variable we defined earlier, which is the same as what we just echoed, but notice the absence of quotes.
echo $letter;
?>
Output:

Code: Select all

abc
abc
You will also have noticed that i included some HTML in here, since html is just text interpreted by the browser we can include it in strings and it will be displayed by the browser as HTML would. You will also notice that, like many other programming languages you can add comments by putting a double forward slash and then putting the comment after that. You can also


Comments
-------------------------------------------------------

As you may or may not have noticed in the last example I showed you, i have added comments to tell you what I am doing. If you have done programming in other languages then you will already be used to the idea of commenting. But in case your not, you can put a double forward slash at the beginning of a line, and then add the comment on the line following that; anything put after 2 forward slashes in a line will be ignored by the browser, so someone reading the code will see the comments but wont see them if looking at the output of the page. Alternatively you can enclose a comment with a /* at the start, and a */ at the end. Using this type of comment allows you to have a comment spreading over several lines instead of just the one.

Source:

Code: Select all

<?php
//this is a comment
echo "here is some code <br />;
/* this
is a
multiple line comment, which
can go on
for several lines */
echo "here is some more code <br />";
?>
Output:

Code: Select all

here is some code
here is some more code

Quotes
-------------------------------------------------------

You need to be careful when using quotes in your code, if you are using quotes then what you want to contain inside them will end when there is another quote of the same type you used to start with. This means that if you want to have a quote inside a quote you have to use an apostrophe instead, otherwise it will end the string where you start the embedded string, and start it again where you end it. Thankfully you are able to use an apostrophe for strings as well as a quote mark, " and ' so you are able to have quotes embedded in quotes.

Source:

Code: Select all

<?php
echo "<font color='red'>red text</font>";
?>
Output:

Code: Select all

red text
This generally isn’t a problem but if you are including html, or some other language, inside your php strings then this may become a problem.

You don’t have to use an apostrophe instead of another quote mark; you are able to escape the quotes instead. This basically means putting a backslash in front of it. What this does is tell the PHP server that the quote is to be included in the string and that it doesn’t mark the end of the string.

Source:

Code: Select all

<?php
//we can do it like before
echo "<font color='red'>red text</font> <br />";
//or we can escape the quote marks instead
echo "<font color=\"red\">red text</font>";
?>
Output:

Code: Select all

red text
red text
For someone new to this it may be a bit confusing to begin with, an you may want to just stick to one type of embedding quotes inside quotes, but you will over time become accustomed to the different ways of embedding quotes.

Phantom
Posts: 16
Joined: Thu Nov 08, 2007 8:17 pm

Re: PHP Tutorial - Part 1

Post by Phantom » Tue Nov 27, 2007 9:04 pm

what, no comments?

User avatar
phiber
Site Admin
Posts: 260
Joined: Sun Oct 13, 2002 11:26 pm
Location: Cakovec, Croatia

Re: PHP Tutorial - Part 1

Post by phiber » Tue Nov 27, 2007 9:05 pm

wait for it to be read.
I'll post it as global.

jd2kuk
Posts: 1537
Joined: Fri Mar 16, 2007 12:19 am
Location: UK

Re: PHP Tutorial - Part 1

Post by jd2kuk » Tue Nov 27, 2007 11:04 pm

Nice! It's clear and well laid out, and easy to follow if you're a beginner :D

Now get writing part two.

zeus_zf
Posts: 7
Joined: Fri Nov 02, 2007 5:14 pm

Re: PHP Tutorial - Part 1

Post by zeus_zf » Tue Jan 08, 2008 3:59 am

This tutorial rocks!!!!

Post Reply