Variable and other stuff

Post all your tuts or request for tuts here.
Prodigy
Posts: 5
Joined: Thu Jul 08, 2010 1:52 pm

Variable and other stuff

Post by Prodigy »

ok.. so i just wanted to know what a varialble is?


i been following the tutorials but there are some of the simple things that you like explain briefly that i dnt get, such as:

mysql the query the array.. i'm writing this stuff down on the context but i have no idea what i am exactly doing...

what do the colors in context mean... like red blue and green...

why do you use { or ( or [ for?...

i mean i'm watching the vids over and over again, you specify what happens when you do this and this is how you write it, but i just don't rreall know why if you see what i mean.

anyway you i think you got how deep in the dark i still am.. reading the tutorials atm but i'd appreciate more help.
Commitment, honor, bravery, loyalty and sacrifice...
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: Variable and other stuff

Post by Rastan »

http://www.w3schools.com/php/default.asp


That site gives an easy to go through set of explanations for a lot of he stuff dealing with php and many other things. I have used it a lot personally to clarify things for myself. You can navigate the different sections by the menus on the left.
Loopy

Re: Variable and other stuff

Post by Loopy »

A variable in php works just like a variable in your math class. It's just a placeholder for information. That information can be just about anything, which is why it's called a variable. It can be a integer (e.g. $x=4), a decimal number (e.g. $pi=3.14), a string of text (e.g. $race='Asian'), or other things, such as an array.

The tutorials for making a game are probably a bit high level for you if you're having trouble getting your head around variables. I'd recommend an absolute beginner class for you to build up to the point where you can understand the tutorials better, otherwise, you're just going to frustrate yourself.

Something like this:
http://devzone.zend.com/node/view/id/625

Regarding your questions:

Querying the database - Whenever you ask the database a question (e.g. selecting the details of a player) and want it to return a result, that's called a query.

Color coding - The color you're seeing on some text is from the text editor. Text editors will generally color-code your work to make it easier to read. For instance, functions might be colored blue, variables green, strings of text red, etc.

Use of {,(, and [ depend on what you are doing, because there are different uses for each. Three common uses:

{ is the open and closing character for functions, as such:

Code: Select all

function calculate_area {
//do something
}
Parens are used for a lot of things, but you see them commonly in functions and loops and they are used when passing arguments/parameters to functions, as such:

Code: Select all

function calculate_area ($length, $width) {
//do something
}

Brackets are commonly used in arrays like such:

Code: Select all

$my_array=array();
for ($i=0; $i<10; $i++){
$value=$i*$i;
$my_array[]=$value;
}
print_r ($my_array);
Prodigy
Posts: 5
Joined: Thu Jul 08, 2010 1:52 pm

Re: Variable and other stuff

Post by Prodigy »

i'm checking it out now thx.

edit: just saw ur post loopy, this was very clear thx a lot.
Commitment, honor, bravery, loyalty and sacrifice...
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Variable and other stuff

Post by hallsofvallhalla »

i dont like the wiki but here is a good definition

http://en.wikipedia.org/wiki/Variable_%28programming%29

basically a variable is a container. Think of a variable like a empty jar. Currently it is empty so it is on the shelf out of the way.

Lets say you want to fill a jar with peanuts. You take it off the shelf and add one peanut

Peanut jar = 1

so lets say we add 5 to it

peanut jar = 6

now lets say you want some cashews
you take another jar and put 10 cashews in it

Peanut jar =6 still
Cashew jar = 10

so we have 2 jars,(2 variables) that are holding different data

as far as the { } go

they are basically do blocks, think of it like a work day lets say you go to work from 1pm and 8pm and the boss needs to assign you work

Starting time
1pm{

}8pm end time

anything he assigns you he puts in those blocks

so if he wanted to assign you to mop the floors

1pm{
mop floors
}8pm end time
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Variable and other stuff

Post by Chris »

If you've ever done algebra at school.. then you should remember having a question like, "If A is 5 times larger than B and B divides 10 times into twice the amount of A, what is the value of A?".

A and B are variables. They both have a value. In this case B is 1 and A is 5.

A variable is say a name for something that can change.. it can vary. In PHP, which I'm assuming you're using. A variable always begins with a '$' infront of it. For example:

Code: Select all

<?php
$A = 5;
$B = 1;
?>
 
We have now set our variables: A and B. you might notice at the end of each line I have placed a ';', this is always required in PHP.

So what if I want to see the value of my variables? In PHP this is done buy "echoing" or "printing" what you want.

Code: Select all

<?php
$A = 5;
$B = 1;
echo $A;
echo $B;
?>
 
This will show "51".

So what is the point of making a variable. Well as I just said a variable has a value that can vary. This means it can change. You can change the value of a variable by simply restating it:

Code: Select all

<?php
$A = 5;
echo $A;
$A = 9;
echo $A;
?>
 
This will show "59".

Okay, very good, we now know we can change variables values and show them. But it is also important to know how a variable works.

Because a computer works in binary.. A string of bits, often referred to as 1's and 0's. It is not always possible for it to do certain things.
For programming in PHP you don't really need to have any knowledge in how the binary works.. However I would suggest you Google it.

While in algebra you generally always work with numbers.. In programming you don't.. therefore there are different ways for storing information in variables. I just showed you an example with numbers.. These numbers are automatically detected by PHP to be Integers(INT), a whole number.

There are then different types of variables.. The most important are integers and strings in PHP. A string is a piece of text. and must always start with quotations marks:

Code: Select all

<?php
$A = "This is a string";
echo $A;
?>
will show: This is a string.

Now that you have a slight insight to how things work.. there is a tutorial I wrote here that should help you get a better gist of things. Once you understand this you can look into using arrays and classes.

http://indie-resource.com/forums/viewto ... =26&t=1069

If you are still haveing a hard time understanding, I would highly recommend buying a PHP book.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Prodigy
Posts: 5
Joined: Thu Jul 08, 2010 1:52 pm

Re: Variable and other stuff

Post by Prodigy »

thx for this chris, and yea i have already read this thank you. Would you say i need to write stuff down to remember them?
Commitment, honor, bravery, loyalty and sacrifice...
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Variable and other stuff

Post by Chris »

The best thing you can do is try the codes, the best way of learning is through trial and error.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Variable and other stuff

Post by Jackolantern »

Chris wrote:The best thing you can do is try the codes, the best way of learning is through trial and error.
I definitely second this. You can read tutorials, books and wikipedia entries until you turn blue but until you start coding, make some mistakes and learn to correct them you will have a very hard time remembering how to do things correctly.
The indelible lord of tl;dr
Prodigy
Posts: 5
Joined: Thu Jul 08, 2010 1:52 pm

Re: Variable and other stuff

Post by Prodigy »

well, i went to the site w3schools.com and it turns out i need to learn html/xhtml and javascript b4 i can learn php. I am starting at the root of everything i know, but i'm very eager to learn and i'm hoping to reach a high enough level to create a pro level site / game... more or less... btw how do you know which <!doctype> to use?
Commitment, honor, bravery, loyalty and sacrifice...
Post Reply

Return to “Tutorials”