0

Basically what i mean is i want to echo a piece of a php using a php method code below is getting errors

<?php
    $myString = '<?= $_GET['content'] ?>'; 
    echo $myString;
?>

maybe theres a javascript alternative?

full code below

<?php

if (in_array($_GET['content'], array(tree, 3, 4, 5)) ) {
$myString = "<?= $_GET['content'] ?>"; 
$myParsedString = htmlentities($myString); 
echo $myParsedString; 
}
else {

    echo "nothing to see here";

}

?>
4
  • 3
    @Gabe Are you serious? No do not even consider eval() for this. You need to consider why you would want to echo out PHP code instead of creating code which produces the output. In what context would you actually need to create PHP code instead of creating output? Commented Dec 7, 2014 at 14:07
  • So really what would help us answer your question is an explanation of the kind of output you want to achieve and where you want to use it. All PHP code is executed on the server before it is sent down to the browser, so you can't effectively send an PHP code down with the HTML output. Commented Dec 7, 2014 at 14:07
  • why do you enclose GET in tags and quotes? Commented Dec 7, 2014 at 14:10
  • The first issue with your code is that you didn't escaped the colons, '<?= $_GET['content'] ?>'; should be '<?= $_GET[\'content\'] ?>'; Commented Dec 7, 2014 at 14:16

4 Answers 4

2

you can directly access $_GET inside <?php ?>no need to use tag again inside :try this.

$myString = $_GET['content']; 
Sign up to request clarification or add additional context in comments.

Comments

1

This should work for you:

if ( in_array($_GET['content'], array("tree", 3, 4, 5)) ) {
    $myString = $_GET['content']; 
    echo $myParsedString = htmlentities($myString); 
} else {
    echo "nothing to see here";
}

Comments

0

No need to use another PHP tag inside PHP tag. Simply can try this

<?php
if (in_array($_GET['content'], array('tree', 3, 4, 5)) ) {
    $myString = $_GET['content']; 
    $myParsedString = htmlentities($myString); 
    echo $myParsedString; 
}else {
    echo "nothing to see here";
}
?>

Comments

-1

Just escape the PHP code you get and then u can echo it

$myString = mysql_real_escape_string('<?= $_GET['content'] ?>'); 
echo $myString;

I hope that helps

3 Comments

No. No no no. Why would you use a MySQL escape function to display to the browser?
Because he is asking to display "<?= $_GET['content'] ?>" to the end user on webpage, so it needs to be escaped.
Yes, and MySQL escaping does nothing useful for the end user on a webpage. htmlspecialchars is for browser, and already used in the OP's code.

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.