unwanted double-quote signs in echo output

Unwanted “signs output on a visitor’s last name! Why?

The name is defined once (without quote marks) …
L.122 If($TEST){$last_name = Longfellow;} //with or without quotes on Longfellow

The first output of last_name occurs in an HTML table, as wanted, without “signs.

L. 202

Last Name <? echo "$last_name" ?>
// output: Longfellow

It occurs next in an echo note to the visitor, always with “signs. Here’s 4 test variations:

echo ‘L460 last_name =’."$last_name", ‘’; //Output: L460 last_name =“Longfellow”

echo ‘L461 last_name =’. $last_name, ‘’; //Output: L461 last_name =“Longfellow”

echo “L462 last_name =”. $last_name, “”; //Output: L462 last_name =“Longfellow”

echo “L463 last_name =” , “$last_name”, ‘’; //Output: L463 last_name =“Longfellow”

Why do single- and double-quotes both insert double-quotes, which were never defined in the last name string?

Thanks for ideas
USIT

[php]
If($TEST){ $last_name = “Longfellow” ;}

echo ‘L461 last_name =’.$last_name.’’;[/php]

Use " " for strings as they may contain spaces.

Cleaner and less prone to mistakes.

[php] echo “L461 last_name =$last_name”;[/php]

I tend to disagree, it’s OK if you have only have one or two lines of code, but if you have lots of lines it can be confusing.

I also heard some old cranky programmer state that

[php]echo "L461 last_name = " . $last_name . “
\n”;[/php]
-or-
[php]echo 'L461 last_name = ’ . $last_name . ‘
’ . “\n”;[/php]
should be written like such :

[php]echo "L461 last_name = " , $last_name , “
\n”;[/php]
-or-
[php]echo 'L461 last_name = ’ , $last_name , ‘
’ , “\n”;[/php]
For it takes less process using a comma, but I found out with today’s computers that’s not that significant and that using a comma instead of a period can lead you into trouble in special circumstances.

putting {} braces around the variable doing it this way
[php]echo “L461 last_name = {$last_name}”;[/php] helps out when there is lots of lines of code in my opinion. There I think I have everything covered. ;D

This echo “L461 last_name = {$last_name}”; is actually how I do it when it is not a defined variable.

Example {row[‘some_variable’]} or {$_POST[‘some_var’]} and is actually how I first answered the question until I noticed it was a user/programmer defined variable. If someone is not using an editor with syntax highlighting (shame on them) it is easy to miss one the escape quotes or periods and is extra typing. Since it all works, it really is a matter of programmer preference, the key being, just be consistent. A minor php peeve is there are too many ways to do the same thing. Not sure how you think it can be confusing no matter how many lines. If you have an editor with syntax highlighting it is more than obvious where the variables are. Take this forums highlighting for example, everywhere I see blue there is a variable. I can speed glance an an entire page and see the variables without even actually reading or looking at them.

Example:
[php]echo “Lorem ipsum dolor sit amet, consectetur adipiscing elit. $Suspendisse pulvinar iaculis quam,
$sed gravida risus placerat ut. Donec suscipit efficitur magna, sed dapibus risus accumsan dapibus.
Pellentesque sagittis mauris $placerat augue tincidunt efficitur. Phasellus lacinia metus $ex. Suspendisse
potenti. Phasellus molestie, $libero vitae lobortis accumsan, leo odio rhoncus ipsum, $non imperdiet tortor
$justo ut lacus. Aenean mattis metus ac sapien $interdum sagittis. Nam vel enim vel felis tincidunt congue.
Vivamus facilisis posuere nibh ac tempor. Nullam $sed risus sit amet dui fermentum ornare. $Maecenas
quis blandit quam. Fusce eu diam a $tellus viverra pretium vel sit amet lorem. Ut accumsan efficitur ex id
aliquam. Ut condimentum semper nulla $id fermentum. Etiam pretium ligula elit, $nec feugiat ex $pharetra
in.”;[/php]

Can’t find any place to write this reply so I’m opening a new topic

First: thanks to everyone who replied to the unwanted double quotes issue. To my surprise we all got the same results even though several variations of code were tried. This suggests to me that the bad code exists in what I sent out, or what I sent out didn’t properly explain the problem. So’s here’s a 2nd attempt along with the results we all got.

[php] //////////////////////////////////////////////////////////////////////////
// An echo to a visitor produces the following: “Frank” “Longfellow” your …; The double quotes are not wanted.
//*** My test code:
echo ‘L467 last_name =’."$last_name", ‘’; //Output: L460 last_name =“Longfellow”
echo ‘L468 last_name =’. $last_name, ‘’; //Output: L461 last_name =“Longfellow”
echo “L469 last_name =”. $last_name, “”; //Output: L462 last_name =“Longfellow”
echo “L470 last_name =” , “$last_name”, ‘’; //Output: L463 last_name =“Longfellow”

//*** PHP Help Forum code suggestions:
echo ‘L472 last_name =’.$last_name.’’; //Output: L472 last_name =“Longfellow”
echo “L473 last_name =$last_name”; //Output: L473 last_name =“Longfellow”
echo "L474 last_name = " . $last_name . “
\n”; //Output: L474 last_name = “Longfellow”
echo 'L475 last_name = ’ . $last_name . ‘
’ . “\n”; // L475 last_name = “Longfellow”
echo 'L476 last_name = ’ . $last_name . ‘
’ . “\n”; // L476 last_name = “Longfellow”
echo "L477 last_name = " , $last_name , “
\n”; //Output: L477 last_name = “Longfellow”
echo 'L478 last_name = ’ , $last_name , ‘
’ , “\n”; // L478 last_name = “Longfellow”
echo “L479 last_name = {$last_name}”; //Output: L479 last_name = “Longfellow”
/////////////////////////////////////////////////////////////////////////////////////////////////[/php]

What’s going on??
Thanks for any ideas. Ed

Well, how is $last_name be stored (propagated) ? That is the only area you really haven’t addressed.

[tt]Topics merged[/tt]

I have to ask where the value is being added as well.

[php]$name = “John”;
echo $name;[/php]

Will not produce “John” on it’s own.

[php]

$last_name =“Longfellow”;

echo ‘L467 last_name =’."$last_name", ‘’;

echo ‘L468 last_name =’. $last_name, ‘’;

[/php]

Neither of these produce

“Longfellow”
for me, they all output
Longfellow
as I would expect.

Re: ‘where value is being stored’ …
At the front of the program, L.101 to L.119, 14 variables are being received via $_POST[‘var’]; Two L’s later, L.121 to L.137, 16 of the variables are set to TEST values in a list …
If ($TEST){
$last_name = Longfellow;
// etc.
}
Next follow two sections of STATE and COUNTRY preparations for vaster selectionl
Then the visitors submitted data are displayed in table format. The last name Longfellow appears in this table without quotation marks.

Later on in the program is the only other output of the last name in a courtesy echo.
echo "


$first_name $last_name your registration was unsuccessful.
";

This line outputs … “Frank” “Longfellow” your registration was unsuccessful. Those double quotes look terrible!

Re: ‘where the value is being added’ …
At the end of this registration script is a table where visitors insert requested data. For example …

First name:

Last name:

Again, Thanks for your interest.
Ed
PS: So far all suggestions produce the same quoted last name :frowning:

Seeing actual code is the only way to figure this out,

[php] $last_name = Longfellow;[/php]

Is not the actual code or Longfellow is a defined constant.

The work around, is to use JavaScript to remove the quotes, but I am sure they are being added somewhere that you are not showing us.

Here are the first two variable definitions stated in the If (TEST){
If($TEST){
$first_name = Frank;
$last_name = Longfellow;

Here is my issue,

What do you see when you run this code? ( And I swear I am not trying to disparage or beat you up on this.)

[php]

<?php $first_name = Frank; $last_name = Longfellow; echo "$last_name, $first_name"; [/php] Because when I run it I get this:
Notice: Use of undefined constant Frank - assumed 'Frank' in C:\Apache2\htdocs\Test\Test\index.php on line 3 Notice: Use of undefined constant Longfellow - assumed 'Longfellow' in C:\Apache2\htdocs\Test\Test\index.php on line 4 Longfellow, Frank

Frank and longfellow need to be within single or double quotes as someone previously posted to you. That is the absolute basic of PHP strings. You would do well to go back to the manual and study the basics.

$first_name=“Frank”;

astonecipher’s suggestion works:
$first_name = Frank; //per astonecipher
$last_name = Longfellow;
echo “$last_name, $first_name”; //Output: J Longfellow, Frank J

However, when this format is inserted in my echo to the visitor I get …
echo


“$first_name $last_name” your registration was unsuccessful.
;

Output: “$first_name $last_name” your registration was unsuccessful. L

Here are the formats I have tried; all with the same output…
//*** My test code:
echo ‘L467 last_name =’."$last_name", ‘’; //Output: L460 last_name =“Longfellow”
echo ‘L468 last_name =’. $last_name, ‘’; //Output: L461 last_name =“Longfellow”
echo “L469 last_name =”. $last_name, “”; //Output: L462 last_name =“Longfellow”
echo “L470 last_name =” , “$last_name”, ‘’; //Output: L463 last_name =“Longfellow”
//*** PHP Help Forum code suggestions:
echo ‘L472 last_name =’.$last_name.’’; //Output: L472 last_name =“Longfellow”
echo “L473 last_name =$last_name”; //Output: L473 last_name =“Longfellow”
echo “L474 last_name = " . $last_name . “
\n”; //Output: L474 last_name = “Longfellow”
echo 'L475 last_name = ’ . $last_name . ‘
’ . “\n”; // L475 last_name = “Longfellow”
echo 'L476 last_name = ’ . $last_name . ‘
’ . “\n”; // L476 last_name = “Longfellow”
echo “L477 last_name = " , $last_name , “
\n”; //Output: L477 last_name = “Longfellow”
echo 'L478 last_name = ’ , $last_name , ‘
’ , “\n”; // L478 last_name = “Longfellow”
echo “L479 last_name = {$last_name}”; //Output: L479 last_name = “Longfellow”
echo ‘L442 last_name =’.”$last_name”, ‘’; //Output: L442 last_name = “Longfellow” repeat
echo ‘L443 last_name =’. $last_name, ‘’; //Output: L443 last_name = “Longfellow”

The gods are not smiling. I think they’re ;D
Ed

I feel like I am going around in circles:

However, when this format is inserted in my echo to the visitor I get … [php]echo

"$first_name $last_name" your registration was unsuccessful.
;[/php]

Output: “$first_name $last_name” your registration was unsuccessful. L

That statement syntax is incorrect,
[php]echo “

$first_name $last_name your registration was unsuccessful.
”;[/php]

That appears to be what you are after. Aside from inline styles.

[php]

<?php $first_name = "Frank"; $last_name = "Longfellow"; echo "$last_name, $first_name
"; var_dump( $first_name ); [/php] Should produce:
Longfellow, Frank string(5) "Frank"

:slight_smile: Finally got it right. This works …
echo "


L.451 $first_name $last_name
";

Output: L.451 Frank Longfellow :o

On rechecking your various suggestions I find that Kevin Rubio’s two suggestions work:

echo “L452 last_name =$last_name”; // Output: L452 last_name =Longfellow
echo “L461 last_name = {$last_name}”; // Output: L461 last_name = Longfellow

Although I thought I ran every suggestion as it came in, I somehow missed Kevin’s or I wasn’t running Kevin’s when I thought I was.
For that I apologize Kevin. And apologizes to those whom I may have confused with my confusion.

Thanks to all.

Ed

Sponsor our Newsletter | Privacy Policy | Terms of Service