# DOMNode::getAttribute()

- Thread: https://externals.io/message/52871
- Original archive: https://news-web.php.net/php.internals/52871
- Started by Matt Pelmear on 2011-06-03T20:38:00+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #52871 — Matt Pelmear — 2011-06-03T20:38:00+00:00

URL: https://externals.io/message/52871#52871

Hello,

I discovered today that the DOMNode::getAttribute() function (which is
undocumented on the php site) returns an empty string if the requested
attribute doesn't exist in the node.

From the source:
	if (value == NULL) {
		RETURN_EMPTY_STRING();
	} else {
		RETVAL_STRING((char *)value, 1);
		xmlFree(value);
	}

Seems to me that it should return NULL. (That's what libxml does, apparently.)

Does anyone know of a particular reason it returns an empty string instead?
Does anyone know of a particular reason it is not documented?
(I'd be happy to document and provide the ridiculously simple patch of
returning NULL...)

Thanks,
-Matt

---

## #52872 — Philip Olson — 2011-06-03T21:13:30+00:00

URL: https://externals.io/message/52871#52872 · In reply to: #52871

> Does anyone know of a particular reason it is not documented?
> (I'd be happy to document and provide the ridiculously simple patch of
> returning NULL...)

Greetings Matt,

Documenting DOM is messy/tricky (and underdocumented), but:

  - http://php.net/domelement.getattribute

Here's one option to begin helping out with the PHP documentation:

  - https://edit.php.net/

And please write phpdoc@lists.php.net for additional documentation related details and/or questions.

Regards,
Philip

---

## #52873 — Scott MacVicar — 2011-06-03T21:44:09+00:00

URL: https://externals.io/message/52871#52873 · In reply to: #52871

> Hello,
> 
> I discovered today that the DOMNode::getAttribute() function (which is
> undocumented on the php site) returns an empty string if the requested
> attribute doesn't exist in the node.
> 
> From the source:
> 	if (value == NULL) {
> 		RETURN_EMPTY_STRING();
> 	} else {
> 		RETVAL_STRING((char *)value, 1);
> 		xmlFree(value);
> 	}
> 
> Seems to me that it should return NULL. (That's what libxml does, apparently.)
> 
> Does anyone know of a particular reason it returns an empty string instead?
> Does anyone know of a particular reason it is not documented?
> (I'd be happy to document and provide the ridiculously simple patch of
> returning NULL...)

We match the behaviour the w3c defines for the DOM.

DOMString
The Attr value as a string, or the empty string if that attribute does not have a specified or default value.

Consider using  hasAttribute() if you care about the existence.

- Scott

---

## #52875 — david.zuelke@bitextender.com — 2011-06-03T22:16:49+00:00

URL: https://externals.io/message/52871#52875 · In reply to: #52871

> Hello,
> 
> I discovered today that the DOMNode::getAttribute() function (which is
> undocumented on the php site)

It's DOMElement::getAttribute()...

> returns an empty string if the requested
> attribute doesn't exist in the node.

Yes, but that's in line with the W3C DOM spec!

David
