The Expression Syntax ¶
The ExpressionLanguage component uses a specific syntax which is based on the expression syntax of Twig. In this document, you can find all supported syntaxes.
Supported Literals ¶
The component supports:
- strings - single and double quotes (e.g.
'hello'
)文字列 - 一重引用符と二重引用符 (例: 'hello') - numbers - integers (e.g.
103
), decimals (e.g.9.95
), decimals without leading zeros (e.g..99
, equivalent to0.99
); all numbers support optional underscores as separators to improve readability (e.g.1_000_000
,3.14159_26535
)数字 - 整数 (例: 103)、小数 (例: 9.95)、先行ゼロのない小数 (例: .99、0.99 に相当);すべての数字は、可読性を向上させるためにセパレーターとしてオプションのアンダースコアをサポートします (例: 1_000_000、3.14159_26535) - arrays - using JSON-like notation (e.g.
[1, 2]
)配列 - JSON のような表記を使用 (例: [1, 2]) - hashes - using JSON-like notation (e.g.
{ foo: 'bar' }
)ハッシュ - JSON のような表記を使用 (例: { foo: 'bar' }) - booleans -
true
andfalse
ブール値 - true および false - null -
null
ヌル - ヌル - exponential - also known as scientific (e.g.
1.99E+3
or1e-2
)指数 - 科学的とも呼ばれます (例: 1.99E+3 または 1e-2)
6.1
Support for decimals without leading zeros and underscore separators were introduced in Symfony 6.1.
Caution
A backslash (\
) must be escaped by 4 backslashes (
) in a string
and 8 backslashes (
) in a regex:
1 2 |
echo $expressionLanguage->evaluate('"\\\\"'); // prints \
$expressionLanguage->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"'); // returns true
|
Control characters (e.g. \n
) in expressions are replaced with
whitespace. To avoid this, escape the sequence with a single backslash
(e.g. \\n
).
Working with Objects ¶
When passing objects into an expression, you can use different syntaxes to access properties and call methods on the object.
Accessing Public Properties ¶
Public properties on objects can be accessed by using the .
syntax, similar
to JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Apple
{
public $variety;
}
$apple = new Apple();
$apple->variety = 'Honeycrisp';
var_dump($expressionLanguage->evaluate(
'fruit.variety',
[
'fruit' => $apple,
]
));
|
This will print out Honeycrisp
.
Calling Methods ¶
The .
syntax can also be used to call methods on an object, similar to
JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Robot
{
public function sayHi($times)
{
$greetings = [];
for ($i = 0; $i < $times; $i++) {
$greetings[] = 'Hi';
}
return implode(' ', $greetings).'!';
}
}
$robot = new Robot();
var_dump($expressionLanguage->evaluate(
'robot.sayHi(3)',
[
'robot' => $robot,
]
));
|
This will print out Hi Hi Hi!
.
Null-safe Operator ¶
Use the ?.
syntax to access properties and methods of objects that can be
null
(this is equivalent to the $object?->propertyOrMethod
PHP null-safe
operator):
1 2 3 4 5 6 7 |
// these will throw an exception when `fruit` is `null`
$expressionLanguage->evaluate('fruit.color', ['fruit' => '...'])
$expressionLanguage->evaluate('fruit.getStock()', ['fruit' => '...'])
// these will return `null` if `fruit` is `null`
$expressionLanguage->evaluate('fruit?.color', ['fruit' => '...'])
$expressionLanguage->evaluate('fruit?.getStock()', ['fruit' => '...'])
|
6.1
The null safe operator was introduced in Symfony 6.1.
Working with Functions ¶
You can also use registered functions in the expression by using the same
syntax as PHP and JavaScript. The ExpressionLanguage component comes with one
function by default: constant()
, which will return the value of the PHP
constant:
1 2 3 4 5 |
define('DB_USER', 'root');
var_dump($expressionLanguage->evaluate(
'constant("DB_USER")'
));
|
This will print out root
.
Tip
To read how to register your own functions to use in an expression, see "Extending the ExpressionLanguage".
Working with Arrays ¶
If you pass an array into an expression, use the []
syntax to access
array keys, similar to JavaScript:
1 2 3 4 5 6 7 8 |
$data = ['life' => 10, 'universe' => 10, 'everything' => 22];
var_dump($expressionLanguage->evaluate(
'data["life"] + data["universe"] + data["everything"]',
[
'data' => $data,
]
));
|
This will print out 42
.
Supported Operators ¶
The component comes with a lot of operators:
Arithmetic Operators ¶
+
(addition)+ (加算)-
(subtraction)- (減算)*
(multiplication)* (乗算)/
(division)/ (分割)%
(modulus)% (係数)**
(pow)** (パウ)
For example:
1 2 3 4 5 6 7 8 |
var_dump($expressionLanguage->evaluate(
'life + universe + everything',
[
'life' => 10,
'universe' => 10,
'everything' => 22,
]
));
|
This will print out 42
.
Bitwise Operators ¶
&
(and)& (と)|
(or)| | (また)^
(xor)^ (xor)
Comparison Operators ¶
==
(equal)== (等しい)===
(identical)=== (同一)!=
(not equal)!= (等しくない)!==
(not identical)!== (同一ではない)<
(less than)< (未満)>
(greater than)> (より大きい)<=
(less than or equal to)>=
(greater than or equal to)>= (以上)matches
(regex match)一致 (正規表現一致)contains
含むstarts with
で始まるends with
で終わる
6.1
The contains
, starts with
and ends with
operators were introduced
in Symfony 6.1.
Tip
To test if a string does not match a regex, use the logical not
operator in combination with the matches
operator:
1 |
$expressionLanguage->evaluate('not ("foo" matches "/bar/")'); // returns true
|
You must use parentheses because the unary operator not
has precedence
over the binary operator matches
.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ret1 = $expressionLanguage->evaluate(
'life == everything',
[
'life' => 10,
'everything' => 22,
]
);
$ret2 = $expressionLanguage->evaluate(
'life > everything',
[
'life' => 10,
'everything' => 22,
]
);
|
Both variables would be set to false
.
Logical Operators ¶
not
or!
または!and
or&&
or
or||
またはまたは ||
For example:
1 2 3 4 5 6 7 8 |
$ret = $expressionLanguage->evaluate(
'life < universe or life < everything',
[
'life' => 10,
'universe' => 10,
'everything' => 22,
]
);
|
This $ret
variable will be set to true
.
String Operators ¶
~
(concatenation)~ (連結)
For example:
1 2 3 4 5 6 7 |
var_dump($expressionLanguage->evaluate(
'firstName~" "~lastName',
[
'firstName' => 'Arthur',
'lastName' => 'Dent',
]
));
|
This would print out Arthur Dent
.
Array Operators ¶
in
(contain)に (含む)not in
(does not contain)入っていない (含まない)
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class User
{
public $group;
}
$user = new User();
$user->group = 'human_resources';
$inGroup = $expressionLanguage->evaluate(
'user.group in ["human_resources", "marketing"]',
[
'user' => $user,
]
);
|
The $inGroup
would evaluate to true
.
Numeric Operators ¶
..
(range).. (範囲)
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class User
{
public $age;
}
$user = new User();
$user->age = 34;
$expressionLanguage->evaluate(
'user.age in 18..45',
[
'user' => $user,
]
);
|
This will evaluate to true
, because user.age
is in the range from
18
to 45
.
Ternary Operators ¶
foo ? 'yes' : 'no'
ふー? 'はい・いいえ'foo ?: 'no'
(equal tofoo ? foo : 'no'
)foo ?: 'no' (foo ? foo : 'no' と同じ)foo ? 'yes'
(equal tofoo ? 'yes' : ''
)ふー? 'はい' (foo に等しい ? 'はい' : '')
Null Coalescing Operator ¶
This is the same as the PHP null-coalescing operator, which combines
the ternary operator and isset()
. It returns the left hand-side if it exists
and it's not null
; otherwise it returns the right hand-side. Note that you
can chain multiple coalescing operators.
foo ?? 'no'
ふー?? '番号'foo.baz ?? 'no'
foo.baz ?? '番号'foo[3] ?? 'no'
フー[3] ?? '番号'foo.baz ?? foo['baz'] ?? 'no'
foo.baz ?? foo['baz'] ?? '番号'
6.2
The null-coalescing operator was introduced in Symfony 6.2.
Built-in Objects and Variables ¶
When using this component inside a Symfony application, certain objects and variables are automatically injected by Symfony so you can use them in your expressions (e.g. the request, the current user, etc.):
- Variables available in security expressions;セキュリティ式で使用可能な変数。
- Variables available in service container expressions;サービス コンテナ式で使用可能な変数。
- Variables available in routing expressions.ルーティング式で使用できる変数。