Echo php identifiers as they are i.e. syntactically!
At a place in my PHP application, I need to echo PHP's Global variable's Syntactically, i.e. something of the sort
echo "$_POST['submit']";
But this ain't working. Can anyone please shed some light on it?
The actual piece of code is:
echo "<?php if(isset($_POST['submit'])) echo 'value="'. $_POST['{$column['Field']}'] .'"'; ?>";
This is not correct:
<?php if(isset($_POST['submit'])) echo 'value="'. $_POST['{$column['Field']}']
switch to this:
<?php
if(isset($_POST['submit']))
echo 'value="'. $_POST[$column['Field']];
// or
echo 'value="'. $_POST['submit'].'"';
?>
whichever is the one you need
UPDATE: from the comment.
echo <<<HERE_DOC
<?php if(isset($_POST['submit']))
echo 'value="'. $_POST['{$column['Field']}'] .'"'; ?>';
HERE_DOC;
this will echo the all the string in between the heredoc string
Read More about the HEREDOC Syntax
Try using nowdoc
$str = <<<'EOD'
<?php if(isset($_POST['submit'])) echo 'value="'. $_POST['{$column['Field']}'] .'"'; ?>
EOD;
This is available from PHP 5.3 and it doesn't interpolate into string the variable value
to echo it simply do a:
echo $str;
or a
echo htmlentities($str);
When echoing something complex with lots of double and single quotes you should probably use nowdoc syntax: http://php.net/manual/en/language.types.string.php
Try something like:
echo <<<'STRING'
<?php if(isset($_POST['submit'])) echo 'value="'. $_POST['{$column['Field']}'] .'"'; ?>
STRING;
You can also do this with single quotes, but you need to escape all the single quotes in your string, like:
echo '<?php if(isset($_POST[\'submit\'])) echo \'value="\'. $_POST[\'{$column[\'Field\']}\'] .\'"\'; ?>';
Finally, if you're outputting to HTML you need to use htmlspecialchars()
to convert <
to <
, '>' to >
, and &
to &
.
echo htmlspecialchars(<<<'STRING'
<?php if(isset($_POST['submit'])) echo 'value="'. $_POST['{$column['Field']}'] .'"'; ?>
STRING
);
And:
echo htmlspecialchars('<?php if(isset($_POST[\'submit\'])) echo \'value="\'. $_POST[\'{$column[\'Field\']}\'] .\'"\'; ?>');
精彩评论