Introduction to Variables
Variables are critical in programming. They hold information, just like algebra variables do. There are two different type of variables in Torquescript. Global and Local. Global variables last forever unless deleted by either the engine, or the user. Local variables only last inside of the scope it was created in.
Write each of these statements in your console:
$variable = "Hello World!";
%variable = "Hello World!";
Now, in the console write this:
echo($variable);
OUTPUT:
==> "Hello World"
echo(%variable);
OUTPUT:
==> ""
You'll notice that $variable still has the data we put into it and %variable doesn't. That is really the difference between the two.
$variable = "Hello World!";
%variable = "Hello World!";
Now, in the console write this:
echo($variable);
OUTPUT:
==> "Hello World"
echo(%variable);
OUTPUT:
==> ""
You'll notice that $variable still has the data we put into it and %variable doesn't. That is really the difference between the two.
Now, let's use some variables inside of a function.
$variable = "Hi";
function sayVariable()
{
echo($variable);
}
sayVariable();
OUTPUT:
==> "Hi";
Now when we call this function (sayVariable();), it'll say what $variable is. You can change $variable at anytime and call the function and see how it echos different things.
$variable = "Hi";
function sayVariable()
{
echo($variable);
}
sayVariable();
OUTPUT:
==> "Hi";
Now when we call this function (sayVariable();), it'll say what $variable is. You can change $variable at anytime and call the function and see how it echos different things.
function sayVariable(%this)
{
echo(%this);
}
sayVariable("Hi");
OUTPUT:
==> "Hi";
Now when we call this function (sayVariable("some text in here");), it'll say whatever we want it to because when we declared the function, we told it to hold data inside of %this. If you tryed that with the previous function, it wouldn't work.
{
echo(%this);
}
sayVariable("Hi");
OUTPUT:
==> "Hi";
Now when we call this function (sayVariable("some text in here");), it'll say whatever we want it to because when we declared the function, we told it to hold data inside of %this. If you tryed that with the previous function, it wouldn't work.
Let's do one more example, but using math this time:
$var1 = 5;
$var2 = 5;
function doAddMath()
{
echo($var1 + $var2);
}
doAddMath();
OUTPUT:
==> "10"
This will echo "5". You can again, change the global variables and call the function to see the results change.
$var1 = 5;
$var2 = 5;
function doAddMath()
{
echo($var1 + $var2);
}
doAddMath();
OUTPUT:
==> "10"
This will echo "5". You can again, change the global variables and call the function to see the results change.
Now for a local variable:
function doAddMath(%num1,%num2)
{
echo(%num1 + %num2);
}
doAddMath(5,5);
OUTPUT:
==> "10"
See, variables aren't so hard. Moving on to arrays!
function doAddMath(%num1,%num2)
{
echo(%num1 + %num2);
}
doAddMath(5,5);
OUTPUT:
==> "10"
See, variables aren't so hard. Moving on to arrays!