5. Statements

A statement is an expression terminated by a semicolon. Statements can contain code blocks delimited by curly braces which can then contain one or more statements themselves. Flow control constructs can also be statements.

A statement consisting wholly of a string literal, or a string literal with other strings concatenated to it will be output to the client. A statement consisting wholly of a variable will behave similarly.

The print instruction will cause the string or numeric expression supplied as its parameter to be output to the client. The print safe alternative does similarly, but forces the output to be checked for “safety”. In the case of LiveJournal and FotoBilder, this means running a HTML cleaner. All untrusted (non-system) layers always run through the checker, reglardless of which print instuction is used.

5.1. Flow Control

A limited subset of the flow control constructs normally present in high-level programming languages is available in S2. Each includes at least one code block enclosed in curly braces.

if ( expr ) block
Simple conditional. If expr evaluates to true, block will be executed, otherwise it will be skipped.
if ( expr ) block1 else block2
If expr evaluates to true, block1 will be executed, otherwise block2 will be executed.
if ( expr1 ) block1 elseif ( expr2 ) block2 else block3
If expr evaluates to true, block1 will be executed. Otherwise, expr2 will be tested and if it evaluates to true, block2 will be executed. If both expr1 and expr2 evaluate to false, block3 is executed.
foreach var ( expr ) block
block will be executed once for each element in the regular or associative array given in expr. On each iteration, an element of the array (or key of an associative array element) will be placed into the variable declared in var. foreach can also be used on strings, in which case the iteration variable must be a string and this variable will contain a character from the string with each iteration.