YAML in PHP
Installing YAML extension
YAML does not come with a standard PHP installation, instead it needs to be installed as a PECL extension. On linux/unix it can be installed with a simple
libyaml-dev package must be installed on the system, as the PECL package is simply a wrapper around libYAML calls.
Installation on Windows machines is different - you can either download a pre-compiled DLL or build from sources.
Using YAML to store application configuration
YAML provides a way to store structured data. The data can be a simple set of name-value pairs or a complex hierarchical data with values even being arrays.
Consider the following YAML file:
Let's say, it's saved as
config.yaml. Then to read this file in PHP the following code can be used:
print_r will produce the following output:
Now config parameters can be used by simply using array elements: