1

I have two files, one called test3.php, and another called test4.php. I'm trying to echo the variable in the link of the file test4.php, but it's echoing unexpected results. Please take a look.

In the file called test3.php:

<?php

$text = "Good morning.";

header('Location:test4.php?text=$text');

?>

In the file called test4.php:

<?php

$text = $_GET['text'];

echo "$text";

?>

Expected echo result:

"Good morning."

Actual echo result:

$text

I don't understand why it's echoing out $text, instead of "Good morning." One thing that came to mind is that you can't actually set variables when you're using a header, so if that's the case please let me know. Thank you.

iscattered
  • 103
  • 1
  • 10
  • try `echo $text` via http://php.net/echo – hoss May 26 '15 at 01:39
  • 1
    basically this: http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php –  May 26 '15 at 01:39

3 Answers3

6

Variables do not get parsed in single quotes

header('Location:test4.php?text=$text');

therefore, you need to use double quotes

header("Location:test4.php?text=$text");

References:

Plus, it's best to add exit; after header, in order to stop further execution, should you have more code below that (or decide to in the future).

and using a full http:// call, as per the manual

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Footnotes, about header, and as per the manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.


  • However you wrote, and I'm using this literally:

Expected echo result:

"Good morning."

If you want to echo just that "Good morning." having the text in double quotes, then you will need to change the following in your test4.php file:

echo "$text";

to, and escaping the " using \

echo "\"$text\"";
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You always structure your questions so perfectly, I'm going to call your professor Fred from now on :-) (*ps, great answer!*) – Darren May 29 '15 at 00:39
2

use

header("Location:test4.php?text=".$text);
Suman Singh
  • 1,379
  • 12
  • 20
-1

In test4.php:

<?php

$text = $_GET['text'];

echo "$text";

?>

When you quote "$text", you are echoing af string. What you will want to do, is echo the variable: $text.

So:

<?php

$text = $_GET['text'];

echo $text;

 ?>

...Without the quotes.. :)

And also, the: header('Location:test4.php?text=$text'); is a bitch, if you use it below a lot of code...

Safe yourself some trouble, and use:

echo "<script type='text/javascript'>window.location.href = 'test4.php?text=".$text."';</script>";

instead ;)

Ziltoid
  • 35
  • 1
  • 6