SAREscript syntax
1. Basics
SAREscript is case sensitive. Commands, functions and variables must be used with the same capitalization as their declarations. Most function names are lowercase. For example, print exists, but Print, PRINT and PrInT do not. If they did exist, they could be entirely different functions.
As in many programming languages, terminate each statement with a semicolon ;. Omitting it does not always produce an error, but the consequences may be unpredictable.
2. Data types
SAREscript uses these data types: NUMERIC (numbers), STRING (character strings), ARRAY (arrays), BOOL (true/false), and the special NULL type. Use a decimal point for floating-point numbers, for example 0.5, -2.123 and 1.0.
3. Strings
Define strings with double quotes " or single quotes '. PHP-style heredoc syntax is also available. Everything between the opening and closing quotes belongs to the string. Examples: "This is a string" and 'This is also a string'.
Inside double quotes, use a backslash \ to escape special characters. For example:
print("This is the first line.\nThis is the second line.");produces:
This is the first line.
This is the second line.Whereas:
print('This is the first line.\nThis is the second line.');is displayed literally:
This is the first line.\nThis is the second line.Another use of \ is escaping a double quote inside a string. This example produces an error:
print("<img src="http://jakisadresobrazka.gif">");To avoid the error, write:
print("<img src=\"http://jakisadresobrazka.gif\">");Heredoc is useful for defining longer strings, such as HTML fragments. Start with the <<< operator, immediately followed by an identifier and a newline. The string starts on the next line and ends when the same identifier appears again. The closing identifier must start in the first column and have no other characters on its line, except an optional semicolon. Identifiers may contain letters, digits and underscores (_), but must start with a letter.
Example:
print(<<<EOT
<a href="http://www.sare.pl/">
<img src="http://www.sare.pl/logo.gif">
</a>
EOT
);Unlike PHP, SAREscript treats $ inside strings as an ordinary character, not the start of a variable. Variable references inside strings are therefore not replaced with their values during parsing.
SAREscript uses UTF-8 and stores all strings in that encoding.
4. Variables
SAREscript allows any number of variables. A variable name begins with $, followed by a letter without language-specific diacritics, then optionally more letters, digits or underscores. For example, $my_variable2 is valid, but $2my_variable is not.
Array variables are also supported. Arrays provide access to stored values through keys of type NUMERIC or STRING. Create an array using a function such as array(), or by assigning any value, including another array, to an element identified by its key:
$variable[key] = value
All of these declarations are valid and make $numbers an array:
$numbers = array("zero", "one");
$numbers[1] = "one";
$numbers["one"] = 1;
The array key SYSTEM is reserved. It contains system information and cannot be used for your own data.
Unless stated otherwise, most functions returning arrays index them from zero: the first element has index 0.
5. Operators and expressions
SAREscript supports the following operators:
a) Arithmetic
| Name | Operator | Example | Result |
|---|---|---|---|
| Negation | - | -$a | Negates $a; equivalent to ($a * -1) |
| Addition | + | $a + $b | Sum of $a and $b |
| Subtraction | - | $a - $b | Difference between $a and $b |
| Multiplication | * | $a * $b | Product of $a and $b |
| Division | / | $a / $b | Quotient of $a and $b |
| Modulo | % | $a % $b | Remainder of $a divided by $b |
b) Assignment
| Name | Operator | Example | Result |
|---|---|---|---|
| Assignment | = | $a = $b | Assigns the value of $b to $a |
c) Increment/decrement
| Name | Operator | Example | Result |
|---|---|---|---|
| Increment | ++ | $a++ | Increases $a by 1 |
| Decrement | -- | $a-- | Decreases $a by 1 |
d) Comparison
| Name | Operator | Example | Result |
|---|---|---|---|
| Equal | == | $a == $b | True if $a equals $b |
| Not equal | != | $a != $b | True if $a differs from $b |
| Identical | === | $a === $b | True if $a equals $b and both have the same type |
| Not identical | !== | $a !== $b | True if $a differs from $b or they have different types |
| Greater than | > | $a > $b | True if $a is greater than $b |
| Less than | < | $a < $b | True if $a is less than $b |
| Greater than or equal | >= | $a >= $b | True if $a is greater than or equal to $b |
| Less than or equal | <= | $a <= $b | True if $a is less than or equal to $b |
e) Logical
| Name | Operator | Example | Result |
|---|---|---|---|
| Or | or | $a or $b | True if $a or $b is true |
| And | and | $a and $b | True if both $a and $b are true |
f) String
| Name | Operator | Example | Result |
|---|---|---|---|
| Concatenation | . | $a . $b | Concatenates strings $a and $b |
Operator precedence, highest first:
++ --
negation
* / %
+ - .
< > >= <=
== != === !==
=
or
and6. Control structures
Control structures are among the most important elements of SAREscript. They allow code to execute conditionally. As in other languages, the basic structure is if:
if (expression) {
// code to execute
}If the expression is true, the code inside the braces runs. Example:
if ($plec == "M") {
print("Dear Sir");
}Use else to define an alternative:
if (expression) {
// code to execute
} else {
// alternative code to execute
}If the expression is true, the first block runs. Otherwise, the alternative block runs. Example:
if ($plec == "M") {
print("Dear Sir");
} else {
print("Dear Madam");
}SAREscript also supports elseif, combining else and if:
if (expression1) {
// code to execute 1
} elseif (expression2) {
// code to execute 2
} elseif (expression3) {
// code to execute 3
} else {
// code to execute 4
}If expression1 is true, its block runs. Otherwise, expression2 is checked, followed by subsequent elseif expressions until a true condition is found. If none is true, the else block runs, if defined. Example:
if ($wiek < 21) {
print("Hi");
} elseif ($wiek < 50) {
print("Hello");
} else {
print("Good morning");
}Structures can be nested.
If an if condition contains expressions that require evaluation, all of them are evaluated, even when some are unnecessary to determine the result. For example:
if (expression1 or expression2)Both expressions are evaluated. SAREscript does not use the short-circuit behavior familiar from languages such as PHP.
7. Loops
A for loop executes a section of the script more than once. Its syntax is:
for ($variable = start to stop) {
// code to execute
}start is the initial value of $variable, and stop is the final value. If start is smaller than stop, the variable increases by one each iteration until it reaches or exceeds stop. If start is larger than stop, the variable decreases by one each iteration until it reaches or falls below stop. If start and stop are equal, the code runs exactly once.
$variable cannot be an array variable.
Example:
for ($licznik = 1 to 5) {
print($licznik." times ".$licznik." equals ".($licznik*$licznik)."\n");
}Output:
1 times 1 equals 1
2 times 2 equals 4
3 times 3 equals 9
4 times 4 equals 16
5 times 5 equals 25Example:
for ($licznik = 5 to 1) {
print($licznik." times ".$licznik." equals ".($licznik*$licznik)."\n");
}Output:
5 times 5 equals 25
4 times 4 equals 16
3 times 3 equals 9
2 times 2 equals 4
1 times 1 equals 18. Registries
SAREscript supports system-wide global variables called registries. Registries are database fields accessed by name. A registry name can contain up to 255 characters. A standard registry stores strings of up to 1023 characters; BIG registries support up to 16777215 (16 MB).
9. Functions
Functions accept parameters and return the result of their operation. SAREscript offers both email-system-specific functions and equivalents of PHP functions. Alongside basic functions such as print(), there are advanced functions that operate on large amounts of data, such as pl_imiewolacz(). Adding more functions is a main direction of SAREscript development. See the detailed function reference.