how to echo php variable in a header direct

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
tate
Forum Newbie
Posts: 5
Joined: Thu Nov 27, 2008 10:09 am

how to echo php variable in a header direct

Post by tate »

Hi

if I have a variable
say
$sausage = something
How do I put that variable in the header direct
i.e
header("Location: http://mysite.com/?tid=$sausage");
does not work.

How do I do it???

Thanks.

Tate.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to echo php variable in a header direct

Post by John Cartwright »

Variables are parsed within double quotes, so it actually is working. I would suspect the value of $sausage is not what you expect.

Alternatively, you can escape the quotes or use single quotes.

Code: Select all

 
header("Location: http://mysite.com/?tid=$sausage");
 
header("Location: http://mysite.com/?tid=". $sausage);
 
header('Location: http://mysite.com/?tid='. $sausage);
Are all perfectly legit. Try var_dump($sausage); before the header call to see what the value of $sausage is.
tate
Forum Newbie
Posts: 5
Joined: Thu Nov 27, 2008 10:09 am

Re: how to echo php variable in a header direct

Post by tate »

Thats great!

It works!! Thanks!

If I wanted it to open a new webpage, so my site site still remains, how do I do that???
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to echo php variable in a header direct

Post by John Cartwright »

tate wrote:Thats great!

It works!! Thanks!

If I wanted it to open a new webpage, so my site site still remains, how do I do that???
You will have to use html/javascript. A header redirect has no control of the client.
Post Reply