**small tip: Whenever a bracket starts, it must end, same thing with the parenthesis...
So { must have a }
" must have another "
( and ) etc.
end small tip**
I told you guys you can name your variables whatever you want, well, not really.
Try to avoid having space in your variable names, you can't have john audi = 2; but johnaudi = 2; will work.
Try not to name your variable a number, you can't do 4 = "bacon"; (I'll go wider into strings in another tutorial).
Some variables have already assigned numbers, like x, y, z, zoom, image, timevar2 etc...
You can use them, but make sure you're not the only one that can manipulate them... (timevar2 is read only, you can't set timevar2 = 3; cause timevar2 is the amount of seconds when UNIX first started, will teach you that later)
The if statement:
The if statement is one of the crucial statements in scripting, you MUST know this one to finish learning your noob stage.
What the if statement is, it's a small box, in your big function box, that has a to-do list in it...
So some of you will think it's a function inside a function ... ? No, we're not in Inception where there's a dream inside the dream, not that complicated... What do you use 'If' for in your daily routine?
Here's an example...
Hi john, if 2 is less then 3, I'll give you a cookie...
The script for that will be:
PHP Code:
function onCreated() {
if (2 < 3) { // the if statement is written like this : if (test, checks if the test is true, if yes, it will run the code inside the box/brackets
// You'll find new operators in here, in math class we all know what < and > is, right? If you do not, I'm sorry to tell you that this tutorial is not made for you...
//So is 2 less then 3? Yes, yes it is, okay, since it is less than 3, let's run the code inside the brackets which is echo("Here's your cookie!");
echo("Here's your cookie!");
}
}
This will print out Here's your cookie! since 2 is in fact less than 3.
But is 2 greater than 3? No it's not!
PHP Code:
function onCreated() {
if (2 > 3) { // this test is false, that means it is not true, and since we 'lied' to the compiler, he will not run the script inside the brackets for us! bad compiler!
echo("COOKIE!");
}
}
And this will echo nothing...
So, I've been told I'll get another cookie if 3 is equal to 6 divided by 2, so you guys would be like 'Hey john, I know this one! you just have to put if (3 = 6 / 2) right?'
Well, WRONG, = is used to assign a variable to a number, such as x = 4; t = -3; nub = 0; etc, but if you're checking 'if' something is 'equals' to another, you have to use '==' not '='... Yes this is confusing, but play with it at first so you can understand.
So basically:
PHP Code:
function onCreated() {
if (3 == 6/2) {
echo("stuff");
}
}
---
As you can see, there are some exceptions in the if() statement, but it's easy to use!
== is used to check if two things are equal
< is used to check if the first var is smaller than the second var
> if first var is bigger than second var
<= if first var is less or equal to second var
>= if first var is bigger or equal to second var
!= if first var is NOT equal to second var
remember that ! means NOT.
Why isn't it !==? I don't know, take this out with most of the scripts used these days
Logical Operators:
Yes, It's beginning to get boring, but in my next threads I'll teach you guys how to have some player interactions once you understand these important stuff, and your programming will become a whole lot easier.
What are these logical operators?
Let's say for example, I want something to echo if 1 + 1 equals 2, and 2 is less than 4.
Two ways to do that, you can place an if statement inside an if statement, or use a logical connector.
Example of 2 if statements:
PHP Code:
function onCreated() {
if (1 + 1 == 2) {
if (2 < 4) {
echo("test");
}
}
}
And here's how the compiler thinks:
Oh, john, we meet again! So what do you have for me?
PHP Code:
function onCreated() {
Oh yes, the onCreated() function, we should call this now since I can trigger it for free at the beginning!
PHP Code:
if (1 + 1 == 2) {
Wait, doing some math, does 1 + 1 actually equals 2? Hmmmm, Yes, since 2 == 2 so, there, you can access the code inside the if statement, have fun!
Another if statement? Cool! if the last result was false, I wouldn't have seen that... Is 2 < 4? yes, it's
true. So what's inside these brackets?
Okay john, you drove me all the way to simply echo 'test'? as you wish... *sends the text 'test' to RC*
Consider the if statements as a Guru, if the answer is correct, you shall see what's inside the box!
So, what's a logical operator? Without having to put 2 if statements, you can easily put the logical operator '&&' which means and.
so you'd have:
PHP Code:
function onCreated() {
if (1 + 1 == 2 && 2 < 4) {
echo("test");
}
So what it did, is check the operation on the left, and check the operation on the right. If the operation on the left AND the operation of the right is TRUE, the compiler will read the script in the box/brackets. But if one/ or two of them is false, the compiler will not read the stuff inside this statement.
Another logical operator is called '||' or simply OR. This will check if one of them is 'true'.
so
PHP Code:
function onCreated() {
if (1 + 1 == 3 || 2 < 3) { // 1 + 1 is not equal to 3, so this is false, but 2 is in fact less than 3, so that's true, since there's 1 true, let's let the nice compiler read the script
echo("test"); // and it will echo test
}
}
---
else:
You guys remember that an if statement needs to return 'true' if it's correct? But what if it's not?
Let's talk this out... John, I'll give you a cookie if 1 + 2 is 4, but if not, you're going to cry...
The script of that would be:
PHP Code:
function onCreated() {
if (1 + 2 == 4) {
echo("Here's your cookie!");
} else {
echo("*cries* :(");
}
}
And this will only echo '*cries* '... so what it does is, check if the if() statement is true, if it is, it will read the stuff inside if without reading the stuff inside else, but if it's not, it will read the stuff inside else.
Maybe reading that isn't as practical as trying it, go ahead, copy some of those scripts, tweak with them a bit, and check out how it works.
Using if's with variables:
Programmers and developers almost never use two numbers in an if() statement, since it will cause it to calculate them for no reason, but they use the if() statement for variables, for example...
PHP Code:
function onCreated() {
john = 3;
if (john == 3) {
echo("yay!");
} else {
echo("nub");
}
}
Which will of course echo 'yay!'...
Remember that we can put two or more variables in an if statement:
PHP Code:
function onCreated() {
john = 5;
tuna = 2;
burger = 2;
if (john != 4 && tuna == burger) {
echo("wow cool!");
} else echo("that didn't work x.x");
}
and this will echo 'wow cool!'...
If you're wondering while I didn't add the {} to the else, is because everything that needs brackets (function something(), if(), else, others you'll learn later) can have it's bracket removed and replace in one line, for example:
PHP Code:
function onCreated()
if (1 + 1 == 2) echo("hi");
The compiler will automatically place brackets for the next semicolomn, so if you have:
PHP Code:
function onCreated() {
if (1+ 2 == 3) echo("hi"); echo("sup");
}
the compiler will read it as:
PHP Code:
function onCreated() {
if (1 + 2 == 3) {
echo("hi");
}
echo("sup");
}
But this is more of a styling code, if you didn't get that last part, it's not that important, you can keep using the {} which is better to keep track of your code...
So, basically, you know the major basics of the if statement, I'll see you guys later on my next tutorial!
Any questions are welcome!