Page 1 of 1

variable

Posted: Sat Mar 15, 2008 8:45 am
by patty07
Can anyone tell me why this variable is not working in this code ?

Code: Select all

 
 
$ROOT="http://www.mysite.com/new";
 
$rating=$rating."<img alt='$imrating' src='$ROOT/images/starn.gif'>";
 
 
$ROOT is not working as a variable in the rating line.

Now I can hardcode it and it works, but I need the variable to work in that line.
ROOT need to work because I am using basically fake directorys, for search friendly urls.
So ROOT variable is important to work.

I tried ".$ROOT." and also with a single qoute as well, so if you know what I am doing wrong please help.

Re: variable

Posted: Sat Mar 15, 2008 8:59 am
by Chalks
patty07 wrote:

Code: Select all

$ROOT="http://www.mysite.com/new";
 
$rating=$rating."<img alt='$imrating' src='$ROOT/images/starn.gif'>";
->

Code: Select all

$ROOT="http://www.mysite.com/new";
 
$rating = $rating . "<img alt=\"$imrating\" src=\"" . $ROOT . "/images/starn.gif\">";
That's how I would do it.

Re: variable

Posted: Sat Mar 15, 2008 9:06 am
by patty07
I tried that, and it isnt working, it seems to miss the /new/ directory, which means it isnt working.

$ROOT="http://www.mysite.com/new";

And what actually is showing is :

http://www.mysite.com/images/starn.gif

So $ROOT varuable still isnt working in that line.

Re: variable

Posted: Sat Mar 15, 2008 9:16 am
by Chalks
patty07 wrote:I tried that, and it isnt working, it seems to miss the /new/ directory, which means it isnt working.
That's odd. Echo out $ROOT and echo $rating. Make sure that $ROOT is a global variable (if applicable, I'm assuming it is, since all caps).

Re: variable

Posted: Sat Mar 15, 2008 9:32 am
by patty07
Ended up doing this which worked:

Code: Select all

 
define("root", "http://www.mysite.com/new", false);
$rating=$rating."<img alt='$imrating' src='".root."/images/starn.gif'>";
 

Re: variable

Posted: Sat Mar 15, 2008 10:14 am
by Verminox
It seems like $ROOT is getting unset somewhere or is out of scope. Are you using it in your code just the way youve posted? Or are the two lines in different parts of the code? A lilekly mistake is that $ROOT is global, while the $rating modification is done inside a function. In that case you would need to use the 'global' keyword to get $ROOT into scope.

Code: Select all

<?php
$ROOT="http://www.mysite.com/new";
function foo()
{
    global $ROOT;
        // ...
    $rating=$rating."<img alt='$imrating' src='$ROOT/images/starn.gif'>";
        // ...
}
foo();
?>