Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to set a variable to some HT 2

Status
Not open for further replies.

hillbillyfritz

Technical User
Mar 28, 2001
34
US
I need to set a variable to some HTML. Can I do this without putting all of the code on one line?

Code:
$var = &quot;<html>\n<head>\n</head>\n<body>\n</body>\n</html>;

How do I make it more like this?

Code:
$var = &quot;<html>
<head>
</head>
<body>
</body>
</html>;

Thanks for the help.
 
it wont matter whethere it is on one line or different lines
unless it is inside double quotes

so

$var = &quot;<html>\n<head>\n</head>\n<body>\n</body>\n</html>&quot;;



cheers

spookie


 
it wont matter whethere it is on one line or different lines
unless it is inside double quotes

so

$var = &quot;<html>\n<head>\n</head>\n<body>\n</body>\n</html>&quot;;



cheers

spookie


 
You could also do this (using the concatenation operator):

-------------------------------
Code:
$var  = &quot;<HTML>\n&quot;;
$var .= &quot;<HEAD>\n&quot;;
$var .= &quot;</HEAD>\n&quot;;
$var .= &quot;<BODY>\n&quot;;
$var .= &quot;</BODY>\n&quot;;
$var .= &quot;</HTML>&quot;;
-------------------------------

or, without having to type all those \n's:

-------------------------------
Code:
$var  = &quot;<HTML>
<HEAD>
</HEAD>
<BODY>
</BODY>
</HTML>&quot;;
-------------------------------

Either way, when you do a
Code:
echo &quot;$var&quot;;
, you'll get the following:

-------------------------------
Code:
<HTML>
<HEAD>
</HEAD>
<BODY>
</BODY>
</HTML>
-------------------------------

Hope this helped clear things up for you! -gerrygerry
Go To
 
Actually, you can use multi-line strings, even with double quotes. The only problem with your initial example was that you were missing the closing quotes:

$var = &quot;<html>
<head>
</head>
<body>
</body>
</html>&quot;;

Even more useful for working with large strings, try the &quot;heredoc&quot; method, which just requires a marker at the beginning and end of the string, and let's you treat the rest of the string more naturally:

$mystring = <<<EOT

<html>
<head>
</head>
<body bgcolor=&quot;#F0F0F0&quot;>
...and here is a $variable, which will be parsed.
</body>
</html>

EOT;

Notice, you can include both variables, and single and double quotes as part of your string, without escaping them. The only thing you cannot have in your string is &quot;EOT;&quot; at the beginning of a line. (the marker can be named anything you want, although it is considered good practice to use all upper-case letters to distinguish it. You could use STRINGISINSIDE as a marker)

Spend some time reading -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
                          -unknown F student
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top