473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Jul 17 '05 #1
5 1372
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dont bother | last post by:
Hey, I have these attributes: index which is a numerical value value vector which is a numerical float value and I want to concatenate like this:
4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see...
1
by: ebobnar | last post by:
I need to call the function LoadImage which take a LPCTSTR argument to specify the path to the image to load. However, I need to create this path dynamically from user input by concatenating...
6
by: Ryan Smith | last post by:
I am trying to store a string into a string variable via the following code however am receiving an error and cant figure out what i am doing wrong. Any feedback is greatly appreciated. Line...
4
by: FB's .NET Dev PC | last post by:
Interesting note, the code below as is will attempt to cast what is clearly indicated as a string into a double. This is becuase the use of + as a concatenation operator. The error message...
4
by: lindiwemaduna | last post by:
I want to concatenate values of two text boxes into one string but these should be separated by a space in the database table. i have tried all the following but twas not successful: Dim fullName...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
10
by: Neil | last post by:
Using the MS Rich Textbox Control 6.0 in Access 2000, I need to concatenate several RTB controls into a single RTF file. Sometimes two strings will be side-by-side; other times they need to be...
3
by: Big Brother | last post by:
I've been thinking about the seemingly simple task of writing a va_arg-type function to concatenate arbitarily many strings. My first thoughts violated the following principles: 1) never...
4
by: clinisbut | last post by:
I'm not sure if this is the right group, but I didn't found any other more appropiate to post my problem. I'm trying to concatenate chars using the Glib library and I'm getting strange...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.