Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
Tutorial : Basics of PHP
MindTrix
Join Date: Apr 2002
Posts: 1,833

Emcee and Nerd

United Kingdom
by MindTrix MindTrix is offline 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.

Last edited by MindTrix; 04 Jan 2004 at 10:10.. Reason: missed a space :)
Views: 7664
Reply With Quote
Comments
  #2  
Old 18 Dec 2003, 20:46
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Real name: Dean Clatworthy
Hehe don't mean to pick up on mistakes but:

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

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

print $number1
Should be:

print $number1;
Also when you talk about assigning strings and the usage of strings you should use sinqle quotes. If you want to include a variable in a string then you use double quotes.

Example:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Other than that good tutorial for the beginners. You might want to credit your source for the information though !

Thanks for sharing.

- Dean
__________________
Dean Clatworthy - Web Developer/Designer
Reply With Quote
  #3  
Old 18 Dec 2003, 20:49
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Thanks for pointing that out Mist i really appreciate it. As i said at the begginin i am learning myself so i was expecting to make some mistakes. I'll edit the first post now, Thanks.
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #4  
Old 19 Dec 2003, 00:46
ULTIMATESSJ's Avatar
ULTIMATESSJ ULTIMATESSJ is offline
 
Join Date: Nov 2001
Real name: Mike
Personally i think what you've wrote is an excellent tutorial, it was easy to read through, and everything i read was picked up. I can't wait for the next installment, if you decide to make more that is
Reply With Quote
  #5  
Old 19 Dec 2003, 00:50
filburt1 filburt1 is offline
 
Join Date: Feb 2002
PHP variable names cannot start with a number, like all other programming languages.
Reply With Quote
  #6  
Old 19 Dec 2003, 08:12
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Doh! Cant believe i made an error that stupid Thanks Filburt ill edit it now.
Thanks for the comments Ultimatessj i really appreciate it More comments like that and i guess ill have no choice but to attempt another one

Oh yeah, Vegita is the best one
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #7  
Old 21 Dec 2003, 03:05
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Real name: Lucius Hunk
instead of this, say, do you all want to have a full tutorial of PHP ? well, I can set up a site and then you can learn by watching its movies, from basic PHP to "How to make your own message board? " . That's really an easy way to learn PHP, beside I can provide a Mysql tour too vote it if you are interested in :P
Reply With Quote
  #8  
Old 21 Dec 2003, 09:55
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Way to hijack someone elses thread.
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #9  
Old 21 Dec 2003, 14:33
ULTIMATESSJ's Avatar
ULTIMATESSJ ULTIMATESSJ is offline
 
Join Date: Nov 2001
Real name: Mike
Originally Posted by deathemperor1st
instead of this, say, do you all want to have a full tutorial of PHP ? well, I can set up a site and then you can learn by watching its movies, from basic PHP to "How to make your own message board? " . That's really an easy way to learn PHP, beside I can provide a Mysql tour too vote it if you are interested in :P
Well sure, anyone could do that, but how would people learn from it? how would anyone know to use the information from that tutorial for any other usage? all that would come from it would be identical message board scripts.
Reply With Quote
  #10  
Old 21 Dec 2003, 14:48
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Well said mate, but you could of commented on my tutorial aswell lmao
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #11  
Old 22 Dec 2003, 00:00
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Real name: Lucius Hunk
Originally Posted by MindTrix
Way to hijack someone elses thread.
you make me feel like I'm a robber, that's not my purpose, but if you said so. I'm gone.
Reply With Quote
  #12  
Old 22 Dec 2003, 00:18
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Well no offence but that was the nicest response i could think of You have too see where im comming from when i spend alot of time writting this then you post saying you basically make a better one for people
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #13  
Old 22 Dec 2003, 00:40
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Real name: Lucius Hunk
I don't think so, you know, my tour could make people understand somethings very basic, but you are on your way to guide people to make a hack themself, am I wrong ? I love that thing. so mine is about to make your way easier, more simple and surely quicker
never mind if you wanna do it yourself
Reply With Quote
  #14  
Old 22 Dec 2003, 00:44
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Well look, its nearly 2am here so im ratty, and if you look at all of my posts, im not a nasty guy So sorry if it sounds like i am beeing one.

If you would like to make your own tutorial then go ahead or if you want to post it go ahead

As long as its in a new thread not in this one of course lol
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #15  
Old 22 Dec 2003, 01:00
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Real name: Lucius Hunk
I'm a lazy joker, you should've known that when I prefered text to movies
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 03:27.

Layout Options | Width: Wide Color: