By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

easy way of concatenating 2 strings

P: n/a
Hi Group,

(Using php5 on apache1)

is there an equivalent of .= in PHP?

I'm making a program and I have lots of debug statements being added using

$debug = $debug . "some text"

and at the bottom of the HTML I have a <?php echo $debug ?>

I was just wondering, is there an equvalent to .= in PHP, so like

$debug .= "sometext", save me some typing :)

Also, another question

I tried making a function like so (just inline in the top of the php page):

<?php
$debug = "";
function addDebug($text) {
$debug = $debug . $text . "<br/>";
echo $debug; // returns correctly
}
addDebug("blah");
echo $debug; // not displayed properly
?>

but it doesn't seem to work. Since I'm using php5, I thought everything is
getting passed by reference now?

Thanks in advance,

Kelvin

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Kelvin Chu wrote, On 28/04/2005 00:41:

[snip]
is there an equivalent of .= in PHP?
Umm, yes: .=

[snip]
$debug .= "sometext" [...]
That will work fine. I take it you didn't just try it and looked for an
error in your error log then?

[snip]
$debug = "";
function addDebug($text) {
$debug = $debug . $text . "<br/>";
echo $debug; // returns correctly
}
addDebug("blah");
echo $debug; // not displayed properly
?>

but it doesn't seem to work.


Only superglobals are automatically available in all scopes. You either
have to declare $debug global, or use the $GLOBALS superglobal.

$debug = '';

function addDebug($text) {
global $debug;

$debug .= $text . '<br/>';
echo $debug;
}
addDebug('blah');
echo $debug;

OR

function addDebug($text) {
$GLOBALS['debug'] .= $text . '<br/>';
echo $GLOBALS['debug'];
}

Both of these issues are detailed in the PHP manual.

Mike
Does anyone happen to know why the $GLOBALS superglobal doesn't follow
the naming scheme of all the other superglobals? It seems odd to make it
unique.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 17 '05 #2

P: n/a
Kelvin Chu wrote:
Hi Group,

(Using php5 on apache1)

is there an equivalent of .= in PHP?


..=

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #3

P: n/a
Michael Winter wrote:
Does anyone happen to know why the $GLOBALS superglobal doesn't follow
the naming scheme of all the other superglobals? It seems odd to make it
unique.


IIRC, there was a discussion about that a while ago. It was decided to
not change the $GLOBALS superglobal to $_GLOBALS because of historical
reasons, just as implode() can take it's arguments either way. I think
they should add $_GLOBALS, and gradually phase out $GLOBALS like how
$HTTP_*_VARS was replaced by $_POST, $_GET, and such.

-Joshua Gao
Jul 17 '05 #4

P: n/a
For situations like these it's more flexible to use an array, in case
you want to format the items more nicely in the future.

$debug[] = $text;

....

echo implode(',', $debug);

Jul 17 '05 #5

P: n/a
Kelvin Chu wrote:
Hi Group,
Also, another question

I tried making a function like so (just inline in the top of the php page):

<?php
$debug = "";
function addDebug($text) {
$debug = $debug . $text . "<br/>";
echo $debug; // returns correctly
}
addDebug("blah");
echo $debug; // not displayed properly
?>

but it doesn't seem to work. Since I'm using php5, I thought everything is
getting passed by reference now?


Well no - the problem is that it's NOT passed by reference. $debug is
not passed at all. You could modify the addDebug to accept $debug
passed by reference. That way, the contents of the memory location
pointed to by $debug will change.

function addDebug(\$debug, $text) {
$debug .= $text . "<br />";
}

$debug = "";
addDebug($debug, "foo");
addDebug($debug, "bar");
echo($debug);

Someone suggested using
global $debug;
This would also work - but it flies in the face of good programming
practice - as far as scoping variables etc.

The most elegant way would be to use a Class.

class Debug {
var $debug;
function add($text) {
$this->debug .= "$text<br />";
}
function out() {
echo $this->debug;
}
}

$odebug = new Debug;
$odebug->add("foo");
$odebug->add("bar");
$odebug->out();

Then again, you could just use the .= operator!

$debug = "";
$debug .= "foo<br />";
$debug .= "bar<br />";
echo ($debug);
Jamie
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.