0

I want to generate a php page which contains HTML and some php commands. Problem is that when I submit the code below, I get this output

<?php echo '$stfromyearErr  '?><?php echo '$stfrommonthErr  '?><?php echo '$stfromdayErr  '?> <?php echo '$sttoyearErr  '?><?php echo '$sttomonthErr  '?><?php echo '$sttodayErr  '?>

rather than php commands. How do I fix this?? Thanks!!

<?php

$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='0' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo '\$stfromyearErr &nbsp;';?&gt&lt?php echo '\$stfrommonthErr
&nbsp;';?&gt&lt?php echo '\$stfromdayErr &nbsp;';?&gt &lt?php echo '\$sttoyearErr &nbsp;';?
&gt&lt?php echo '\$sttomonthErr &nbsp;';?&gt&lt?php echo '\$sttodayErr &nbsp;';?&gt</span></td>
</tr></table> </body></html>";  
echo $page; 
?>
user2038763
  • 51
  • 1
  • 7
  • 2
    Is your question why variable interpolation doesn't happen in single quotes? Or did you really intent to generate a HTML page with unprocessed PHP tags? – mario Sep 09 '14 at 19:43
  • I want to create a HTML page with unprocessed PHP tags.. thanks! – user2038763 Sep 09 '14 at 19:48
  • Is this the expected echo'd output? ` ` **using** `$stfromyearErr = "YEAR"; $stfrommonthErr = "MONTH"; $stfromdayErr = "DAY"; $stfromdayErr = "TO YEAR"; $sttomonthErr = "TO MONTH"; $sttodayErr = "TO DAY";` as test variables. – Funk Forty Niner Sep 09 '14 at 19:53
  • I have no idea what you're trying to achieve, so I can't give you a definite answer. – Funk Forty Niner Sep 09 '14 at 19:59
  • Hi Fred, What I want is to dynamically create a form inside a php (e.g dates.php) which will be then displayed to my client who would then populate it... this date.php would then validade the form and display any errors via the $...Err variables. – user2038763 Sep 09 '14 at 20:02
  • So would not appear at all when dates.php was displayed. But an ERROR MESSAGE would appear AFTER the form on dates.php, if I set $stfromyearErr = "blah blah"; in my validation routine. – user2038763 Sep 09 '14 at 20:06
  • You could probably use an output buffer instead of going through all that trouble. See this on Stack http://stackoverflow.com/questions/2832010/what-is-output-buffering – Funk Forty Niner Sep 09 '14 at 20:06
  • Thanks Fred.. I'll see if it works for what I want.. thanks!! – user2038763 Sep 09 '14 at 20:20
  • You're welcome. To call out my name personally, add the `@` followed by my name. – Funk Forty Niner Sep 09 '14 at 20:21

4 Answers4

1

You're echoing out your PHP tags as &lt;?php, and then presumably displaying that in a browser. It will LOOK like php code in a browser, because the browsers will render &lt; as < but it's NOT PHP code. it's just some text.

PHP is not recursively executable, e.g.

<?php
   echo "<?php echo 'foo '; ?>";
?>

would echo out <, ?, p, etc..., not just foo.

You can do stuff like

<?php

    $foo = "<?php echo 'hello world!'; ?>";
    file_put_contents('hello.php', $foo);
?>

without any issues. As long as the file you're producing actually gets executed by PHP (e.g. don't name it "hello.html"), then PHP will not know (or even care) that the script was produced by some OTHER php code.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • The interesting thing is that if I use echo it generates the php but it still shows   and doesn't treat this piece of code as php(but the rest of the php code on the same page is invisible as we expect). However if I use file_put_contents, the generated php works ok. No idea why!! – user2038763 Sep 09 '14 at 21:18
  • why should it execute? PHP code is just text. You're echoing some text that happens to LOOK like php code. "echo" doesn't execute anything, so any php code which you echo out stays as plaintext. stuffing that php code into a file, and then feeding it back into PHP DOES execute it. – Marc B Sep 09 '14 at 21:57
0

You are encapsulating your variables in single-quotes, therefore they get output as plain text, simply remove the commas on any of your echo statements to fix this.

<?php echo $stfromyearErr; ?>
Halfpint
  • 3,967
  • 9
  • 50
  • 92
0

When you want to output PHP code including tag and even syntax highlighting, you can use the function highlight_string(). Documentation here

<?php
highlight_string("<?php echo '$stfromyearErr  '?><?php echo '$stfrommonthErr  '?><?php echo '$stfromdayErr  '?> <?php echo '$sttoyearErr  '?><?php echo '$sttomonthErr  '?><?php echo '$sttodayErr  '?>");
?>
Mic1780
  • 1,774
  • 9
  • 23
0

There are two things, first remove the quotes:

<?php

$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='1' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo $stfromyearErr &nbsp;?&gt&lt?php echo $stfrommonthErr
&nbsp;?&gt&lt?php echo $stfromdayErr &nbsp;?&gt &lt?php echo $sttoyearErr &nbsp;?
&gt&lt?php echo $sttomonthErr &nbsp;?&gt&lt?php echo $sttodayErr &nbsp;?&gt</span></td>
</tr></table> </body></html>";
echo $page;

?>

However, this will give you:

<?php echo your_var_1  ?><?php echo your_var_2  ?><?php echo your_var_3 ?> <?php echo your_var_4  ? ><?php echo your_var_5  ?><?php echo your_var_6  ?>

This is because the php tags are redundant: you have already started php and are echoing these out within php.

If this is unavoidable, try replacing them like this:

echo str_replace("&nbsp;?&gt", "", str_replace("&lt?php echo", "", $page));
sherwoor
  • 185
  • 6