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:
New forum theme up and running!
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
PHP
(Moderator:
De KoFfieŠ
) »
Php: Lesson 3
« previous
next »
Print
Pages: [
1
]
Author
Topic: Php: Lesson 3 (Read 2359 times)
GaryCXJk
<strong>Official Charas.EX Team Member</strong>
Exemplar
Posts: 1,586
FOUR OH FOUR'D
Php: Lesson 3
«
on:
August 12, 2005, 05:33:07 PM »
Okay, let's summarise what Alexander had explained before:
Lesson 1
You can just mix HTML with PHP, and that the PHP parts won't be seeable when looking into the source. We also know how the
print
command works.
Example:
My page
print "Hello world!";
?>
When opening this file in your web browser, you'll see:
Hello world!
And when looking into the source, you would see:
My page
Hello world!
Lesson 2
We know how to use variables, how to set them and how to alter them. We also learn about different variable types, and how they can somewhat be cross-used.
Examples:
print "Hello world!";
is the same as
$text="Hello world!";
print $text;
The following blocks:
$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;
result in the same text:
My sentence is: Hello world!
If you don't know enough about variables or PHP itself, it's best to look into the previous lessons again, maybe even ask questions there. The topics are always open, you can ask questions anytime, no matter what the age of the topic or the last post is.
Okay, now we know how to display variables on screen, and we know how to add parts to it, or do mathematical things with it, but how can we also use them?
The first thing we need to learn is comparing. This is the base of all programming and scripting, without comparing variables you can't create dynamic scripts, and that's what we want right?
So how does this comparing work? Let's start out with a simple thing. This is not PHP yet.
1 = 1
1 + 1 = 2
2 - 1 = 3 - 2
a * a + b * b = c * c
2 > 1
4 < 100
4 <= 4.01
3 >= 3
It's simple, it's like normal algebra. But how do we do it in PHP? I'll start by explaining how it works:
if( statement )
{
// Action
}
Basically, I've put in two things which need explenation. First, the double slashes. They are used in comments, which means that they are not really part of the code. There are two types of comments:
// One-lined comments
/*
Multi-lined comments
*/
Comments are handy to keep track of what you have done, or to explain what happens in that particular code if you share your scripts.
But enough about comments, let's get back to the if-constructor, as most scripters call it. First, it sees if the statement is right, meaning, if the equations is right. Let me explain with a simple example.
$a
=
10
;
$b
=
10
;
if (
$a
==
$b
)
{
print
"
$a
is equal to
$b
"
;
}
When the script is started, you will see something like this:
10 is equal to 10
What is happen? Okay, commander, let me explain to you. First, $a and $b is set to 10. Then it checks if $a and $b are equal. If they are, everthing between the {} is being executed, in this case they are equal.
You can also see another way of using variables in the print-action. When you put a variable directly between the double quotes, it automatically replaces it with the content of the variable. If you would use single quotes, you would see:
$a is equal to $b
Note that in the if-constructor, and in many constructors which require a statement, you will need to use == instead of =. This has one good reason. = means becomes, while == means is equal to. So, $a = $b means $a becomes $b, while $a == $b means $a is equal to $b.
But what if you want something else to happen when the statement, in this case, $a == $b, is not true? For that we have the
else
-block.
$a
=
10
;
$b
=
14
;
if(
$a
==
$b
)
{
print
"
$a
is equal to
$b
"
;
}
else
{
print
"
$a
is not equal to
$b
"
;
}
In this case, $a and $b are not equal. You will not see the message that it is equal, but rather this:
10 is not equal to 14
So, now you understand how the else-block works. It's rather simple, right? Let's make it more simple. Say you have only one action, like the action above. I'll take the first example. When you only have one action, you don't need to use the {}.
$a
=
10
;
$b
=
10
;
if (
$a
==
$b
) print
"
$a
is equal to
$b
"
;
This can surely shorten the ammount of lines used! It can get even simpler, you can preform this trick with the else-block!
$a
=
10
;
$b
=
14
;
if(
$a
==
$b
) print
"
$a
is equal to
$b
"
;
else print
"
$a
is not equal to
$b
"
;
There are even more examples of how you can use the if-block.
if ( 1 == 1 ) print "1 is equal to 1";
if ( 2 > 1 ) print "2 is greater than 1";
if ( 3 < 4 ) print "3 is smaller than 4";
if ( 15 >= 10 ) print "15 is greater than or equal to 10";
if ( 13 <=14 ) print "13 is smaller than or equal to 14";
if ( 0 != 1 ) print "0 is not equal to 1";
Okay, so we know how to compare things. But what if we want to repeat something over and over again, a certain ammount of times, before continuing. How would we do that?
It is done with the for-constructor. This is rather complicated, but I'll try to explain. If you don't get this part, don't worry, just ask here. Basically, you'll have to set a variable to a certain value, then you'll need a statement, and then a block which alters the variable. As long as the statement in the middle is true, the loop continues.
Here's an example to explain it more clearly.
for (
$i
=
0
;
$i
<
3
;
$i
=
$i
+
1
)
{
print
$i
.
"<br>"
;
}
Note that this DOES require the {}, or else it gives errors. When you start this script, you'll see something like this:
0
1
2
What happens here? First, $i is set to 0
($i = 0)
. Next, it checks if $i is smaller than 3
($i < 3)
. This is the case, so it displays the number 0 and adds a
tag (normal HTML). Finally, when the block is complete, it will add 1 to $i
($i = $i + 1)
. Then, again it checks if $i is smaller than 3, which is true, since 1 is smaller than 3. Then it again displays the number, and adds 1 to $i. Again, it checks if $i is smaller than 3, and that's true. It displays the number, and adds one. Then, it checks again if $i is smaller than 3. However, since $i is now equal to 3, the statement is false. It terminates the block, ignoring everything that is in the for-block.
Hopefully you'll understand everything here. When everyone understands it, we'll go on with the first assignment. The assignment is optional, you don't need to do it, but it is good to test your abilities, to see if you understand it enough.
Logged
Play it now!
Charas Breakout DX
Area91: for MUGEN and RPG Maker VX Ace stuff
Print
Pages: [
1
]
« previous
next »
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
PHP
(Moderator:
De KoFfieŠ
) »
Php: Lesson 3