0

I have this.php:

<?php

//some code

?>
Here goes the text that is plain text.
Whenever you use <?php in a .php file it precedes the php code that will be parsed and interpreted by php.

As you may have guessed, this file gives an error and refuses to run correctly. I don't want to use HTML entities as the result file isn't used as HTML. What are my options other than storing the string <?php in a variable?

10
  • If the result is not supposed to be used as HTML, then using <?php once for PHP code and once "plain" is ambiguous. Does whatever you're using the result as not support escape sequences of any sort? Commented Sep 10, 2014 at 14:23
  • @deceze, no just plain text, I'm generating PHP code from PHP itself. Commented Sep 10, 2014 at 14:28
  • 2
    Woah man, codeception! ;o) Commented Sep 10, 2014 at 14:30
  • 1
    @kavoir.com — Sounds like you'd be better off using something like memcached. Commented Sep 10, 2014 at 14:37
  • 1
    I concur. Generating code is virtually always a bad idea, with the only exception being something like scaffolding tools which a developer uses to get started quickly. A regular application shouldn't be generating new code on the fly, there's always a way to do without that. Commented Sep 10, 2014 at 14:39

1 Answer 1

4

Generate it programatically using a string literal.

<?php echo "<?php"; ?>
Sign up to request clarification or add additional context in comments.

6 Comments

I suppose then it's best to store it in a variable? As I'm generating PHP code from PHP and there will be more than one occasions to use <?php in plain text. I thought there were some native ways to do this, such as a backslash or something.
PHP has no special "Output the string <?php" mechanism, and plain text files have no way to escape the text, so there isn't a native way.
Heredocs might be what you are looking for. It is good for a long text without the trouble of escaping quotations or anything else.
Sidenote: Only Here goes the text that is plain text. Whenever you use will echo on screen. The complete Whenever you use <?php in a .php file it precedes the php code that will be parsed and interpreted by php. will only appear in the HTML source, whether that is OP's wish or not.
@DeeJay' — You can just put the big chuck outside the ?><?php and only go into that for echoing out the string <?php itself. More importantly, there is no other way (except variables, but they aren't significantly different).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.