PHP frustration

Most of the code I’ve written in my life so far has been PHP, as long as I include all the small testing of different stuff just to see if it could be done.

PHP is quick to prototype stuff in, because you quickly get something you can poke around with, there’s little boilerplate code. However, specially when I’m just hacking together something in a hurry, theres one thing that’s really, really frustrating. Specially when I deal with SimpleXMLElements.

In Python you can do this:

?View Code PYTHON
firstbar=element.getElementsByTagName("bar")[]

to get the first bar element inside “element”, in Java, i can do pretty much the same, and even in javascript you could do this, but in PHP, you always have to assign the result of getElementsByTagName() to a variable before accessing it’s index.

<?
print $element->xpath("//bar")[]; // FAILS
$bars=$element->xpath("//bar");
print $bars[]; //WORKS
?>

Now of course, this is not the only thing that frustrates me with PHP, but this was the thing biting me yesterday.

EDIT:

I realize that the best way to do this is using the current() function in PHP:

<?
print $element->xpath("//bar")[]; // FAILS
print current($element->xpath("//bar")); // WORKS
?>