XtScript: Variable
Variable in XtScript
Working with variable in XtScript can be imagined like combining variable in Javascript and PHP.
Variable in xtscript must be declared first using var keyword, followed by $ sign next to the variable name and = as the variable value.
Characters that are legal to be used as a variable name in xtscript is letters [a-z, A-Z], numbers [0-9], and underscore [_].
There are two keywords that can be used to declare and set a variable, they are:
- assign
- var
However because var is simply shorter, we prefer to use this keyword in our tutorial and example.
Learn XtScript Variable from different examples
Store a string into a variable and output the variable.
- Code:
- Code:
<!--parser:xtscript--> var $demo = I love Xtgem print $demo <!--/parser:xtscript-->- Result:
I love Xtgem
Set a number into a variable, make and addition, and output the result.
- Code:
- Code:
<!--parser:xtscript--> var $money = 100 var $left = ($money + 50) print My money now is $left; <!--/parser:xtscript>- Result:
My money now is 150
Unlike Javascript and PHP that separate variable and string using " or ', xtscript doesn't have it. The only thing that distinguish a variable and a string in xtscript is the $ sign.
The following print commands will results error:
<!--parser:xtscript--> var $name = Michael var $surname = Jackson # Will result error print My name is $name $surnameCool <!--/parser:xtscript>
The following codes are correct:
<!--parser:xtscript--> var $name = Michael var $surname = Jackson # "-" is an ILLEGAL character in variable name print My name is $name $surname-Cool <!--/parser:xtscript-->- Result:
My name is Michael Jackson-Cool
Last edited on