Re: Strange output from echo statement

From: 2metre (2metre_at_xxxhersham.net)
Date: 07/27/04


Date: Tue, 27 Jul 2004 11:44:36 +0000 (UTC)

ScareCrowe wrote:

> Whenever I try to print a string and a variable together I get incorrect
> output. After echoing the variable the rest of the line is ignored.
> //eg.
> $var1 = "test";
> echo "This is a " . $var1 . "! This is only a " . $var1;
> //returns:
> This is a test
> //instead of
> This is a test! This is only a test.
>

What version of PHP are you using?

I cut and pasted your code into a page (on a server running v4.3.4) and
it returned the full string as it should.

BTW if you are keeping the variables outside the strings, use single
quotes instead of double quotes - this will prevent PHP from having to
parse the contents of the string. Alternatively you could have written:

echo "This is a $var1! This is only a $var1";



Relevant Pages

  • Re: Quotes and passing vs. printing variables
    ... :>> used the quotes to tell php how to find the contents of the string. ... :>> read the source code, found the quotes, and knew where the string was. ... In the shell the value of a variables is converted into a string, ...
    (comp.lang.php)
  • Re: Quotes and passing vs. printing variables
    ... the passed string variable retains its quotes, ... > is _between_ the quotes. ... The quotes themselves are just how you tell php ... > then you are asking php to pass the _contents_ of the variables into the ...
    (comp.lang.php)
  • RE: [PHP] sloppy use of constants as strings. WAS: What does "<<<" mean?
    ... "A key may be either an integer or a string. ... heredoc, ... Those are raw PHP syntax strings, with no delimiters to set them off ... quotes and heredoc! ...
    (php.general)
  • Re: Quotes and passing vs. printing variables
    ... the passed string variable retains its quotes, ... is _between_ the quotes. ... The quotes themselves are just how you tell php ... then you are asking php to pass the _contents_ of the variables into the ...
    (comp.lang.php)
  • Re: Proper way to define? single, double or no quote marks?
    ... >> More specifically my question is whether to use single quotes, ... which of these is the proper method and what would the ... > When PHP finds a variable reference inside a "ed string it replaces the ...
    (alt.php)