Back to vBulletin 3 Articles

Tutorial : Basics of PHP
by MindTrix 18 Dec 2003

Basics of PHP



Why the tutorial?

I myself am in the process of learning PHP, and as a learner, realise how hard it is for some people to understand PHP or too get hold of a book that they may learn from, So this is my attempt at a Tutorial into PHP telling people information as i learn it myself in hope it helps other people.

Common PHP Terms and Meanings.

Lets start things off with some common words/terms used in PHP and attempt to explain them in depth.

Start and End commands of PHP

In a PHP document there are start and end tags.

Here is a list of these tags.

--------------------------------------------------------------------------
Tag Style --------------- Start Tag ------------------ End Tag ----------
--------------------------------------------------------------------------
Standard Tags--------------<?php------------------------ ?> -------------
Short Tags ------------------ <? ------------------------ ?> --------------
ASP Tags ------------------- <% ----------------------- %> -------------
Script Tags ---------- <SCRIPT LANGUAGE="php"> ------- </SCRIPT> ------

(Side Note)
The Standard Tags will definetly work on any configuration, so basically will work for any body using PHP.
The Short Tags and ASP Tags however, must be enabled in the php.ini file.
The best bet is to always use the Standard Tags.

Variable

A variable is a special piece of code you can assign a value too. In simple terms, we can assign lengthy pieces of code too one variable, saving the hassle of re-writting the code everytime.

A variable consists of a dollar sign ($) followed by the name of your choice. Here is an example of a variable

$username


--------------------------------------------------------------------------
l Variable Rules
l
l 1. Variable names MUST start with a dollar sign ($)
l 2. Variable names can include letters, numbers, and the underscore
l character (_)
l 3. They CANNOT include spaces
l 4. They MUST begin with a letter or an underscore
l
l
l Here are some examples of acceptable variable names.
l
l $aaa;
l $a_very_long_name;
l $boringZZZ;
--------------------------------------------------------------------------

When creating a variable it is recommended to give it a meaningful name.

Example if you was too make a variable containing somebodys name, simply calling the variable $n does not give alot of information regarding the data it holds. A more usefull name would be $name_john for example.

At the end of every variable assignment is a icon known as a semicolon ( ; ) and in PHP terms is known as the instruction terminator.

Now we have seen examples of variables, next is assigning values to these variables.

Step one: Naming a variable

Decide upon a variable name, for now we shall use $number1

Step two: Adding a value to the variable

To add a value to this variable name we must use the equals sign ( = ) so the variable will now look like this, $number1 =

Step three: Assigning the value

Now we must assign a value to the variable, in this case we will assign the value of 35. The variable now looks like this, $number1 = 35;

(Side note)
When assigning a value to a variable the semicolon goes after the value, HOWEVER, When printing/displaying a variable the semicolon goes after the variable's name.

Step four: Printing the variable

To display the contents of a variable we must use an inbuilt command that PHP has called print .

So to display the contents of the variable we would type this into a php file

print $number1;

which when viewed through a webpage would show up as

35

Because the value assigned to $number1 was 35 (See it all makes sense now )

Data Types

Variables can hold different Date Types. There are six standard Data Types in PHP

--------------------------------------------------------------------------
Type --------------------- Example ------------------- Description -----
--------------------------------------------------------------------------
Integer -----------------------5--------------------- A whole number -----
Double ------------------- 5.876594 ---------------- A decimal number ----
String ------------------- "Hey there" ------------ Collection of characters -
Boolean ------------------- false --------- One of two values true or false -
Object ------------------------------------------- An instance of a class --
Array ---------------------------------- An ordered set of keys and values -

(Side Note)
When adding a STRING to a variable you should use single quotes but if you would like to add a Variable to a string then use quotation marks ( " " )

Along with these six standard Data Types there are also two special types shown here.

--------------------------------------------------------------------------
Type -------------------- Description -----------------------------------
--------------------------------------------------------------------------
Resource ---------- A third party resource, a Database for example --------
NULL -------------- An uninitialized variable (Nothing assigned to them) -----



Operators and Expressions

Operators are symbols that when put inbetween two Operands, produce a new value.

Heres an example

5 + 9

The 5 and 9 are Operands, and the + sign is the Operator.

Usually an Operator sits inbetween an Operand on each side, although there are some exceptions which we will look at another time .

When we put an Operand with an Operator to produce a new result is called an Expression.

Now lets look at some Operators closer.

The Assignment Operator

The Assignment Operator is one you have seen already, and will use alot. It is simple the equals sign ( = )

This Operator simply takes it's Right hand Operand and assigns it to the Left hand Operand.

Example

$name = "Liam";

The name Liam (Sexy name isnt it lol) is assigned to the variable $name

So whenever $name is printed, Liam will be shown.

Arithmetic Operators

Arithmetic Operators is simply your boring maths symbols, so back to school we go.

Here is a list of the Arithmetic Operators.

--------------------------------------------------------------------------
Operator --------------- Example ---------------- Example Result -----
--------------------------------------------------------------------------
Addition ( + ) ---------------4+5-------------------------- 9 --------------
Subtraction ( - )----------- 15-5 ------------------------- 10 -------------
Division ( / ) -------------- 20/2 ------------------------- 10 -------------
Multiplication ( * ) --------- 7*9 ------------------------- 63 -------------
Modulus ( % ) ------------- 10%3 ------------------------ 1 --------------

Addition

The Addition Operator adds the right Operand to the left Operand.

Subtraction

The Subtraction Operator subtracts the right Operand from the left Operand.

Division

The Division Operator divides the left Operand by the right Operand.

Multiplication

The Multiplication Operator multiplies the left Operand by the right Operand.

Modulus

The Modulus Operator returns the remainder of the left Operand divided by the right.

The Concatenation Operator

The Concatenation (go on say it i dare you ) Operator is simple the decimal point character ( . )

This treats both Operands as strings and appends the right hand Operator to the left hand.

Example

"vBulletin "."Rules"

Would show up as

"vBulletin Rules"

(Side note)
No matter what Data Type is in the Operands, they will be treated as strings and the result given is always a string.

Here is an example

$userage = 50;
print "The users age in days is ".($userage*365)." days";

Notice the ( . ) symbol connecting everything together.


Well that is all i got for now If this tutorial gets enough replys and proves helpfull i will post some more as i learn them

Thanks go to Mist for pointing out Newbie errors
And thanks to Sams Teach Yourself Php, Mysql and Apache which i am using to learn php with

Note : I have not COPIED the book. Meerly wrote down what i have learnt, as i learn from the book.

vblts.ru supports vBulletin®, 2022-2024