vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Tutorial : Basics of PHP (https://vborg.vbsupport.ru/showthread.php?t=59260)

MindTrix 12-17-2003 10:00 PM

Tutorial : Basics of PHP
 
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 :p.

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 :p 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.

Dean C 12-18-2003 07:46 PM

Hehe don't mean to pick up on mistakes but:

Quote:

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:

Quote:

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:

Quote:

print $number1
Should be:

Quote:

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:
PHP Code:

$number 1;
echo 
'the number is 1';
echo 
"the number is $one"

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

Thanks for sharing.

- Dean

MindTrix 12-18-2003 07:49 PM

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.

ULTIMATESSJ 12-18-2003 11:46 PM

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

filburt1 12-18-2003 11:50 PM

PHP variable names cannot start with a number, like all other programming languages.

MindTrix 12-19-2003 07:12 AM

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 ;)

deathemperor 12-21-2003 02:05 AM

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

MindTrix 12-21-2003 08:55 AM

Way to hijack someone elses thread.

ULTIMATESSJ 12-21-2003 01:33 PM

Quote:

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.

MindTrix 12-21-2003 01:48 PM

Well said mate, but you could of commented on my tutorial aswell lmao

deathemperor 12-21-2003 11:00 PM

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.

MindTrix 12-21-2003 11:18 PM

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 ;)

deathemperor 12-21-2003 11:40 PM

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

MindTrix 12-21-2003 11:44 PM

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 :p

As long as its in a new thread not in this one of course lol

deathemperor 12-22-2003 12:00 AM

I'm a lazy joker, you should've known that when I prefered text to movies :D

himerus 12-23-2003 05:05 PM

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.

MindTrix 12-23-2003 05:52 PM

Wow thanks for the comments mate :) Much appreciated!

Westech 12-23-2003 07:59 PM

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"?

MindTrix 12-23-2003 08:09 PM

Nope it would show as "vBulletinRules"

Buczilla 12-27-2003 04:36 AM

Seems helpful :)

Thanks!

MindTrix 12-27-2003 09:55 AM

No problem :)

Exero 01-03-2004 12:21 AM

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");
}
?>


MindTrix 01-03-2004 12:24 AM

Why did you post that?

TouchingVirus 01-03-2004 11:39 AM

my thoughts exactly!

MindTrix, great tutorial, it was simple yet effective and should help any beginner in php. Hopefully you shall begin to understand php more and write more tutorials for the vbulletin community :)

MindTrix 01-03-2004 11:48 AM

Thanks for the comments :)

I might think about writting another one about if and else if and all that kind of thing.

TouchingVirus 01-03-2004 12:10 PM

LOL, it will be the one i was hoping you would write...i could do with a few pointers ;)

MindTrix 01-03-2004 12:12 PM

Lol ok well ill see if i can get started on it today sometime.

TouchingVirus 01-03-2004 12:16 PM

Now thats service - and i didnt even have to pay, thanks :)

MindTrix 01-03-2004 12:57 PM

Ok ok i just sat on my RC1 i installed on my server and wrote a guide, so far i think i have been writting for over 40 minutes so i had wrote loads, I click preview post and vB3 had done that stupid time out thing where it decided i was not doing anything for a while so it logged me out :@ Of course i had to log back in and it deleted everything i wrote, so go figure i am now majorly annoyed, and wont write it again either until late tonight when im bored, or tomorrow. Sorry but im maddddddddd!!!!

TouchingVirus 01-03-2004 05:12 PM

URGH! DAmn j00 vbulletin!!!

Im sure it would have made for good reading, get un-mad or really bored..and start writing :)

MindTrix 01-03-2004 05:15 PM

Well i aint joking i wrote loads :( I guess i should start it up again, might do in a little bit after a beer :)

gldtn 01-04-2004 07:03 AM

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

MindTrix 01-04-2004 09:09 AM

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!

MindTrix 01-04-2004 09:14 AM

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

Dean C 01-04-2004 12:40 PM

Exero - this is a lot clearer:

PHP Code:

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



MindTrix 01-04-2004 12:46 PM

True Mist but both have nothing to do with this tutorial :)

N_Jay 01-06-2004 01:57 AM

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.

MindTrix 01-06-2004 04:22 AM

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

Dean C 01-06-2004 10:06 AM

Worth every penny though :) ^^

MindTrix 01-06-2004 04:10 PM

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 :)


All times are GMT. The time now is 04:32 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.01749 seconds
  • Memory Usage 1,876KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (5)bbcode_php_printable
  • (7)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (40)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete