Search code examples
phpxmlxpathsimplexml

Increase Integer in SimpleXML XPath variable doesn't work


I'm creating a PDF-View-Counter.

Now I'm stuck at changing the "view" value from the XML. When I'm trying to sum the variable ($VAR++), it doesn't work. What should I do?

<?php

$datei = "pdf.pdf";

$xmldb = "db.xml";

$id = md5($datei);

$xml = simplexml_load_file($xmldb);

$views = $xml->xpath('/data/count[@id="'.$id.'"]/views');

$num = $views[0][0];

$num++;

echo $num; // it doesn't work 

XML-Code:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <count id="883c7046854e04138c55680ffde90a61">
        <filename>s</filename>
        <views>1</views>
        <lastview>f</lastview>
    </count>
</data>

Solution

  • I guess you must cast the resulting object into integer.

    As far as I know SimpleXML->xpath() always returns an array with zero or more SimpleXMLElement Objects.

    $num = (int) $views[0];
    $num++;