Basic Statements and Expressions
Table of contents
- Echo Statement
- Variable Assignment
- Literals
- Accessing Values
- Return Statement
- Loop Control Statements
Echo Statement
The echo statement is used to output values.
echo 'Hello World!';
echo 123;
echo true;
echo LINEBREAK; // Outputs a new line
Variable Assignment
Variables can be assigned values using the = operator. Variables are dynamically typed.
myVariable = 10;
anotherVariable = 'some text';
booleanVariable = true;
Literals
PHP Script supports the following literal types:
- Numbers: Integers and floating-point numbers.
number = 123; floatNumber = 3.14; - Strings: Enclosed in single or double quotes.
singleQuoteString = 'Hello'; doubleQuoteString = "World"; - Booleans:
trueandfalse.isTrue = true; isFalse = false; - Null:
null.emptyValue = null; - Lists: Values in square brackets. They become PHP list arrays.
numbers = [1, 2, 3]; empty = []; echo numbers[0]; // 1 - Maps: Key-value pairs in curly braces. Keys are identifiers or strings. Maps become PHP
stdClassobjects, so an empty map{}stays different from an empty list[].customer = {name: 'Ada', 'first order': true}; echo customer.name; // Ada echo customer['first order']; // 1 - LINEBREAK: A special keyword representing a new line character.
echo 'Line 1' + LINEBREAK + 'Line 2';
Accessing Values
Dot notation reads object properties and array keys alike, index access reads list elements and map keys:
echo data.customer.name;
echo data.items[0].price;
Reading a key or property that does not exist is an error. PHP Script never silently returns null for a typo.
Return Statement
return ends the script and hands a value back to the host application. Without a value it returns null. The host reads the value with Engine::run(), see Evaluating and Running.
A line break ends a return, break or continue statement, as in JavaScript. The value or level must be on the same line:
return
total // not returned: `return` above already ended the script
total = data.amount * 1.19;
return {total: total, currency: 'EUR'};
Loop Control Statements
Break Statement
The break statement terminates the current loop immediately. It accepts an optional numeric level to exit multiple nested loops.
// Exit a single loop
for (i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop
}
}
// Exit multiple nested loops
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
break 2; // Exit both loops
}
}
See Control Flow for detailed examples and usage patterns.
Continue Statement
The continue statement skips the rest of the current loop iteration and continues with the next iteration. It accepts an optional numeric level to continue an outer loop.
// Skip to next iteration
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
echo i;
}
// Continue outer loop
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (j == 5) {
continue 2; // Continue outer loop
}
}
}
See Control Flow for detailed examples and usage patterns.