17 C
Jaipur
Saturday, March 6, 2021

How to Install YAML and Use in PHP – Linux Hint

Must read

YAML is a human-readable data-serialization mark-up language. It is better than another mark-up language, XML to store the data because YAML content is easy to read and write. YAML syntax rules are used to create a YAML file that is a text file with the extension “.yml“. Many built-in functions exist in PHP for the YAML package. How YAML can be installed and used in PHP have shown in this tutorial.

Install YAML:

YAML extension is not installed and enables in PHP by default. So, you have to install the YAML package of PHP before trying the script in this tutorial.

Update the operating system by running the following command.

Install the yaml package of PHP by running the following command.

$ sudo apt-get install -y php-yaml

Restart the apache server to make the yaml extension enabled for PHP.

$ sudo service apache2 restart

Convert any data into YAML content:

yaml_emit() function is used to convert any data into YAML content. The syntax of this function is shown below.

Syntax:

string yaml_emit (<a href=“https://www.php.net/manual/en/language.types.declarations.
php#language.types.declarations.mixed”
>mixed</a> $data [, int $encoding = YAML_ANY_ENCODING [, int $linebreak = YAML_ANY_BREAK [, array $callbacks = NULL ]]] )

This function returns the YAML representation of any data. It can take four argument values. The first argument is mandatory and can any type of data. The other three arguments are optional. YAML has different types of encoding to generate output. The first optional argument can take any of YAML encoding as the argument value. YAML has different types of line break options also. The second optional argument can take any of the YAML line breaks. The third optional argument can take any call back function that returns an array.

Another function named yaml_emit_file() exists in the YAML package to store the YAML content into a file after converting the data. This function is supported only for YAML version 0.5.0 or more.

Example-1: Use of yaml_emit() function

The following example shows how the yaml_emit() function can be used to convert a two-dimensional associative array into YAML content. Create a PHP file with the following script. A two-dimensional array named $employees is declared in this script that contains five rows and four columns. <pre> tag is used to print YAML content in a structured format.

<?php

//Define a two-dimensional array

$employees= array(

‘E-10023’ => array(‘name’ => ‘Jafar Iqbal’, ‘post’ => ‘Manager’, ‘department’ => ‘Sales’, ‘joining_date’ => ’08-12-2000′),

‘E-10047’ => array(‘name’ => ‘Anisul Hoque’, ‘post’ => ‘Assistant Manager’, ‘department’ => ‘HR’, ‘joining_date’ => ’06-11-2010′),

‘E-10039’ => array(‘name’ => ‘Humayan Ahmed’, ‘post’ => ‘Accountant’, ‘department’ => ‘Marketing’, ‘joining_date’ => ’01-06-2009′),

‘E-10027’ => array(‘name’ => ‘Tamim Iqbal’, ‘post’ => ‘Manager’, ‘department’ => ‘HR’, ‘joining_date’ => ’02-11-2011′),

‘E-10093’ => array(‘name’ => ‘Rokeya Rahman’, ‘post’ => ‘Accountant’, ‘department’ => ‘Sales’, ‘joining_date’ => ’05-10-2011′)

);

//Convert the array into YAML content

$data = yaml_emit($employees);

//Print the array

echo “<pre>”.$data.“</pre>”;

?>

Output:

The following output will appear after running the script from the server. The output shows that the YAML content starts with ‘‘ and ends with ‘‘. Each row is identified by the employee ID that is printed first and the column values are printed under each employee ID.

Convert YAML content into an array:

yaml_parse() function is used to convert YAML content into an array. The syntax of this function is given below.

Syntax:

mixed yaml_parse ( string $input [, int $pos = 0 [, int &$ndocs [, array $callbacks = NULL ]]] )

This function can take four arguments and returns mixed data. The first argument is mandatory that will take YAML content as the argument value. The other three arguments are optional. The first optional argument is used to extract the document from the stream. The second optional argument is used to set the number of documents in the stream. The third optional document is used for defining the callback function.

Example-2: Use of yaml_parse() function

The following example shows how the yaml_parse() function can be used to convert any YAML content into an array. Create a PHP file with the following script. The YAML content is stored in the variable $yaml here. A delimiter is used to define the YAML content. The content starts with the delimiter followed by the ‘<<<‘ symbol. YML is used as the delimiter in this script. ‘—‘ is used before defining the property and ‘‘ is used after defining all properties. The property name and value are defined by using the colon(:). According to the script, id, name, post, and joining_date of all employees based on the department are defined in the content. ‘‘ symbol is used to define the property under another property. Next, <pre> tag is used with print_r() function to print the array in structured format after converting the YAML content.

<?php

//Define YAML content into a variable

$yaml = <<<YML

department: HR

employees:

– id: hr-56784

name: Jafar Iqbal

post: Manager

joining_date: “08-12-2000”

– id: hr-78342

name: Anisul Hoque

post: Assistant Manager

joining_date: “01-06-2009”

total_employees: 2

department: Sales

employees:

– id: sl-12897

name: Humayan Ahmed

post: Accountant

joining_date: “08-12-2002”

– id: sl-50067

name: Rokeya Rahman

post: Assistant Manager

joining_date: “01-10-2012”

– id: sl-67342

name: Tamim Iqbal

post: Assistant Manager

joining_date: “05-12-2017”

total_employees: 3

YML;

//Convert the YAML content into an array and print

echo “<pre>”;

print_r(yaml_parse($yaml));

echo “</pre>”;

?>

Output:

The following output will appear after running the script from the server.  According to the YAML content, the following array will be generated. The output shows that the Sales department contains three employee information and the HR department contains two employee information.

Conclusion:

YAML is a useful package of PHP for storing data or transferring data from one format to another format. YAML package contains many functions to store data or read data from different formats. The uses of yaml_emit() and yaml_parse() functions are explained in this tutorial by using two examples. I, hope, the concept of using the YAML package in PHP will be cleared for the readers after reading this tutorial.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article