-1

In a php file I have the following

<script id="svg-xml" type="text/template">
    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <etc />
</script>

and <? in the beginning of XML declaration is rendered as the short form of the PHP opening tag. My solution is to make PHP echo the < sign and use it like <?='<'?>?xml version=.... Is there any other/better option out there that is more elegant?

[EDIT] I'm looking for an alternative PHP approach, if any. Also, I'm not planning to turn short tags off since I'm using many of them here and there in this page.

inhan
  • 7,394
  • 2
  • 24
  • 35

3 Answers3

7

Echo the whole line, which is more readable than you proposition.

<?php echo '<?xml version="1.0" standalone="no"?>';?>

You could also disable short tags in your configuration. (Obviously this is not a solution if you rely elsewhere on short tags.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • You're right, I don't want to disable short tags, and yes, your solution definitely seems more readable. But I was wondering if there's another approach than to make it echo certain part of that string. – inhan Sep 07 '13 at 18:59
  • +1 .. If you're considering PHP to be parsed as XML this is the best option. – Keval Domadia Sep 07 '13 at 19:05
1

Simply disable short open tags with the short_open_tag directive. The precise mechanism depends on how you run PHP.


Right, I've just seen your edit about not wanting to use this. Then, this question is becoming subjective since it depends on your idea of what more elegant means.

If using a variable/constant is more elegant:

<?php

define('XML_INIT', '<?xml');

?>
<script id="svg-xml" type="text/template">
    <?=XML_INIT?> version="1.0" standalone="no"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <etc />
</script>

If using an external file is more elegant:

<script id="svg-xml" type="text/template">
<?php readfile('./template.svg'); ?>
</script>

If using heredoc is more elegant:

<script id="svg-xml" type="text/template">
<?php echo <<<EOM
    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <etc />
EOM
?>
</script>
Community
  • 1
  • 1
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
-1

You seem to be focusing on making your PHP code as readable as possible. take a look at XHP, which is used internally at Facebook to make PHP more readable.

Here are a few example of valid XHP code (which is an extension of PHP so you can do anything you do in PHP there):

<?php
$href = 'http://www.facebook.com';
echo <a href={$href}>Facebook</a>;

Second line is not a string, but something you can echo directly in XHP. I don't see much point in copy paste doc samples from the site, if you find it interesting go ahead and give it a try. it takes time to get used to, but the result is much cleaner PHP code that is also more secure and less bug-prone.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • 2
    First, whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. Second, tthe answer is incorrect. The asker is not looking for a library to replace HTML. He only wants to output that single tag. – John Dvorak Sep 07 '13 at 19:03
  • You may be color blind, but the XHP text is indeed a link to the library. the author is trying to make his PHP look more readable, which is what this library is all about. – Omry Yadan Sep 07 '13 at 19:10
  • he isn't looking for a library to replace HTML. He's looking for a place to readably embed _one tag_ into a PHP page. Ad-hominem attacks won't change the fact, nor will they change the fact that this is a link-only answer. Note that technically, color blindness does not prevent one from distinguishing hyperlinks from the usual text. Note that I never disputed that you were linking to a library, only that the library was useful to the asker. – John Dvorak Sep 07 '13 at 19:19
  • @OmryYadan - Yes, it's obvious that your question is a link to a tool. That's the problem: there's no way to know what your answer is about until you follow the link. And even after having a quick look it isn't entirely clear how that applies to this specific problem. Stack Overflow wouldn't be as useful as it is if it was a mere link directory. – Álvaro González Sep 07 '13 at 19:21