Jump to content

Working with XML in PHP


SecondCity

Recommended Posts

When you work with XML in a PHP script, are you allowed to have carriage returns in the code to make it more readable?

 

My IDE is complaining about a missing ending or something...

 

<?php //create a template for the XML that's sent as the transaction request$xmlStr = <<<XML<?xml version="1.0" encoding="utf-8"?><document><title>Forty What?</title><from>Joe</from><to>Jane</to><body>I know that's the answer -- but what's the question?</body></document>XML; $xml = simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOWARNING); print_r($xml);?>

 

 

For some reason my formatting from my IDE isn't carrying over, but suffice it to say that I have carriage returns and tabs in my code to make it easier to read...

 

Edited by SecondCity
Link to comment
Share on other sites

Yes you can have whitespace, but if you put it in the XML then you're altering the data.

 

Did you put whitespace before the "XML;"? That's one thing you cannot do: the ending to a heredoc/nowdoc must be the only thing on its line, except optionally a semicolon if it forms the end of a statement (like yours does).

Link to comment
Share on other sites

Yes you can have whitespace, but if you put it in the XML then you're altering the data.

 

Did you put whitespace before the "XML;"? That's one thing you cannot do: the ending to a heredoc/nowdoc must be the only thing on its line, except optionally a semicolon if it forms the end of a statement (like yours does).

 

That seems to be the issue.

 

I needed this on one line...

$xmlStr = <<<XML

then my xml

 

then this on the last line

 

 

XML;
Edited by SecondCity
Link to comment
Share on other sites

Some side questions related to this thread...

 

1.) Why is spacing lost when I use the [ code ] and [ php ] tags?

 

Usually bbcode preserves spacing.

 

 

2.) Why was my var_dump stripped from my reply?

 

I included it in [ code ] and then [ quote ] and each time it was stripped out.

 

 

3.) When is it when I "liked" Requenix's response it said i had met my daily quota when I have never liked anyone before?

 

 

 

Link to comment
Share on other sites

1. Try using Paste as Plain Text with this editor (it's one of the paste buttons).

2. The forum won't "strip" a var_dump(). Are you sure you copied it? Or maybe you mean print_r()? Because that is in your post...

3. It's a limitation on new users. I've now modified it so you should be able to like a couple posts a day. But if you hit that quota again, don't worry about it.

Link to comment
Share on other sites

1. Try using Paste as Plain Text with this editor (it's one of the paste buttons).

2. The forum won't "strip" a var_dump(). Are you sure you copied it? Or maybe you mean print_r()? Because that is in your post...

3. It's a limitation on new users. I've now modified it so you should be able to like a couple posts a day. But if you hit that quota again, don't worry about it.

 

Requinix,

 

In Post #4 I tried twice to copy and paste the results of my var_dump and when I hit submit (or whatever) the results were stripped out.

 

In you look in any of my posts in this thread you will see no print_r or var_dump results.

 

That is both strange and annoying, because I was trying to show you something to ask a follow up question.

 

I can't imagine that either the code of a var_dump or the results would do any harm to this forum.

 

Then again, I have noticed some weird behavior on this website that I haven't encountered before with other forums - especially those using bbcode.

 

Just sayin...

Link to comment
Share on other sites

print_r results? I'm not sure what to say: I've never heard of anyone having this problem before. And I know the forum doesn't remove anything from posts. What other "weird behavior" are you seeing?

 

I found this code yesterday online in the PHP manual...

 

<?php$xmlStr = <<<XML<?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>XML; $xml = simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOWARNING); print_r($xml); var_dump($xml);
?>

 

 

When I tried to copy and paste the print_r and var_dump results yesterday into my post every time this website stripped the results out.

 

Let's see what happens today...

 

print_r results:

 

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )

 

 

var_dump results:

 

object(SimpleXMLElement)[1]
public 'to' => string 'Tove' (length=4)
public 'from' => string 'Jani' (length=4)
public 'heading' => string 'Reminder' (length=

public 'body' => string 'Don't forget me this weekend!' (length=29)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.