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

Well i aint joking i wrote loads I guess i should start it up again, might do in a little bit after a beer
Reply With Quote
  #33  
Old 01-04-2004, 07:03 AM
gldtn gldtn is offline
 
Join Date: Apr 2003
Location: US
Posts: 169
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Exero
PHP Code:
<?php
if($action == "") {
// Main page coding here
echo("Welcome");
}
elseif(
$action == "news") {
// News coding here
echo("News");
}
elseif(
$action == "about") {
// About coding here
echo("About");
}
elseif(
$action == "search") {
// Search coding here
echo("Search");
}
?>
I think Exero was just trying to show us how to write a sophisticated if statements, which in my opinion could be a great topic you can write about MindTrix... By the way great tutorial, although I found ONE mistake;

You wrote;
Code:
Example

"vBulletin"."Rules"


Would show up as


"vBulletin Rules"
Should'nt it be;
Code:
"vBulletin"."Rules"


Would show up as


"vBulletinRules"
Also how would this show on a browser;
Code:
Here is an example


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


Notice the ( . ) symbol connecting everything together.
Gread job, keep it up
Reply With Quote
  #34  
Old 01-04-2004, 09:09 AM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you for the comments, i am half way doing my next tutorial as it is.

You are correct on what you said, however i am sure i put a space before the " so it looked like

"vbulletin "."rules"

Maybe it got auto corrected, however i will change the first post now as i did not notice my error. Thanks!
Reply With Quote
  #35  
Old 01-04-2004, 09:14 AM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$userage 50;
print 
"The users age in days is ".($userage*365)." days"
Would show as

The users age in days is 18250 days
Reply With Quote
  #36  
Old 01-04-2004, 12:40 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Exero - this is a lot clearer:

PHP Code:
if($_REQUEST['action'] == 'blah')
{
dothis();
}
else if(
$_REQUEST['action'] == 'moo')
{
dothat();
}
else
{
donothing();

Reply With Quote
  #37  
Old 01-04-2004, 12:46 PM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

True Mist but both have nothing to do with this tutorial
Reply With Quote
  #38  
Old 01-06-2004, 01:57 AM
N_Jay N_Jay is offline
 
Join Date: Dec 2003
Posts: 13
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I am looking for a good book on PHP programming.
I am a non-programmer, so I need something I can flip through as I need to to "adjust", fix, or just understand some vBulletin hacks and other PHP files.

Anyone have a recommendation?

And Thanks MindTrix for the tutorial, every littel bit helps.
Reply With Quote
  #39  
Old 01-06-2004, 04:22 AM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I personally use a two books now.
One called SAMS Teach yourself - PHP, MYSQL and Apache.

And then

SAMS Teach yourself PHP

They aint cheap though, Around $30 or £20
Reply With Quote
  #40  
Old 01-06-2004, 10:06 AM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Worth every penny though ^^
Reply With Quote
  #41  
Old 01-06-2004, 04:10 PM
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Location: United Kingdom
Posts: 1,833
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

True they are definetly worth it. The book with all 3 in it, i am borrowing from the library and even comes with a cd complete with all 3, and a step by step guide to installing them onto your pc for a local server.

The other one just delves deeper into PHP, its hard too take in sometimes but the more you read it, the more you understand it.

At the moment i am onto Arrays
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 11:03 AM.


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.04787 seconds
  • Memory Usage 2,336KB
  • 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
  • (3)bbcode_code
  • (3)bbcode_php
  • (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
  • (4)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