An undocumented way - and, IMO, a hack - of getting an element's value OR attributes
(but not both) without type-casting is to use the function `current()`:
<?php
$xml = new SimpleXMLElement(<<<'XML'
<foo>
<bar>1</bar>
<baz name="baz" />
<qux name="qux">3</qux>
</foo>
XML
);
var_dump([
'bar' => [
$xml->bar,
current($xml->bar),
],
'baz' => [
$xml->baz->attributes(),
current($xml->baz->attributes()),
],
'qux' => [
$xml->qux,
current($xml->qux),
$xml->qux->attributes(),
current($xml->qux->attributes()),
],
]);
?>
Results in the following output (re-formatted to shorten it):
```
array(3) {
'bar' => array(2) {
[0] => class SimpleXMLElement#2 (1) {
public ${0} => string(1) "1"
}
[1] => string(1) "1"
}
'baz' => array(2) {
[0] => class SimpleXMLElement#5 (1) {
public $@attributes => array(1) {
...
}
}
[1] => array(1) {
'name' => string(3) "baz"
}
}
'qux' => array(4) {
[0] => class SimpleXMLElement#6 (2) {
public $@attributes => array(1) {
...
}
public ${0} => string(1) "3"
}
[1] => array(1) {
'name' => string(3) "qux"
}
[2] => class SimpleXMLElement#7 (1) {
public $@attributes => array(1) {
...
}
}
[3] => array(1) {
'name' => string(3) "qux"
}
}
}
```
This apparently reads the element as an array and just returns the value of the first property, whether it be the attributes or the value. As seen in the third example, this is unreliable, especially if you're parsing XML from an outside source that may or may not have attributes on an element that you want the value of.
I certainly do NOT advise anyone to use this since it is undocumented and it is just a coincidence that it works, so it might break in the future without prior notice.