Working with YAML in PHP: Libraries, Parsing, and File Creation

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with YAML in PHP: Libraries and Tools



YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It is often used for configuration files, defining data schemas, and exchanging information between services. In PHP, there are several ways to work with YAML: from built-in extensions (if available) to powerful third-party libraries. In this article, we will thoroughly examine the main tools, their installation, and practical examples.



Why is YAML needed in PHP?

PHP developers actively use YAML for:

  • Configuration files of frameworks (Symfony, Laravel in some cases);
  • Describing database migrations;
  • Storing translations and localization;
  • Defining the structure of test data (fixtures);
  • Configuring CI/CD (GitLab CI, Ansible).

Understanding how to parse and create YAML in PHP is a mandatory skill for a modern backend developer.



1. Built-in yaml extension (PECL)

The most performant way is to use the yaml extension, installed via PECL. It provides two key functions:

  • yaml_parse() — converts a YAML string into a PHP array;
  • yaml_emit() — converts a PHP array into a YAML string.


Installation

pecl install yaml

After installation, add the following line to php.ini:

extension=yaml.so


Usage Example

<?php// Parsing a YAML string$yamlString = "database:  host: localhost  port: 3306  name: test_db";

$config = yaml_parse($yamlString);echo $config['database']['host']; // localhost

// Creating YAML from an array$data = [ 'app' => [ 'name' => 'MyApp', 'version' => '2.0' ]];

$yamlOutput = yaml_emit($data);echo $yamlOutput;?>


2. Symfony Yaml Library (Recommended Approach)

If you cannot install PECL extensions (e.g., on shared hosting), the best choice is the Symfony Yaml library. It is part of the Symfony ecosystem but works perfectly as a standalone component. The library is written entirely in PHP, requires no external dependencies, and supports all features of YAML 1.1 and 1.2.



Installation via Composer

composer require symfony/yaml


Main Classes

  • Symfony\\Component\\Yaml\\Yaml — a static facade with parse() and dump() methods;
  • Symfony\\Component\\Yaml\\Parser — a class for parsing with additional options;
  • Symfony\\Component\\Yaml\\Dumper — a class for creating YAML with fine-grained formatting control.


YAML Parsing Example

<?phprequire_once 'vendor/autoload.php';

use Symfony\\Component\\Yaml\\Yaml;

$yamlContent = "server: host: 0.0.0.0 port: 8080 ssl: true";

try { $array = Yaml::parse($yamlContent); print_r($array); // Array ( [server] => Array ( [host] => 0.0.0.0 [port] => 8080 [ssl] => 1 ) )} catch (\\Symfony\\Component\\Yaml\\Exception\\ParseException $e) { echo 'YAML parsing error: ' . $e->getMessage();}?>


Creating YAML (Dump) from an Array

<?php$config = [    'application' => [        'name' => 'PHP App',        'debug' => true,        'database' => [            'driver' => 'mysql',            'host' => 'localhost',            'user' => 'root'        ]    ]];

$yaml = Yaml::dump($config, 3, 2);// The second argument is the nesting level (inline), the third is indentation in spacesecho $yaml;?>


Execution result:

application:  name: 'PHP App'  debug: true  database:    driver: mysql                    

Recommendations