Forum Moderators: coopster

Message Too Old, No Replies

echo variable's value instead of variable's name

newbie needs help

         

nmaus

1:02 am on Oct 8, 2004 (gmt 0)

10+ Year Member



Hello all,

I'm unsure how to do this - I know it can be done, so I'm hoping someone here can help me.

I have a PHP based file (page1.php) In there I have a link to an include file that makes up the menu system of the page. No problem so far.

Also in there is another include file (header.inc or header.php I'm not sure which is best to use for this - I have tried both .inc and .php here and both produce the same result) that references the header information. These two include files are on all pages of the site - allowing me to easily change one file to reflect menu or header changes on all pages. Great so far no problem.

However. In the header.inc file is a page title. Each page that is to come up in the browser is to have a page title. For example page1.php has a page title of Page 1 in bold letters at the top of the page. This tells the viewer what page they are on. Likewise page2.php has a title of Page 2, etc. (This is in the page itself not at the top of the menu bar of the browser window)

This data is in the header.inc file. Therefore I need to dynamically change that section of the include file to reflect the current page.

So what I have in the page1.php is the following:


<html>
<head>
<title>Some sort of page title here</title>
<link rel="stylesheet" href="includes/style.css" type="text/css" />
<?
// Name of this page
$pgname = 'Put the page name here';
?>
</head>
<body bgcolor="#ffffff" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<?
include("includes/header.inc");
?>
...snip...

Now, where the header.inc file is the header information is displayed on the generated web page. In the include file I have the following:

...snip...
<tr>
<td bgcolor="#FFFFFF" valign="top" height="40" width="19"><img src="images/layout/Trans.gif" width="1" height="1" border="0" alt></td>
<td valign="middle" align="center" nowrap class="bah" height="40"><? echo '.$pgname.';?></td>
<td valign="top" height="40" align="right" nowrap colspan="2">&nbsp;<a href="http://www.url.com/" target="_blank"><img src="images/pic/logo.jpg" width="200" height="40" alt="alt text" border="0"></a></td>
</tr>
...snip...

Notice the <? echo '.$pgname.';?> part.

I thought (obviously wrongly) that this would then generate the name of the page as indicated in the first part. But the output is simply in big bold letters:


.$pgname.

When it should show:

Put the page name here

So, what have I done wrong? The ideal is to have

<?
// Name of this page
$pgname = 'Put the page name here';
?>

on every page with the relavant page name listed, so that when the page is being generated by the server, it adds the include file and displays the correct page name.

I thought maybe because it's calling an include file that it can't display the page name in that way - that perhaps it need to be in the main file. The problem with that is that the page name is literally in the middle of the header - which I want to be referenced from an include file.

Any and all help much appreciated.

coho75

1:43 am on Oct 8, 2004 (gmt 0)

10+ Year Member



nmaus,

Also quite new to PHP myself.

Try something like:

<?=$pgname?>

This is what I use in my pages and it seems to work. Good luck.

Put a space before?> -- the system took my space out for some reason.

coho75

nmaus

2:07 am on Oct 8, 2004 (gmt 0)

10+ Year Member



Thanks for the tip.

Unfortuantely it just shows:


$pgname

instead of the bit it should.

Thanks again though :)

coho75

3:23 am on Oct 8, 2004 (gmt 0)

10+ Year Member



I just tried your code with <?=$pgname?> and it worked for me. Not sure why it is not working for you. Did you replace <? echo '.$pgname.';?> with <?=$pgname?>?

nmaus

3:33 am on Oct 8, 2004 (gmt 0)

10+ Year Member



Actually no. I thought you meant take the .'s out.

When I changed it to <?=$pgname?> it worked!

Thank you so much for this!

Legend!

It's probably simple/basic for most but like I said I'm new :)

mincklerstraat

9:47 am on Oct 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note on this kind of solution to other newbies:
this will only work if you have the 'short open tag' enabled, which for newbies, probably isn't a good idea - since it involves setting a specific config thing, and if you ever move to a different host, this might not be available without doing stuff to your htaccess files, and will make it more difficult for you to share your code.

Instead of <?=$variable?>, it's probably better to do this the longer way:
<?php echo $variable?> - <?php is the 'long', 'more official' opening tag, echo is sort of like a function (only offically it's a language construct if we insist on splitting hairs) that outputs stuff to the browser (i.e., makes you see it on a page) and?> ends the php, makes the rest just get interpreted like ordinary HTML.

baertyp

8:49 am on Nov 10, 2004 (gmt 0)

10+ Year Member



Replace

<? echo '.$pgname.';?>

with

<? echo " $pgname ";?> or
<?php echo " " . $pgname . " ";?>

and there you go, echoing your value surrounded by one space either side.

You got the quoting wrong and single-quoted the variable. Single quotes dont evaluate variables inside them.

Regards
Markus