Old 02-05-2007   #1 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default Chaos' PHP Tutorial - Part 2: Variables.

Chaos' PHP Tutorial - Part 2: Variables

Update Log
[+] 3/5/07 - Added a section on operators to the tutorial.


Welcome to the second installment of PHP tutorials. In this tutorial we will be focusing on variables and operators -- two crucial element to any well-written PHP script.

Operators
Operators are used in most programming languages to manipulate or perform operations on values and variables; this is true in the case of PHP also. There are 5 different types of operators:
* Assignment Operators
* Arithmetic Operators
* Comparison Operators
* String Operators
* Combination Arithmetic & Assignment Operators

I will explain each of these types and give examples of them.

Assignment Operators
These are used to set a variable equal to a value or to set a variable to use another variable's value. You use the equal sign (=) to do so. A good example of this is as follows:
PHP Code:
$var1 10;
$var2 $var1
This firstly sets the variable of var1 to equal 10. It then assigns that variable to the variable of var2 -- the result is that both of these variables now equal 10.
• The assignment operator is used to assign values to something. The values can be integers or text.

Arithmetic Operators
These operators are used for arithmetic or geometric operations. The operators are:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)

Here's an example of what it would look like in PHP:
PHP Code:
$add 4;
$take 4;
$times 4;
$divide 4
Comparison Operators
Comparison operators are used to compare, or check the relationship between different variables or values. Comparison operators are used with conditional statements (will be covered next tutorial) and will result in being either true or false.
Here's are the comparison operators:

Code:
    == (Equal To)
    != (Not Equal To)
    < (Less Than)
    > (Greater Than)
    <= (Less Than or Equal To)
    >= (Greater Than or Equal To)
An example of these in use would look like:
PHP Code:
$x 5// Assigning two values to two variables; x and y.
$y 10;

$x == $y (5 equals to 10False)
$x != $y (5 is not equal to 10True)
$x $y (5 is less than 10True)
$x $y (5 is greater than 10False)
$x <= $y (5 is less than or equal to 10True)
$x >= $y (5 is greater than or equal to 10False
These operators are used very commonly in conditional statements which will be covered shortly. It is important that you learn and remember what each one does.

The assignment operator of = is different to the comparison operator of ==, it is important that you understand the difference between these two operators.

At present there is no need to go through the other two types of operators; however, if you would like me to explain them to you please PM me.

Variables
What exactly is a variable? Firstly, variables are used to store information; they act as a basket to put your eggs in, if you will. For example, if you had a string of text such as:
"I really like nuk3's shoes!" you have many options of what to do with it. You could echo it out to the browser:
PHP Code:
<?PHP

echo "I really like nuk3's shoes!";

?>
or you could save it for later -- by assigning it to a variable. Variables are also used for storing information and are very dynamic. You could "assign" that string of text to any variable like so:
PHP Code:
<?PHP

$text 
"I really like nuk3's shoes!";

?>
Let me explain what has happened here. Firstly we've told the server it's a PHP script (using the delimiters.) Then, we've written $text. This is called defining a variable. Whenever you define a variable you must include a dollarsign ($)
before it to indicate it is indeed a variable. You then state the name of the variable, which in this case is text. What comes next is the equals sign; this tells the server that you will be assigning a value to the variable you just defined.
After that comes the value or string of text we wan't to assign to the variable of $text. You must include the quotation marks for any text string, but in the case of an integer you don't need to include the quotation marks.

PHP Code:
$text "This is text, you need to include quotation marks!";

$integer 1// This is an integer, you don't need to include quotation marks 
Variables have some naming rules, they are as follows:
* A variable name must start with a letter or an underscore "_"
* A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
* A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

Before we go any further, I need to point out that whenever you see double back-slashes (//), a back-slash followed by an asterisk (/*) or a hash sign (#) these indicate comments. Comments are used for just that -- to make comments. They will not be parsed by the server as text and remain untouched.

Back on track. Variables are extremely useful in many applications. Using variables on user-submitted forms (like posting on the forums) are essential! As I said before, variables are mainly used for storing information for later use. Here's an example of this:

PHP Code:
<?PHP

$message 
"nuk3, you have lovely shoes.";

// You could do any number of things here, such as echo some texts to the user or what-not.

echo $message;

?>
Let's have a look at that code. We start with the delimiters (as usual) and we then define a variable called message. We assign the string of text "nuk3, you have lovely shoes." to it. After that, any number of things could be put in place. A little later in the code, we may want what we assigned to the variable.
This is where we used echo (that handy command I explained in the last tutorial) and instead of giving it a string to print out, we gave it the variable we defined earlier!

The result of this would be the same as telling the server to echo "nuk3, you have lovely shoes." This possesses many benefits which will be explored in later tutorials.

You can also add variables together and assign them to another variable. You use the period (.) sign to do so.

PHP Code:
<?PHP

$"Hello";
$
" "// This is a space.
$"GotGames";

$text = $1.$2.$3;

echo 
$text;

?>
This would give you the output of: Hello GotGames.


Variables Key Summary
• Variables can store and update information, they can also be added together.
• You define a variable by giving a dollarsign ($) followed by it's name followed by an equals sign (=) and lastly a value, which can be numerical, string or array.
• You can call a variable at any point in a script, as long as you have defined it somewhere.
• Variables are extremely useful, very versatile and really yummy. Learn how to use them and you will save yourself a lot of mess -- especially when dealing with a lot of data.

Well, that's about it on variables. The next tutorial will be on the various conditional statements and how to use them. Stay tuned!

--Chaos.

Last edited by chX; 03-05-2007 at 10:35 PM..
chX is offline   Reply With Quote
Old 02-05-2007   #2 (permalink)
Monster Member
 
shirasE_'s Avatar
 
Join Date: Jan 2007
Location: Melbourne, Australia.
Age: 18
Posts: 3,555
Rep Power: 5
Send a message via ICQ to shirasE_ Send a message via AIM to shirasE_ Send a message via MSN to shirasE_ Send a message via Yahoo to shirasE_ Send a message via Skype™ to shirasE_
Default

Very nice, I linked my friend as he is a big cake and wants to learn too and he found it easy to understand and informative. Well done.
__________________
AMD 5000+BE@3.21GHZ//2GB OCZREAPER//ATI HD3870@850/1301
ANTEC 900//RAZER TARANTULA//TT TAI-CHI M2//SS 5HV2 XFI
#GotGames #MelbCSS #s2.au #Reality.Gaming
#TRAiNWRECK melb.lan.net.invite - kto_-[1b]
shirasE_ is offline   Reply With Quote
Old 02-05-2007   #3 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

Quote:
Originally Posted by shirasE_ View Post
Very nice, I linked my friend as he is a big cake and wants to learn too and he found it easy to understand and informative. Well done.
Awesome. My main aim for writing these is to make them easy to understand so anyone with half a brain can look at it and learn something. A lot of tutorials just throw people in the deep end and they often get confused, not knowing what anything does or why something was used in the way it was.
chX is offline   Reply With Quote
Old 02-05-2007   #4 (permalink)
Monster Member
 
shirasE_'s Avatar
 
Join Date: Jan 2007
Location: Melbourne, Australia.
Age: 18
Posts: 3,555
Rep Power: 5
Send a message via ICQ to shirasE_ Send a message via AIM to shirasE_ Send a message via MSN to shirasE_ Send a message via Yahoo to shirasE_ Send a message via Skype™ to shirasE_
Default

Quote:
Originally Posted by ChaosX View Post
Awesome. My main aim for writing these is to make them easy to understand so anyone with half a brain can look at it and learn something. A lot of tutorials just throw people in the deep end and they often get confused, not knowing what anything does or why something was used in the way it was.
Yeah its great starting from the beginning like this and explaining it fully. Other tutorials that he has tryed kind of expect him to know a certain amount. Even though its a 'basic' one.
__________________
AMD 5000+BE@3.21GHZ//2GB OCZREAPER//ATI HD3870@850/1301
ANTEC 900//RAZER TARANTULA//TT TAI-CHI M2//SS 5HV2 XFI
#GotGames #MelbCSS #s2.au #Reality.Gaming
#TRAiNWRECK melb.lan.net.invite - kto_-[1b]
shirasE_ is offline   Reply With Quote
Old 02-05-2007   #5 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

Quote:
Originally Posted by shirasE_ View Post
Yeah its great starting from the beginning like this and explaining it fully. Other tutorials that he has tryed kind of expect him to know a certain amount. Even though its a 'basic' one.
I know how it feels when you first start learning. I find that using analogies helps people understand better and get their heads around it all.

The goal is with each tutorial the knowledge and difficulty will increase little by little, so hopefully people reading it will increase their understanding with it.

Remember to PM me if you have questions.

Last edited by chX; 03-05-2007 at 03:54 PM..
chX is offline   Reply With Quote
Old 02-05-2007   #6 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default

does anyone use classes for php?

Interested to know how you apply them.
billy baxter is offline   Reply With Quote
Old 02-05-2007   #7 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

Quote:
Originally Posted by billy baxter View Post
does anyone use classes for php?

Interested to know how you apply them.
Yeah I use classes a lot. I'll be covering all that once I get to Object-Oriented Programming a little later on in the tutorials.
chX is offline   Reply With Quote
Old 02-05-2007   #8 (permalink)
Honorary Member
 
bokCHOI's Avatar
 
Join Date: Jun 2006
Location: |====|¯¯¯¯¯¯¯/ Penis Size : 10 "s ¯¯¯¯¯¯
Posts: 1,372
Rep Power: 4
Default

Mmm my friend is a PHP god... made a browser mmorpg using php in year 9 =\

lol he made youfail.org
__________________

»-(¯`v´¯)-»"I'm so gay I sweat glitter!"»-(¯`v´¯)-»
.ǝɹʇɐǝɥʇ ǝıʌoɯ ʇǝ1oɹʌǝɥɔ ɐ ǝʌıɹp ı ɹoʇɐbı11ɐ ǝ1ıpoɔoɹɔ ɹoıɹǝʇuı
bokCHOI is offline   Reply With Quote
Old 02-05-2007   #9 (permalink)
Honorary Member
 
doublethink's Avatar
 
Join Date: Jun 2006
Location: Sydney NSW
Posts: 1,784
Rep Power: 4
Default

Quote:
Originally Posted by shirasE_ View Post
Yeah its great starting from the beginning like this and explaining it fully. Other tutorials that he has tryed kind of expect him to know a certain amount. Even though its a 'basic' one.
http://au.php.net/tut.php

If you can't understand this, you probably shouldn't be programming. But then again when I first started (using Eiffel!) I had no farken idea what was going on

</old man sigh>
doublethink is offline   Reply With Quote
Old 02-05-2007   #10 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Quote:
Originally Posted by billy baxter View Post
does anyone use classes for php?

Interested to know how you apply them.
As with any Single Inheritance language. NOT STUPIDLY. OO Design can be done horribly, and it commonly is.

Ps. a good online PHP book is here:

http://hudzilla.org/phpwiki/index.php?title=Main_Page
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature.
Khaless is offline   Reply With Quote
Old 02-05-2007   #11 (permalink)
Monster Member
 
Faded.'s Avatar
 
Join Date: Mar 2007
Location: Brisbayneeee
Posts: 4,283
Rep Power: 6
Default

I am so confused =/ oh well, keep up the good work chaos
__________________
yeah sup
Faded. is offline   Reply With Quote
Old 02-05-2007   #12 (permalink)
Monster Member
 
shirasE_'s Avatar
 
Join Date: Jan 2007
Location: Melbourne, Australia.
Age: 18
Posts: 3,555
Rep Power: 5
Send a message via ICQ to shirasE_ Send a message via AIM to shirasE_ Send a message via MSN to shirasE_ Send a message via Yahoo to shirasE_ Send a message via Skype™ to shirasE_
Default

Quote:
Originally Posted by doublethink View Post
http://au.php.net/tut.php

If you can't understand this, you probably shouldn't be programming. But then again when I first started (using Eiffel!) I had no farken idea what was going on

</old man sigh>
As I said above it isn't for me. Yes I can understand that but to be honest it is formatted poorly compared to the way ChaosX is doing it. Thats just my opinion.
__________________
AMD 5000+BE@3.21GHZ//2GB OCZREAPER//ATI HD3870@850/1301
ANTEC 900//RAZER TARANTULA//TT TAI-CHI M2//SS 5HV2 XFI
#GotGames #MelbCSS #s2.au #Reality.Gaming
#TRAiNWRECK melb.lan.net.invite - kto_-[1b]
shirasE_ is offline   Reply With Quote
Old 02-05-2007   #13 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

Quote:
Originally Posted by Faded. View Post
I am so confused =/ oh well, keep up the good work chaos
Haha. It does take a little while to understand what it's all about, though. Thankyou.
chX is offline   Reply With Quote
Old 03-05-2007   #14 (permalink)
Participating Member
 
blindside's Avatar
 
Join Date: Jun 2006
Posts: 231
Rep Power: 3
Default

I think you should whack in a little note about some of the shorthand assignment operators here (.= , +=, -=, *=, etc), maybe even the arithmetic operators (++, --) Variables seems like a good place to put that info (unless you intend on talking about primitive types individually later).
blindside is offline   Reply With Quote
Old 03-05-2007   #15 (permalink)
Participating Member
 
blindside's Avatar
 
Join Date: Jun 2006
Posts: 231
Rep Power: 3
Default

Quote:
Originally Posted by billy baxter View Post
does anyone use classes for php?

Interested to know how you apply them.
Just saw this, and yeah, I've done a bit of programming in Java (classes are entirely inherent), and you use them for almost everything. In PHP, however, I must admit I don't find them to be terribly useful. Probably because I don't work on huge projects, but I've gotta say skills' website framework is a rather fantastic (yet simple) implementation. Don't wanna spill the beans on his creation, but in essence he's written navigation, template, interface, peripheral and page classes which run the entire site. Once it's implemented, writing new pages is a walk