PHP Coding

Can anyone help me to understanding the coding below ? If possible, please mark the comment next to each line...
Any special meaning for "->"  in PHP ? Tks

$doc1=new DOMDocument();
$doc1->load($xmlfile1);      
$xpath1 = new DOMXpath($doc1);
$obj = $xpath1->query("//Header/Time");
$update_date2="";
       
if($obj->length>0)
     $update_date2 = $obj->item(0)->nodeValue;
else
    $error_content.=nodeNotExist("//Header/Time",$xmlfile1);
AXISHKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I haven't used either of those classes but the 'man' pages are here:
DOMDocument : http://cn2.php.net/manual/en/class.domdocument.php
DOMXpath : http://cn2.php.net/manual/en/class.domxpath.php

Represents an entire HTML or XML document; serves as the root of the document tree.

The '->' is an operator that connects to an element of a 'class'.  More here: http://cn2.php.net/manual/en/language.oop5.php
Julian HansenCommented:
Any special meaning for "->"  in PHP ? Tks
Yes the arrow is used to access properties and methods of an object.

Commented code
// CREATE A NEW object OF TYPE DOMDocument()
$doc1=new DOMDocument();

// CALL THE load METHOD ON THE DOMDocument 
// OBJECT CREATED ABOVE TO LOAD XML DOC
$doc1->load($xmlfile1);

// THIS INSTANTIATES (CREATES) A NEW DOMXpath
// OBJECT AND PASSES IN THE DOMDocument
// OBJECT CREATED ABOVE. THE DOMXPath 
// OBJECT ALLOWS YOU TO NAVIGATE THE XML
// DOCUMENT USING XPATH's
$xpath1 = new DOMXpath($doc1);

// QUERY THE DOCUMENT FOR THE TIME NODES 
// THAT ARE CHILDREN OF THEHEADER (ROOT) NODE
$obj = $xpath1->query("//Header/Time");

// CREATE AN EMPTY VARIABLE TO RECEIVE THE 
// nodeValue
$update_date2="";

// CHECK THE length PROPERTY of the DOMXPath
// OBJECT TO SEE IF ANY MATCHING NODES WERE FOUND
if($obj->length>0)

     // IF THERE ARE NODES THEN GET THE VALUE
    // OF THE FIRST ITEM
     $update_date2 = $obj->item(0)->nodeValue;
else
    // REPORT THE ERROR
    $error_content.=nodeNotExist("//Header/Time",$xmlfile1);

Open in new window


Basically what this code is doing is reading in an XML document and then finding the first <Time> node in the XML document that is a child of the root <Header> node and return the text value of that node.

Consider the following XML file
<Header>
	<Time>2015-07-29</Time>
	<Time>2015-07-31</Time>
</Header>

Open in new window

The value for $update_date2 after running it on this file would be
2015-07-29

The XPath //Header/Time says start at the top find the Header node then find the Time nodes under this - in this case 2 nodes are returned. The line
$update_date2 = $obj->item(0)->nodeValue;

Open in new window

Then retrieves the value of the first node.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
In PHP, objects and arrays are similar data structures.  Objects may be derived from classes that offer programming unique to the data structures.  The object data is called its properties; the programming is called methods.

How to access a named "index" element in an array:
$thing = $arr['index'];

The analogous access to the "index" property in an object:
$thing = $obj->index;

Learn more here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

And here:
http://php.net/manual/en/language.oop5.php
AXISHKAuthor Commented:
Tks
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.