|
|
#1 (permalink) |
|
GotGames Moderator
|
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:
• 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:
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)
PHP Code:
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 Code:
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:
* 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:
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:
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.. |
|
|
|
|
|
|
|
#2 (permalink) |
|
Monster Member
|
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] |
|
|
|
|
|
#3 (permalink) |
|
GotGames Moderator
|
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.
|
|
|
|
|
|
#4 (permalink) | |
|
Monster Member
|
Quote:
__________________
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] |
|
|
|
|
|
|
#5 (permalink) | |
|
GotGames Moderator
|
Quote:
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.. |
|
|
|
|
|
|
#8 (permalink) |
|
Honorary Member
Join Date: Jun 2006
Location: |====|¯¯¯¯¯¯¯/ Penis Size : 10 "s ¯¯¯¯¯¯
Posts: 1,372
Rep Power: 4
|
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ı |
|
|
|
|
|
#9 (permalink) | |
|
Honorary Member
Join Date: Jun 2006
Location: Sydney NSW
Posts: 1,784
Rep Power: 4
|
Quote:
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> |
|
|
|
|
|
|
#10 (permalink) | |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Quote:
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. |
|
|
|
|
|
|
#12 (permalink) | |
|
Monster Member
|
Quote:
__________________
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] |
|
|
|
|
|
|
#14 (permalink) |
|
Participating Member
Join Date: Jun 2006
Posts: 231
Rep Power: 3
|
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).
|
|
|
|
|
|
#15 (permalink) |
|
Participating Member
Join Date: Jun 2006
Posts: 231
Rep Power: 3
|
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 |








