Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. Expression Language
  5. The Expression Syntax
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Supported Literals
  • Working with Objects
    • Accessing Public Properties
    • Calling Methods
  • Working with Functions
  • Working with Arrays
  • Supported Operators
    • Arithmetic Operators
    • Bitwise Operators
    • Comparison Operators
    • Logical Operators
    • String Operators
    • Array Operators
    • Numeric Operators
    • Ternary Operators

The Expression Syntax

Edit this page

Warning: You are browsing the documentation for Symfony 2.8, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

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')
  • numbers - e.g. 103
  • arrays - using JSON-like notation (e.g. [1, 2])
  • hashes - using JSON-like notation (e.g. { foo: 'bar' })
  • booleans - true and false
  • null - null

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',
    array(
        '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 = array();
        for ($i = 0; $i < $times; $i++) {
            $greetings[] = 'Hi';
        }

        return implode(' ', $greetings).'!';
    }
}

$robot = new Robot();

var_dump($expressionLanguage->evaluate(
    'robot.sayHi(3)',
    array(
        'robot' => $robot,
    )
));

This will print out Hi Hi Hi!.

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 = array('life' => 10, 'universe' => 10, 'everything' => 22);

var_dump($expressionLanguage->evaluate(
    'data["life"] + data["universe"] + data["everything"]',
    array(
        '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',
    array(
        'life' => 10,
        'universe' => 10,
        'everything' => 22,
    )
));

This will print out 42.

Bitwise Operators

  • & (and)
  • | (or)
  • ^ (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)

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 parenthesis 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
16
17
$ret1 = $expressionLanguage->evaluate(
    'life == everything',
    array(
        'life' => 10,
        'universe' => 10,
        'everything' => 22,
    )
);

$ret2 = $expressionLanguage->evaluate(
    'life > everything',
    array(
        'life' => 10,
        'universe' => 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',
    array(
        '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',
    array(
        '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"]',
    array(
        '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',
    array(
        '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 to foo ? foo : 'no')
  • foo ? 'yes' (equal to foo ? 'yes' : '')
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Code consumes server resources. Blackfire tells you how

Code consumes server resources. Blackfire tells you how

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Eugene Leonovich, a Symfony contributor

Thanks Eugene Leonovich (@rybakit) for being a Symfony contributor

8 commits • 130 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia