-2

I need to display the contents of my program at the end of the webpage for an assignment, but it just keeps executing the PHP instead of displaying it. I was trying to use <xmp></xmp>, but I guess that only works for HTML code. Is there any way for me to display PHP code in a webpage without having to do escapes across the entire program? Surely that's not the only way.

Edit: I figured it out. Just needed to replace the <?php with &lt;?php. Thanks for the classic snarky responses, though.

Sam
  • 193
  • 3
  • 11
  • 1
    How do you expect PHP to figure out which exact code blocks need to be executed? – Álvaro González Nov 30 '20 at 10:44
  • 2
    Well, that would be why I'm asking. – Sam Nov 30 '20 at 10:45
  • Does this answer your question? [HTML,PHP - Escape '<' and '>' symbols while echoing](https://stackoverflow.com/questions/10551116/html-php-escape-and-symbols-while-echoing) – TidyDev Nov 30 '20 at 10:46
  • If you want to execute a PHP script where part of the code is not meant to be code, then you need to include as text literal, there's no other way. It can be automated in any conceivable way but that's how it works. – Álvaro González Nov 30 '20 at 10:47
  • How exactly are you doing it right now? PHP doesn't care about `` tags. That's HTML and for the browser. `<xmp><?php ... ?>` will still execute the PHP code. You need `&lt;?php ...` to output the text " – deceze Nov 30 '20 at 10:49
  • @TidyDev Somewhat, but then I'd have to echo every single line of the program. Professor wants us to print an exact copy of the program at the end of the webpage. Why I can't just submit the files separately, I do not know. – Sam Nov 30 '20 at 10:49
  • It's also worth nothing that this is asked as an HTML question rather than a PHP question. Even a PHP-enabled server should be able to display static HTML files if configured properly—you still need to escape your code, though, to make literal `<` show up correctly. – Álvaro González Nov 30 '20 at 10:57
  • About your edit... You say the solution was to escape the code, which is what we already told you in our "snarky responses", but the question specifically required "without having to do escapes". It's all like: "How can I X without Y?" "You cannot, you need Y". "Thank you for nothing, I figured out myself without any help that all I need is Y". Anyway, glad you got it sorted out. – Álvaro González Dec 01 '20 at 07:44

2 Answers2

2

Just use file_get_contents to create a php string with your code and echo it using htmlentities.

<?php

$code = file_get_contents('php_file.php');

?>

<h1>My essay</h1>
<pre><?php echo htmlentities($code); ?></pre>
anpel
  • 931
  • 6
  • 16
0

Is there any way for me to display PHP code in a webpage [...] ?

Yes, there is.

You can add the PHP lines to any element in HTML (e.g. <code>) if you use mini angle brackets or single guillemets ( and ) as placeholders for the standard angle brackets (< and >).

You can then use javascript to reconvert the mini angle brackets into standard angle brackets after the DOM Content has loaded.


Working Example:

const myPHPCodeBlocks = document.querySelectorAll('code.php');

for (myPHPCodeBlock of myPHPCodeBlocks) {
  
  let myPHPCode = myPHPCodeBlock.textContent;
  myPHPCode = myPHPCode.replace(/‹/g, "<");
  myPHPCode = myPHPCode.replace(/›/g, ">");
  myPHPCodeBlock.textContent = myPHPCode;
}
code {
  display: block;
  color: rgb(0, 0, 191);
  background-color: rgb(239, 239, 239);
  border: 12px solid rgb(239, 239, 239);
  border-top: none;
  white-space: pre;
}
<code class="php">
‹?php

echo 'Hi, I\'m PHP!';

for ($i = 0; $i ‹ 10; $i++) {

  echo '‹p›' . $i . '‹/p›';
}

?›
</code>

<p>I&#39;m HTML</p>

<code class="php">
‹?php

echo '‹p›I\'m PHP again!‹/p›';

?›
</code>
Rounin
  • 27,134
  • 9
  • 83
  • 108