Skip to content

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:

php
print("This is the first line.\nThis is the second line.");

produces:

text
This is the first line.
This is the second line.

Whereas:

php
print('This is the first line.\nThis is the second line.');

is displayed literally:

text
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:

php
print("<img src="http://jakisadresobrazka.gif">");

To avoid the error, write:

php
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:

php
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 ​

NameOperatorExampleResult
Negation--$aNegates $a; equivalent to ($a * -1)
Addition+$a + $bSum of $a and $b
Subtraction-$a - $bDifference between $a and $b
Multiplication*$a * $bProduct of $a and $b
Division/$a / $bQuotient of $a and $b
Modulo%$a % $bRemainder of $a divided by $b

b) Assignment ​

NameOperatorExampleResult
Assignment=$a = $bAssigns the value of $b to $a

c) Increment/decrement ​

NameOperatorExampleResult
Increment++$a++Increases $a by 1
Decrement--$a--Decreases $a by 1

d) Comparison ​

NameOperatorExampleResult
Equal==$a == $bTrue if $a equals $b
Not equal!=$a != $bTrue if $a differs from $b
Identical===$a === $bTrue if $a equals $b and both have the same type
Not identical!==$a !== $bTrue if $a differs from $b or they have different types
Greater than>$a > $bTrue if $a is greater than $b
Less than<$a < $bTrue if $a is less than $b
Greater than or equal>=$a >= $bTrue if $a is greater than or equal to $b
Less than or equal<=$a <= $bTrue if $a is less than or equal to $b

e) Logical ​

NameOperatorExampleResult
Oror$a or $bTrue if $a or $b is true
Andand$a and $bTrue if both $a and $b are true

f) String ​

NameOperatorExampleResult
Concatenation.$a . $bConcatenates strings $a and $b

Operator precedence, highest first:

text
++ --
negation
* / %
+ - .
< > >= <=
== != === !==
=
or
and

6. 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:

php
if (expression) {
// code to execute
}

If the expression is true, the code inside the braces runs. Example:

php
if ($plec == "M") {
print("Dear Sir");
}

Use else to define an alternative:

php
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:

php
if ($plec == "M") {
print("Dear Sir");
} else {
print("Dear Madam");
}

SAREscript also supports elseif, combining else and if:

php
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:

php
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:

text
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:

php
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:

php
for ($licznik = 1 to 5) {
print($licznik." times ".$licznik." equals ".($licznik*$licznik)."\n");
}

Output:

text
1 times 1 equals 1
2 times 2 equals 4
3 times 3 equals 9
4 times 4 equals 16
5 times 5 equals 25

Example:

php
for ($licznik = 5 to 1) {
print($licznik." times ".$licznik." equals ".($licznik*$licznik)."\n");
}

Output:

text
5 times 5 equals 25
4 times 4 equals 16
3 times 3 equals 9
2 times 2 equals 4
1 times 1 equals 1

8. 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.