Alternate string variable definition in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jkassemi
Forum Newbie
Posts: 5
Joined: Fri Dec 10, 2004 9:54 am
Location: USA

Alternate string variable definition in PHP?

Post by jkassemi »

Hello everyone. I'm trying to define a variable that contains a large amount of HTML code, but I want that HTML code to contain both " and ' characters. I defined a skeleton function that just prints the general page format with two variables, one for the page title and one for the page content, which is to be located in a center cell of a table.

I have code like that shown below on all of the pages, which defines the HTML content for the cell in the skeleton page. Whenever I run one of these pages, I get a parse error (at the <h2>You PARSE ERROR when I use a ' in the definition, or at one of the HTML tags when I use the " for definition.)

I was wondering if anyone knows a smarter way to do this, or perhaps some way to let php define variables with a character I'd rarely use in my html, such as :

$variable = ~Hey~;

Using frontpage, so I'm using <% %> tags, and have already turned that option in the php.ini to "1".

Code: Select all

<%

include "skeleton.php";//this includes the skeleton function.

//define the content of the page in HTML between these lines.
$pagecontent = '%>

<h2>You're one step closer to doing the right thing:</h2>




<div align="left">
	<table border="0" id="table1" width="500" bgcolor="#C0C0C0">
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
	</table>
</div>





<%';
//END define the content of the page in HTML between these lines.

$pagetitle = "Page title";



skeleton($pagetitle, $pagecontent);

%>
Any help is appreciated,

James.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i would really recomend not using asp style tags, use <?php ?>
when you put it on a server youll need to change that php.ini too, and you may not be able to.
so why not just use real php tags....

i see what your trying to do, but you cant do it that way.
1- you cannot drop out of php parsing mode if your inside quotes
2- if your not inside quotes, you cant capture the output(unless you use output buffering)

the error is because you did not escape the single quote

Code: Select all

<?php

$foo = 'some text'; // ok
$foo = 'some "more" text'; // ok
$foo = 'some 'more' text'; // parse error
$foo = "some 'more' text"; // ok
$foo = 'some ''more'' text'; // ok, because i escaped the quotes


but escaping everything can be a pain sometimes, and thats where heredoc syntax comes into play

$foo = <<<EOF
"quotes' and anything ..'':';>":.;  can be inside here now

$variables will be expanded as well


EOF;

?>
jkassemi
Forum Newbie
Posts: 5
Joined: Fri Dec 10, 2004 9:54 am
Location: USA

Post by jkassemi »

Is there a method that you would recommend for doing what I'm trying to do, or will I need to change the concept entirely?

Basically, I don't know whether or not this is the best way to define my html and place it in the table. I can't seem to get my design to match up without using one table, and I need the data inside that single cell. I'm fairly new at this, and although I understand most of the code, my exploration of the theory is limited.
npeelman
Forum Commoner
Posts: 32
Joined: Tue Jul 27, 2004 5:13 am
Location: Oviedo,FL.

Post by npeelman »

jkassemi wrote:Is there a method that you would recommend for doing what I'm trying to do, or will I need to change the concept entirely?

Basically, I don't know whether or not this is the best way to define my html and place it in the table. I can't seem to get my design to match up without using one table, and I need the data inside that single cell. I'm fairly new at this, and although I understand most of the code, my exploration of the theory is limited.
Do like the previous poster said;

Code: Select all

<?php
$pagecontent = <<<EOV
<h2>You're one step closer to doing the right thing:</h2>

<div align="left">
    <table border="0" id="table1" width="500" bgcolor="#C0C0C0">
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>
</div>
EOV;

?>
Will do just fine. You can replace the letters EOV with whatever you want. For me EOV = End Of Variable. Don't forget the ';' at the end of the EOV though.
jkassemi
Forum Newbie
Posts: 5
Joined: Fri Dec 10, 2004 9:54 am
Location: USA

Post by jkassemi »

Thanks for the help guys.
Post Reply