开发者

Parse error: syntax error, unexpected T_OBJECT_OPERATOR

I'm getting the following error when trying t开发者_高级运维o add some data from myXml.xml to a string: Parse error: syntax error, unexpected T_OBJECT_OPERATOR.

    $xmlstr = file_get_contents('myXml.xml');
    $xml = new SimpleXMLElement($xmlstr); 

    foreach($xml->order as $order){
            $replace = array();
            $firstName = (string) $order->billing-address->first-name;
            $lastName = (string) $order->billing-address->last-name;
    }

I can't provide my XML directly as it contains sensitive data.

Thanks, Sam


The - sign means subtraction. To use it in property names, you must use this syntax:

$firstName = (string) $order->{"billing-address"}->{"first-name"};
$lastName = (string) $order->{"billing-address"}->{"last-name"};

In general, it's probably better to use firstName, billingAddress, etc. as property names to avoid this. See CamelCase. In this case, however, you may have no control over the the XML input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜