开发者

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?

开发者_StackOverflow中文版

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 &lt;, '>' to &gt;, and & to &amp;.

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\']}\'] .\'"\'; ?>');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜