PHP | DOMAttr __construct() Function
Last Updated :
25 Sep, 2019
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 described below:
- $name: This parameter holds the element name of the attribute.
- $value: This parameter holds the value of the attribute.
Below programs illustrate the DOMAttr::__construct() function in PHP:
Program 1:
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$rootElement = new DOMElement( 'root' );
$element = $domDocument ->appendChild( $rootElement );
$domAttr = new DOMAttr( 'attr' , 'GeeksforGeeks' );
$attr = $element ->setAttributeNode( $domAttr );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<root attr="GeeksforGeeks"/>
Program 2:
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$rootElement = new DOMElement( 'root' );
$element = $domDocument ->appendChild( $rootElement );
$domAttr1 = new DOMAttr( 'Name' , 'GeeksforGeeks' );
$attr = $element ->setAttributeNode( $domAttr1 );
$domAttr2 = new DOMAttr( 'Address' , 'Noida' );
$attr = $element ->setAttributeNode( $domAttr2 );
$domAttr3 = new DOMAttr( 'mail' , 'abc@geeksforgeeks.org' );
$attr = $element ->setAttributeNode( $domAttr3 );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<root Name="GeeksforGeeks" Address="Noida" mail="abc@geeksforgeeks.org"/>
Reference: https://www.php.net/manual/en/domattr.construct.php
Share your thoughts in the comments
Please Login to comment...