PHP addAttribute() Function

Description

The addAttribute() function adds an attribute to the SimpleXML element.

Syntax

PHP addAttribute() Function has the following syntax.

addAttribute(name,value,ns);

Parameter

ParameterIs RequiredDescription
nameRequired.Name of the attribute to add
valueOptional.Value of the attribute
nsOptional.A namespace for the attribute

Return

No value is returned

Example

Add an attribute to the root element and to the body element:


<?php//from w ww . j  ava 2s  . c o  m
$note=<<<XML
<book>
    <name>PHP</name>
    <name>Java</name>
</book>
XML;

$xml=new SimpleXMLElement($note);
$xml->addAttribute("type","private");
$xml->body->addAttribute("date","2013-01-01");

echo $xml->asXML();
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions