Connecting Tech Pros Worldwide Help | Site Map

easy way of concatenating 2 strings

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:52 PM
Kelvin Chu
Guest
 
Posts: n/a
Default easy way of concatenating 2 strings

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




  #2  
Old July 17th, 2005, 12:53 PM
Michael Winter
Guest
 
Posts: n/a
Default 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.
  #3  
Old July 17th, 2005, 12:53 PM
Kenneth Downs
Guest
 
Posts: n/a
Default 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)
  #4  
Old July 17th, 2005, 12:53 PM
Joshua Gao
Guest
 
Posts: n/a
Default 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
  #5  
Old July 17th, 2005, 12:53 PM
Chung Leong
Guest
 
Posts: n/a
Default 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);

  #6  
Old July 17th, 2005, 12:54 PM
James Pittman
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.