Forum Moderators: coopster

Message Too Old, No Replies

how to echo a variable before it is defined?

defined variable before it is defined

         

chikooo

1:54 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



any body knows how to display a variable before it is defined?

like this

<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php $title = "Music Wallpapers"; print $title; ?>
</body>

penders

3:08 pm on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



any body knows how to display a variable before it is defined?

You can't display a variable that is not yet defined! PHP files are parsed top to bottom, so it needs to be defined above where you want to use it. So, may be you can just change the order...

<?php $title = "Music Wallpapers"; ?> 
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php print $title; ?>
</body>

chikooo

4:32 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



what about if i do this?
<head><?php $title = "Music Wallpapers"; ?>
<?php include("http://www.example.com/index/source/modules/php/head_tag.php") ?>
</head>
<body>
<?php print $title; ?>
</body>

on the

include file is where i have the title tags . do u think it will work?

well it doesnt work for me any idea of how to do it

[edited by: eelixduppy at 5:15 pm (utc) on Feb. 19, 2009]
[edit reason] exemplified [/edit]

mvaz

4:45 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



Are you not missing the <title> </title> tags here?

Note: You shouldn't be posting urls in here.

chikooo

4:58 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



no the title tags are in the this file http://www.example.com/index/source/modules/php/head_tag.php

is like if it was thins way
<head><?php $title = "Music Wallpapers"; ?>
<title><?php print $title; ?> </title>
</head>
<body>
<?php print $title; ?>
</body>

but it doesnt work

[edited by: eelixduppy at 5:16 pm (utc) on Feb. 19, 2009]
[edit reason] exemplified [/edit]

chikooo

5:03 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



if i dont put the include file it works just by doing this
<head><?php $title = "Music Wallpapers"; ?>
<title><?php print $title; ?> </title>
</head>
<body>
<?php print $title; ?>
</body>

mvaz

5:06 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



You are missing the colon (;) in the include statement? which you posted initially.

[edited by: eelixduppy at 5:16 pm (utc) on Feb. 19, 2009]
[edit reason] disabled smileys [/edit]

penders

5:07 pm on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does that last example work for you? (ie. when you don't include the external file) ... it should.

Likewise, it should also work by including an external file. But I would guess there is something wrong with your head_tag.php file if it is not?

By including it the way you are, it would need to be something like:

<?php 
echo '<title>'.$title.'</title>';
?>

penders

5:09 pm on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are missing the colon (;) in the include statement? which you posted initially.

The semi-colon (statement separator) is not reqd at the end of the include since there is only 1 statement.

[edited by: eelixduppy at 5:16 pm (utc) on Feb. 19, 2009]
[edit reason] disabled smileys [/edit]

chikooo

5:12 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



what might be the proble with the head_tag.php

<?php
echo '<title>'.$title.'</title>';
?> what is this? so i would have to put this on the head_tag.php file
instead of this

<title><?php echo $title; ?> </title>

?

sonjay

6:04 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



If you include the file via the filesystem, rather than via http, the $title variable will be within the scope of the included file.

I.e, don't do it like this:
<?php include("http://www.example.com/index/source/modules/php/head_tag.php") ?>

Rather, do it like this:
<?php include("head_tag.php") ?>

chikooo

6:08 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



i cant because the include file is not there is on another folder a up folder.
i think that i cannot call a variable from include file

sonjay

6:17 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



Well, you have to use the correct path to the include file, yes. Maybe it would be <?php include '../head-tag.php'; ?> or maybe it would be something like <?php include ../../../source/modules/php/head_tag.php'; ?> You know where your files are in relation to each other; we don't.

Including the file via the filesystem and not through an http call will allow your $title variable to work. It's on you to figure out the location of the file on your system.

chikooo

8:14 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



this is the path of the file i want the index on.
http://www.example.com/wallpapers/index.php
and this is the one of the include file
[example...]

i think i cannot do it the way you say

leadegroot

9:01 pm on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An include statement doesn't work with a 'http' on the front of the param.
It isn't grabbing a web document, its grabbing a file form the server and has no idea what this 'http' thing is.
You want something like:

<?php include $_SERVER['DOCUMENT_ROOT'].'/source/modules/php/head_tag.php'; ?>

(I can never remember if a slash is needed on the front of the following string or not...)

Are you familiar with the values available in the _SERVER variable?
Dump phpinfo() to see - [php.net...]
Very useful...

chikooo

11:47 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



<?php include $_SERVER['DOCUMENT_ROOT'].'/index/source/modules/php/head_tag.php'; ?> it is needed i tested it and it woked when i took the slash out it didnt work.
but using this code that you game worked just fine the way i wanted it to.
thanks man
you r really good
thanks a lot