tags:

views:

90

answers:

7

Can this be done - opening a variable with one PHP tag, then closing the PHP tag but keeping the variable open so everything beneath becomes the value of the variable? Or is there a limit on PHP variable size / characters?

<?php $content = " ?>

a bunch of content goes here <br />
with lots of HTML tags and JS scripts

<?php "; ?>
+3  A: 

Read bout HEREDOC syntax: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Tomasz Kowalczyk
if you are satisfied with my answer, please accept ;]
Tomasz Kowalczyk
oh, thanks for apprectating a little bit ;]
Tomasz Kowalczyk
+2  A: 

You can either use HEREDOC/NOWDOC

$content = <<< 'HTML'

a bunch of content goes here <br />
with lots of HTML tags and JS scripts

HTML;

or output buffering, e.g.

<?php ob_start(); ?>

foo

<?php
    $var = ob_get_clean();
    var_dump($var); // will contain foo and surrounding whitespace
Gordon
+2  A: 

No, but you can probably do some of it with heredoc

$content = <<< END
some content here<br/>
<script type="text/javascript">
alert('hi');
</script>

END;
Fanis
NO is the correct answer.
Elzo Valugi
A: 

try:

<?php ob_start (); ?>

.... html

<? 
$content = ob_get_clean (); 
?>

See http://pl.php.net/manual/en/book.outcontrol.php for details

Piotr Pankowski
this works, but in most cases it's better to use the HEREDOC method.
Spudley
A: 

Yes you can do it.

A closing tag inside double quotes as : " ?>" will not be treated specially. They are just string contents.

is there a limit on PHP variable size / characters?

No. You can stuff as much as you can into a variable till your memory is full.

codaddict
that's taking the code literal, but I don't think it's what the OP wants to do, is it?
Gordon
Its not very clear to me. This is what I inferred.
codaddict
+3  A: 

What your code would do is to store a string starting with ?> and ending with <?php in the variable $content. That's probably not what you want to do? If you later echo such a string, you would most probably get errors due to these php tags.

As mentioned in other answers, heredoc would be a solution but in general you should try to avoid such situations where you have to store very long html sequences in a variable. Rather use a view file and inject some dynamic content there or use some sort of include.

So, depending on what you really want to do,your options are:

  1. heredoc
  2. $content = "<html>markup here</html>";
  3. via output buffering
  4. using a view (look for info about the MVC pattern, you can also just do VC for a start)
  5. using includes
tharkun
1 and 3 could work. I want to avoid 2. Could you point me to a good tutorial on using a view?
Adam Hunter Peck
CodeIgniter is a very good and light framework if you're getting started with MVC.
DMin
+1  A: 

$content = 'large amount of text';
or
$content = 'text';
$content .= 'other text';
$content .= 'end text';

werd