Home
Help
Search
Calendar
Login
Register
Please
login
or
register
.
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News:
Click here to join us on IRC (#charas on irc.freenode.net)!
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
PHP
(Moderator:
De KoFfieŠ
) »
Php: Lesson 2
« previous
next »
Print
Pages: [
1
]
2
Author
Topic: Php: Lesson 2 (Read 30129 times)
Alex
I am the MASTER!
Administrator
Exemplar
Posts: 1,130
Php: Lesson 2
«
on:
September 11, 2003, 11:13:48 AM »
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!
Logged
GaryCXJk
<strong>Official Charas.EX Team Member</strong>
Exemplar
Posts: 1,586
FOUR OH FOUR'D
Extra lessons
«
Reply #1 on:
September 11, 2003, 02:29:49 PM »
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.
Logged
Play it now!
Charas Breakout DX
Area91: for MUGEN and RPG Maker VX Ace stuff
Alex
I am the MASTER!
Administrator
Exemplar
Posts: 1,130
(No subject)
«
Reply #2 on:
September 12, 2003, 11:19:15 AM »
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!
Logged
SereneRose
Member
Initiate
Posts: 39
Nothing to say.
(No subject)
«
Reply #3 on:
September 12, 2003, 04:38:08 PM »
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??
Logged
http://www.everseeingrpg.tk
~Psh ^_^ Pretty new Intro on my page Heh~
Alex
I am the MASTER!
Administrator
Exemplar
Posts: 1,130
(No subject)
«
Reply #4 on:
September 12, 2003, 04:46:20 PM »
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.
Logged
SereneRose
Member
Initiate
Posts: 39
Nothing to say.
(No subject)
«
Reply #5 on:
September 12, 2003, 07:15:21 PM »
Alright I'm still trying to find a server that does php.. Mine doesnt and my PWS is like screwy.. cant understand it half time lol. So i'll just jot everything down and use it later when i figure out where i do this at.
Oh and i got a question.. would this be like a variable thing? Or what. LoL
Logged
http://www.everseeingrpg.tk
~Psh ^_^ Pretty new Intro on my page Heh~
Alex
I am the MASTER!
Administrator
Exemplar
Posts: 1,130
(No subject)
«
Reply #6 on:
September 13, 2003, 09:44:46 AM »
SereneRose:
the code you've written is an html style tag.
Yes, in php you can enclose it in a variable, but there is no php at all in your code
What can be done, for example, is:
$meta_var="
";
$style_var="";
print $meta_var;
print $style_var;
?>
Kijuki_Magazaki:
Uhm... may you explain what is not clear?
And, first of all, have you found a server to test some scripts?
Testing generally explains better than 10 hours of theory
Logged
GaryCXJk
<strong>Official Charas.EX Team Member</strong>
Exemplar
Posts: 1,586
FOUR OH FOUR'D
(No subject)
«
Reply #7 on:
September 13, 2003, 01:08:49 PM »
Okey, let's put it simpler, to explain what a variable is. I'll start with some maths.
1+1=2
Everyone knows this. Well, there are more complex things we use in maths, like:
( 2 * 5 ) / ( 4 - 2 ) = 10 / 2 = 5
A variable is something that resembles an unknown number.
a+a=2a
This example is just the same as:
1+1=2*1
With variables, you can leave the * out in most cases, but remember that programming languages do need the *. So now the example would be:
a+a=2*a
Okey, but what does a mean? Well, to be simple, nothing, until you assign a number to it. So let's say a is 2. We write it down like this:
a=2
The calculation would be:
2
+
2
=2*
2
This is just like in PHP and other languages:
a=2;
You can also 'store' calculations in a variable:
b=1/2;
Now b is 1/2 or 0.5 to be exact. This way you can store a whole calculation at once in a variable:
c = a * ( 2 + b ) - 9;
Logged
Play it now!
Charas Breakout DX
Area91: for MUGEN and RPG Maker VX Ace stuff
SereneRose
Member
Initiate
Posts: 39
Nothing to say.
(No subject)
«
Reply #8 on:
September 13, 2003, 06:45:27 PM »
HA okay LOL Just wanted to see what that was.. Didnt know for sure, ^_^..
as for that last part think i understanding it more now.
Logged
http://www.everseeingrpg.tk
~Psh ^_^ Pretty new Intro on my page Heh~
Alex
I am the MASTER!
Administrator
Exemplar
Posts: 1,130
(No subject)
«
Reply #9 on:
September 15, 2003, 09:54:51 AM »
That's fine!
What about you, Kijuki_Magazaki?
Logged
GaryCXJk
<strong>Official Charas.EX Team Member</strong>
Exemplar
Posts: 1,586
FOUR OH FOUR'D
(No subject)
«
Reply #10 on:
September 15, 2003, 12:03:53 PM »
Forgot something.
Note that if you change the a or b variable in the previous example, the c variable won't change. You'll have to call the calculation again.
Example:
a
=
5
;
// result: 5
b
=
9
;
// result: 9
c
=
a
+
b
;
// result: 14
a
=
4
;
// result: 4
d
=
c
;
// result: 14
Logged
Play it now!
Charas Breakout DX
Area91: for MUGEN and RPG Maker VX Ace stuff
SereneRose
Member
Initiate
Posts: 39
Nothing to say.
(No subject)
«
Reply #11 on:
September 15, 2003, 07:54:35 PM »
Alright.. And also, i still havent found a Server, at least that i can understand being in English and free to do php with.
Logged
http://www.everseeingrpg.tk
~Psh ^_^ Pretty new Intro on my page Heh~
Ougle
Member
Initiate
Posts: 12
Programmer, Web Master, etc.
(No subject)
«
Reply #12 on:
April 04, 2005, 01:18:43 AM »
well well well, I was quite impressed by the first lesson, it inspired people on a forum to actually go and search?!!! that is impressive and I think it was quite a good lesson (to be honest I didnt fall asleep in class and skip some of the lesson, only coz I know php though)
A free php host, that is actually free, can be quite hard to find though (for a college website project my team went through loads of goodle result pages and link listings that didnt get us where we wanted).
Really you need to know someone who knows someone who knows a free php host, luckily I stumbled across this forum, and luckily I really like you.
Here's the link
Gaurd it with your life, I wont let anyone know this address, it really is precious.
Logged
From Gregory
Ougle
Member
Initiate
Posts: 12
Programmer, Web Master, etc.
(No subject)
«
Reply #13 on:
April 04, 2005, 01:32:14 AM »
as for understanding lesson 2,
I think it will be clearer what variables are for in Alex's next lesson.
Here's one example why I might want to put some text(a 'string') into a variable rather than just using the text:
I ask the user what their name is and they type it in a text box.
$name = (we need to learn the code to get whats in the text box)
Now I want to say hello to the user...
print "Hello ";
print $user;
And the result will be something like "Hello Ougle". As it's different for each user and the user's name (or what they chose to type in) it will v-a-r-y it has to be stored in a v-a-r-i-able!! (did you see my clever link there?)
Overal, much better than just being able to say "Hello world" each time!
Logged
From Gregory
SleepAid
Anime made baby Jesus cry.
Zealot
Posts: 732
I uh... like sleeping?
(No subject)
«
Reply #14 on:
April 04, 2005, 02:35:43 AM »
I think we should ban him. I mean, I know it's a sticky, but that topic kick was well over a year.
Logged
Print
Pages: [
1
]
2
« previous
next »
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
PHP
(Moderator:
De KoFfieŠ
) »
Php: Lesson 2