Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Tutorial : Basics of PHP
MindTrix's Avatar
MindTrix
Join Date: Apr 2002
Posts: 1,833

Emcee and Nerd

United Kingdom
Show Printable Version Email this Page Subscription
MindTrix MindTrix is offline 12-17-2003, 10:00 PM

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.
Reply With Quote
  #12  
Old 12-21-2003, 11:00 PM
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Location: HOL
Posts: 1,270
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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
  #13  
Old 12-21-2003, 11:18 PM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #14  
Old 12-21-2003, 11:40 PM
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Location: HOL
Posts: 1,270
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
  #15  
Old 12-21-2003, 11:44 PM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #16  
Old 12-22-2003, 12:00 AM
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Location: HOL
Posts: 1,270
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm a lazy joker, you should've known that when I prefered text to movies
Reply With Quote
  #17  
Old 12-23-2003, 05:05 PM
himerus himerus is offline
 
Join Date: Nov 2003
Location: Denver, CO
Posts: 75
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I think it was a great tutorial, and if you are a beginner, you have a better understanding than I did when I was learning.

Nice Work MindTrix

Not everyone can understand in depth, complicated stuff, and I thought your tut was easy to read and comprehend, and should be for any level of PHP programmer.
Reply With Quote
  #18  
Old 12-23-2003, 05:52 PM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Wow thanks for the comments mate Much appreciated!
Reply With Quote
  #19  
Old 12-23-2003, 07:59 PM
Westech Westech is offline
 
Join Date: Jul 2003
Posts: 2
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Good job! I like this tutorial and hope to see more like it.

I may have found an error: I don't know much about php at all, so I'm basing this on my knowledge of other languages. In your concatination example would "vBulletin"."Rules" show up as "vBulletin Rules" or "vBulletinRules"?
Reply With Quote
  #20  
Old 12-23-2003, 08:09 PM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Nope it would show as "vBulletinRules"
Reply With Quote
  #21  
Old 12-27-2003, 04:36 AM
Buczilla Buczilla is offline
 
Join Date: Jun 2003
Location: St. Petersburg, FL
Posts: 138
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Seems helpful

Thanks!
Reply With Quote
Reply


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

Forum Jump


All times are GMT. The time now is 12:02 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04695 seconds
  • Memory Usage 2,321KB
  • Queries Executed 25 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (3)pagenav_pagelink
  • (11)post_thanks_box
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete