Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP SimpleXML DOM using between functions
    primarykey
    data
    text
    <p>I have been trying to get my head around how simplexml and dom updates XML between functions. The reason i ask is that the code i have written seems to work, but without me having to declare anything as global, and im a little confused as to why it does this. </p> <p>For example, i have this (simplified) code:</p> <pre><code>&lt;? foreach ($filenames as $filename) { $xml = simplexml_load_file($filename); updateXml($xml); $xml-&gt;last_update = date('Y-m-d H:i:s'); $xml-&gt;asXML($filename); } function updateXml($xml) { //... if ($data = $xml-&gt;xpath('//data/info[product_id="' . $product_data['id'] . '"]')) { $parent = $data[0]-&gt;xpath("parent::*"); $data = updateItem($parent[0], $product_data); } else { $product = addItem($xml-&gt;products, $product_data['id']); $data = updateItem($product, $product_data); } } function updateItem($parent, $product_data) { $node = dom_import_simplexml($parent); $dom = $node-&gt;ownerDocument; $product = $dom-&gt;createElement('product'); $node-&gt;appendChild($product); $item = $dom-&gt;createElement('id', $product_data['id']); $product-&gt;appendChild($item); $item = $dom-&gt;createElement('name', $product_data['name']); $product-&gt;appendChild($item); $item = $dom-&gt;createElement('url'); $product-&gt;appendChild($item); $cdata = $dom-&gt;createCDATASection($product_data['url']); $item-&gt;appendChild($cdata); $item = $dom-&gt;createElement('price', $product_data['price']); $product-&gt;appendChild($item); return $node; } ?&gt; </code></pre> <p>Can you please help me understand how the XML is being updated between the functions without needing to declare it as global? I know it seems strange to ask about something that works, but i need to get my head around it :-)</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload