Example 1 — Load a Temporary XML File
Write a small XML file into the temp folder, load it, read two values, and delete the file. The output is the same on every run.
<?php
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun-load-file.xml';
file_put_contents($file, '<book><title>PHP Basics</title><price>9.99</price></book>');
$xml = simplexml_load_file($file);
echo 'Title: ', $xml->title, PHP_EOL;
echo 'Price: ', $xml->price, PHP_EOL;
unlink($file);
echo 'Deleted: ', var_export(!file_exists($file), true), PHP_EOL; How It Works
- The file path points into the system temp folder, and file_put_contents() writes the fixed XML into it.
- simplexml_load_file() reads the file and returns the root element, which is book.
- $xml->title and $xml->price print the text of the child elements.
- unlink() removes the temporary file so nothing is left behind.

