Connecting Tech Pros Worldwide Forums | Help | Site Map

easy way of concatenating 2 strings

Kelvin Chu
Guest
 
Posts: n/a
#1: Jul 17 '05
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




Michael Winter
Guest
 
Posts: n/a
#2: Jul 17 '05

re: easy way of concatenating 2 strings


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

[snip]
[color=blue]
> is there an equivalent of .= in PHP?[/color]

Umm, yes: .=

[snip]
[color=blue]
> $debug .= "sometext" [...][/color]

That will work fine. I take it you didn't just try it and looked for an
error in your error log then?

[snip]
[color=blue]
> $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.[/color]

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.
Kenneth Downs
Guest
 
Posts: n/a
#3: Jul 17 '05

re: easy way of concatenating 2 strings


Kelvin Chu wrote:
[color=blue]
> Hi Group,
>
> (Using php5 on apache1)
>
> is there an equivalent of .= in PHP?
>[/color]

..=



--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Joshua Gao
Guest
 
Posts: n/a
#4: Jul 17 '05

re: easy way of concatenating 2 strings


Michael Winter wrote:[color=blue]
> 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.
>[/color]

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
Chung Leong
Guest
 
Posts: n/a
#5: Jul 17 '05

re: easy way of concatenating 2 strings


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);

James Pittman
Guest
 
Posts: n/a
#6: Jul 17 '05

re: easy way of concatenating 2 strings


Kelvin Chu wrote:
[color=blue]
> 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?[/color]

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
Closed Thread