Off-Topic > PHP

Php: Lesson 2

(1/5) > >>

Alex:
Ok, now time to introduce something more!
Variables and types: these will become your basic for EVERYTHING. And not only for php: this is a general programming knowledge.

So, let's start with our first new friend: variables.
What are they? Physically, they are a "cell" in which we can save values.
In php they are represented by a dollar sign followed by the name of the variable.
What to keep in mind:
1) The variable name is case-sensitive.
2) The variable name cannot start with a number
3) valid character for a variable name are, mainly: letters, number, _ and -
So, for example:
$var
$my_name
$name_1
$name_2
$TESTtest9998
All of these are valid variable names.
Now, how to use them?
A simple simple example: consider this little code:

print "Hello world!";

Another way to do the same thing is this:

$text="Hello world!";
print $text;

Easy to understand what's happening: in the first line, we're "saving" the "Hello world!" string into $text. With the second one, we've simply printed i out.
Consider that string may be concatenated in php using the dot.
So we can correctly write the following codes: everyone will print out "My sentence is: Hello world!":

$text="Hello world!";
print "My sentence is: ".$text;

$text="My sentence is: "."Hello world!";
print $text;

$text="Hello world!";
$text2="My sentence is: ".$text;
print $text2;

$text="Hello world!";
$text="My sentence is: ".$text;
print $text;

As you can see in th last example, variables may "grow" using they're old value too.


Now that we know what the hell a variable is, time to introduce the different types you can have.
In our examples, $text was a string. Everything which is defined into qutes is a string.
Now consider these variables:
$var1="Hello";
$var2=10;
$var3=10.5;
$var4=TRUE
They are different.
$var1 is a string: after the = sigh we have somthing enclosed within quotes.
$var2 is an INTEGER. Simply, it is a number. From now over, we'll call it INT
$var3 is a FLOAING POINT NUMBER: again a number, but accept also non INT values. From now over, we'll call it FLOAT
$var4 is a BOOLEAN.

Uhm, maybe better to say more on booleans?
It is the easiest type. A boolean expresses a truth value. It can be either TRUE or FALSE. It is useful to check conditions.
But we'll see more about this when we'll talk about Control Structures.
So now, back to our new INT and FLOAT.
Again, a simple example will clear it all!

$name="Alex";
$age=26;
$actual_year=2003;
$birth_year=$actual_year-$age;
print "My name is ".$name." and i am ".$age." years old. So i was born in ".$birthyear;

result will be:

My name is Alex and i am 26 years old. So i was born in 1976

Simply, no? What can be done with INT and FLOAD variables is exactly what you could expect: arithmetic!!!
You can sum, divide, subtract and so on. Also there are ways to find square roots, exponetial and whatever else in math you can do. We will se this better in the future.
For example, consider the easy operation needed to find a triangle area: (base x height) : 2
Note that in php the multiplication symbol is *, the division one is /.
So here is our simple script:

$base=42.33;
$height=12.3;
$area=($base*$height)/2;
print "The triangle area is ".$area;

Not that hard, no?
You should have noticed the use of the round brackets: yes, you can use them, but ONLY THEM. [] and {} are not for us, right now.
So it's ok to write:

$var=((12/3)*((13+2)-(2+1.42)));


Ok,now sop, too much writing for me!!!
Again, if there are question or something which is not clear, ask.

And, if you can, try to realize some simple script using what you know right now: to try is the best way to learn and understand!

GaryCXJk:
For the people who think this is too easy, you can also use variables in the url. Example, when I wanted to create this reply, I got this url:

 charas-project.net/forum/postreply.php?forumid=9&catid=5&threadid=1095

You can apply this on your own scripts. Just create a file called test.php or something, enter in the url of your script, and add the variable you defined.

example:

http://www.yourdomain.com/test.php?testvariable=19

In the script, there could be something like:

print $testvariable;

So when you will open the url as in the example, you will see:

19

This can also be usefull with other things.

Alex:
Yeah, Gary is right.
Anyhow we will discuss this better when we'll talk about form elements and querystrings.

Oh, and just a fast note: i will not proceed till i'm not sure this part is clear for everyone. So, if you want me to continue (next is: Control structures and Arrays!), you've to tell me about this! ;)

SereneRose:
I'm trying to understand this fully before i say i got it down.  But i think i am getting and have it written down now in my notebook for further examination.  But i think we can go on ^_^ *Finds this Very useful* But one question, for the variables in the links what exactly are they there for??

Alex:
Well, that is a "little extra".
As i told, we will discuss them better when we'll talk about form elements and querystrings.

Fast introduction is: you can pass variables to your script writing them in the url.

This is Gary's example:
http://www.yourdomain.com/test.php?testvariable=19
And then
print $testvariable;

The same result, calling simply your script:
http://www.yourdomain.com/test.php
And then
$testvariable=19;
print $testvariable;

Again, don't try to go too far. Better to proceed a bit slowly but really understand things.

Navigation

[0] Message Index

[#] Next page

Go to full version