-2
<?php 
    function A() {
        return "<?php echo \"some text\"; ?>";
    }
    echo A();
?>

I expected to see "some text" on the page, but i did not. So how do I make this code work? Thanks in advance.

UPD: I'm sorry for the not clear description. I'm going to insert a random number into the value of the input.

<?php 
 function print_form() {
    return "<form method=\"POST\">
    <input type=\"text\" name=\"code1\" value=\" <?php echo rand(0, 999999); ?> \" />
    </form>"; 
 }
    echo print_form(); ?>
4
  • Well... you are including <?php inside another <?php Commented Mar 16, 2015 at 17:03
  • 1
    you can use eval.... but at your own risk! Commented Mar 16, 2015 at 17:08
  • I think real question is "how do you insert a block of PHP code as the result of a function? I think you should consider using includes. php.net/manual/en/function.include.php Commented Mar 16, 2015 at 17:12
  • php.net/manual/en/language.operators.string.php Commented Mar 16, 2015 at 17:13

3 Answers 3

1

Concatenate the return of the function:

return "<form method=\"POST\">
<input type=\"text\" name=\"code1\" value=\"" . rand(0, 999999) . "\" />
</form>";
Sign up to request clarification or add additional context in comments.

Comments

1

mate you have to return the text not a PHP code:

<?php 
    function A() {
        return "some text";
    }
    echo A();
?>

2 Comments

this is not the answer according to the question. he wants to run the code inside the function and return the output. not just output the string.
@itachi Not really: I expected to see "some text" on the page
-1
<?php 
 function print_form() {
    return '<form method="POST">
    <input type="text" name="code1" value="'.rand(0, 999999).'" />
    </form>'; 
 }
    echo print_form(); ?>

you are returning, so just concatenent the value.

Comments

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.