variable

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
patty07
Forum Newbie
Posts: 8
Joined: Sat Feb 23, 2008 2:14 am

variable

Post 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.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: variable

Post 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.
patty07
Forum Newbie
Posts: 8
Joined: Sat Feb 23, 2008 2:14 am

Re: variable

Post 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.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: variable

Post 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).
patty07
Forum Newbie
Posts: 8
Joined: Sat Feb 23, 2008 2:14 am

Re: variable

Post 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'>";
 
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: variable

Post 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();
?>
Post Reply