0

I want to put some procedural stuff like html in a variable;

Normally we do this:

<?php

$my_var = '

    <h1>some header</h1>

    some text.

';

?>

Now I want it to look like this:

<?php

$my_var = '

?>

    <h1>some header</h1>

    some text.

<?php

';

?>

So that we open a php code, declare a variable, do some stuff outside php, and then reopen php code and close the var and we get that stuff that is outside php, we get it inside that variable.

Thanks.

1
  • 2
    I'm just curious, why would you want to do this? Commented Oct 26, 2012 at 19:54

3 Answers 3

6
<?php
ob_start();
?>
    <h1>some header</h1>
    some text.
<?php

$my_var = ob_get_contents();
ob_end_clean();

echo my_var;
?>
Sign up to request clarification or add additional context in comments.

2 Comments

You might also use $my_var = ob_get_clean(); instead of using ob_get_contents(); and ob_end_clean();. [source]
@HashemQolami ob_get_clean() is my preferred way too but I thought I would show the beginner method.
1

i don't think there is a way because php ignores lines outside the php tags. if u have the php in a other file user file_get_contents

1 Comment

ups i fogott to think about ob_start
1

Please check php heredoc

http://php.net/manual/en/language.types.string.php

Hope it helps

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.