Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Ampersand in String

Tags:

string

php

echo

I'm having a bit of a problem. I am trying to create an IRC bot, which has an ampersand in its password. However, I'm having trouble putting the ampersand in a string. For example...

<?php

$var = "g&abc123";

echo $var;

?>

I believe this should print g&abc123. However it's printing g.

I have tried this as well:

<?php
$arr = array("key" => "g&abc123");
print_r($arr);
?>

This prints it correctly with the g&abc123, however when I say echo $arr['key']; it prints g again. Any help would be appreciated. I'm running PHP5.3.1.

EDIT: Also, I just noticed that if I use g&abc123&abc123 it prints g&abc123. Any suggestions?

like image 704
John M. Avatar asked Jun 17 '10 23:06

John M.


1 Answers

I don't have that issue in a console:

php > $d="g&abc123";
php > echo $d;
g&abc123

What environment are you printing the output to? It sounds like you are viewing it in a web browser, and the & is being interpreted as a malformed HTML entity. Try replacing the & symbol with the entity encoded version &amp;.

like image 100
JAL Avatar answered Sep 20 '22 09:09

JAL