group
The group attribute links code blocks together so they share state within a single PHP process. Variables defined in one block are accessible in subsequent blocks of the same group.
Syntax
markdown
```php group="name"
// Blocks with the same group name share state
```How It Works
- All blocks with the same
groupvalue are collected - They execute in document order within a single PHP process
- Variables, functions, and classes defined in earlier blocks are available in later ones
- Each block's assertions are evaluated independently
Example
php
$numbers = [1, 2, 3, 4, 5];
$sum = array_sum($numbers);
echo $sum;php
// $numbers and $sum are still available
$average = $sum / count($numbers);
echo $average;With Setup and Teardown
Groups are commonly used with setup and teardown blocks:
php
$pdo = new PDO('sqlite::memory:');
$pdo->exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');php
$pdo->exec("INSERT INTO users (name) VALUES ('Alice')");
$count = $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn();
echo $count;php
$pdo->exec("INSERT INTO users (name) VALUES ('Bob')");
$count = $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn();
echo $count;php
$pdo->exec('DROP TABLE users');Execution Order
Within a group:
setupblocks run first (in document order)- Regular group blocks run in document order
teardownblocks run last (in document order)
Multiple Groups
You can have multiple independent groups in the same file:
php
$greeting = 'Hello';php
$count = 42;php
echo $greeting . ', World!';php
echo $count * 2;Each group runs in its own process.