Forum Moderators: coopster

Message Too Old, No Replies

Newbie Question - Parsing Sequence

Will Variables Declared in An INclude be Used in HTML before Include constr

         

calvinmicklefinger

3:02 am on Jan 20, 2005 (gmt 0)

10+ Year Member



Unfamilair with PHP enough to ask what is probably a self evident question, but ...

If I use include to get a footer that declares variables which are used in the wrapper document, will the PHP parser loop through the HTML to ensure that all variables are used, or will they be ignored?

I'm betting ignored, but that's not what I want, so here's another question. Is there a way to get the information form the include to the top of the master document?

Many thanks.

jatar_k

4:46 am on Jan 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the key is to include the file before the vars are used.

calvinmicklefinger

1:13 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



That was what I figured, which leaves me trying to figure out how to construct my web pages to contain dynamic header information as well as content.

Right now I have one file with header, footer, sidebars and a single include in the center.

I would like to contain the header information such as page title, and meta descriptions, keywords, etc., in the same file as the content, but if the file is parsed in a linear mode, my current design makes it too late to use the include in the center.

I know diddly about using databases, so that is out, but one thought that comes to mind is ...

Would it be possible to put the include file at the head of the file, possibly just after the <HTML> tag, and have everything in it treated as a variable? Then, put it in place using said variables instead of includes later in the document?

How would you treat an entire section of formatted HTML as a variable?

coopster

3:12 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are a number of options. You could make each section a function and declare your variables in that function as globals:
function myList() 
{
global $name, $address, $phone;
$content =
"<ul>
<li>Name: $name</li>
<li>Address: $address</li>
<li>Phone: $phone</li>
</ul>";
return $content;
}
Then in your script:
. 
.
.
<?php
$name = 'Name';
$address = '123 Main Street';
$phone = '555-1212';
?>
<body>
<? php print myList(); ?>
</body>
.
.
.
Another way to parse variables is the eval() [php.net] function.

calvinmicklefinger

4:26 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



Thanks Coopster,

About midnight last night I was able to work out another way.

I put the include construct before the <HTML> tag and made everything in the include file a $var.

I declared a var in my include file/page (as in $var1 = " ...) and typed in a gazoodle of HTML, then in that same HTML, I was using vars from my main page that I needed to build dynamic links which were to be made whole as the pages were rendered.

All I had to remeber was to use single quotes where HTML tags required quotes, such as in all <a href = '... tags through my HTML.

Then on my main page, I called the $var1 where I would ordinarily have used an include construct.

When I called the $var1 on my main page, I had to remember to use the format ${var1} (with the curly barces) to insert the complex $var1 and it's nested vars.

New bucaneer laugh? Var, var, varrr.

Anyhoo, that got it working for me, and I was just getting back to the forum this morning.

BTW - I tried the eval() and couldn't get it to return anything, which I believe I discovered it didn't, how do it work?

Thanks for your help.

coopster

4:31 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



eval() evaluates the string given as PHP code. The manual pages explain it fairly well and there are examples there that you can grab and play around with to get a feel for how it works.

calvinmicklefinger

5:00 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



Thanks.

I tried reading the manual and googling for eval() last night. I'll need to work on it some more later.

Again, thanks.