PHP | DOMXPath evaluate() Function Last Updated : 13 Jul, 2021 Comments Improve Suggest changes Like Article Like Report The DOMXPath::evaluate() function is an inbuilt function in PHP which is used to execute the given XPath expression which is a pattern defined to select a set of nodes.Syntax: mixed DOMXPath::evaluate( string $expression, DOMNode $contextnode, bool $registerNodeNS ) Parameters: This function accept three parameters as mentioned above and described below: $expression: It specifies the XPath expression to execute.$contextnode (Optional): It specifies the contextnode for doing relative XPath queries. By default, the queries are relative to the root element.$registerNodeNS (Optional): It specifies whether to disable automatic registration of the context node. Return Value: This function returns TRUE on success.Exceptions: This function throws DOMXPathException on error.Below examples illustrate the DOMXPath::evaluate() function in PHP:Example 1: php <?php // Create a new DOMDocument instance $document = new DOMDocument(); // Create a XML $xml = <<<XML <?xml version="1.0" encoding="utf-8"?> <bookstore> <book> <title lang="en">GeeksforGeeks</title> <price>400</price> </book> <book> <title lang="en">Intro to XML</title> <price>300</price> </book> </bookstore> XML; // Load the XML $document->loadXML($xml); // Create a new DOMXPath instance $xpath = new DOMXPath($document); // Get the $tbody = $document-> getElementsByTagName('bookstore')->item(0); // Query to get the number of titles with lang // attribute "en" $query = 'count(//title[@lang=\'en\'])'; // Evaluate the query $entries = $xpath->evaluate($query, $tbody); echo "Number of elements with lang = \"en\": $entries\n"; ?> Output: Number of elements with lang = "en": 2 Example 2: php <?php // Create a new DOMDocument instance $document = new DOMDocument(); // Create a XML $xml = <<<XML <?xml version="1.0" encoding="utf-8"?> <GeeksforGeeks> Keep Learning. </GeeksforGeeks> XML; // Load the XML $document->loadXML($xml); // Create a new DOMXPath instance $xpath = new DOMXPath($document); // Get the $tbody = $document-> getElementsByTagName('GeeksforGeeks')->item(0); // Get the element with name GeeksforGeeks $query = '//GeeksforGeeks'; // Evaluate the query $entries = $xpath->evaluate($query, $tbody); echo $entries[0]->nodeValue; ?> Output: Keep Learning. Reference: https://www.php.net/manual/en/domxpath.evaluate.php Comment More infoAdvertise with us Next Article PHP | DOMXPath evaluate() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP eval() Function PHP eval() function in PHP is an inbuilt function that evaluates a string as PHP code. Syntax: eval( $string ) Parameters: This function accepts a single parameter as shown in the above syntax and described below. $string: It must consist of a valid PHP code to be evaluated but should not contain op 2 min read PHP | DOMAttr __construct() Function The DOMAttr::__construct() function is an inbuilt function in PHP which is used to create a new DOMAttr object. This created object is a read-only type. Syntax: public DOMAttr::__construct( string $name, string $value ) Parameters: This function accepts two parameters as mentioned above and describe 2 min read PHP | DOMNode C14N() Function The DOMNode::C14N() function is an inbuilt function in PHP which is used to convert a node into string. Syntax: string DOMNode::C14N( bool $exclusive, bool $with_comments, array $xpath, array $ns_prefixes ) Parameters: This function accepts four parameters as mentioned above and described below: $ex 2 min read PHP | DOMXPath registerNamespace() Function The DOMXPath::registerNamespace() function is an inbuilt function in PHP which is used to register the namespaceURI and prefix with the DOMXPath object. Syntax: bool DOMXPath::registerNamespace( string $prefix, string $namespaceURI ) Parameters: This function accepts two parameters as mentioned abov 2 min read PHP | DOMDocument load() Function The DOMDocument::load() function is an inbuilt function in PHP which is used to load an XML document from a file. Syntax: mixed DOMDocument::load( string $filename, int $options = 0 ) Parameters: This function accepts two parameters as mentioned above and described below: $filename: This parameter h 1 min read Like