Is this operator ".=" valid?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Is this operator ".=" valid?

Post by becky-atlanta »

I take over someone's website. I came across the operator ".=". For example, $a .= "apple". I search this in the php online manual and did not find anything. Should the period be there before the equal sign? I suspect it is not valid because I am getting error that says undefined variable....

If it is a valid code, how to use it properly?

I appreciate your help.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Is this operator ".=" valid?

Post by Eran »

It's a short hand version which can be used with most operators. The manual does list it - http://us2.php.net/manual/en/language.o ... string.php
SimonMayer
Forum Commoner
Posts: 32
Joined: Wed Sep 09, 2009 6:40 pm

Re: Is this operator ".=" valid?

Post by SimonMayer »

= will overwrite the existing value of a variable
.= will concatenate to it

I would not recommend you change this syntax unless you have a specific reason to. The previous programmer may have placed it there for a reason.

Code: Select all

 
$variable = "water";
$variable = "bed";
echo $variable;
 
will output "bed"

Code: Select all

 
$variable = "water";
$variable .= "bed";
echo $variable;
 
will output "waterbed"
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is this operator ".=" valid?

Post by jackpf »

To avoid the error, define the variable before you use the .= operator.

For example:

Code: Select all

$var = NULL;
//or
$var = '';
 
//then you can append to it...
$var .= 'something';
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: Is this operator ".=" valid?

Post by becky-atlanta »

Thank you for all your reply, especially Jack's example. It surely helped to fix the error. :D

Thank you again for helping this newbie out.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is this operator ".=" valid?

Post by jackpf »

Cool, no problem.
SimonMayer
Forum Commoner
Posts: 32
Joined: Wed Sep 09, 2009 6:40 pm

Re: Is this operator ".=" valid?

Post by SimonMayer »

jackpf wrote:To avoid the error, define the variable before you use the .= operator.

For example:

Code: Select all

$var = NULL;
//or
$var = '';
 
//then you can append to it...
$var .= 'something';
I have never had to define a variable before using .=
Is this version-specific or controlled by an ini setting?
It would be interesting to know incase I come across it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Is this operator ".=" valid?

Post by John Cartwright »

It will throw a notice using an undefined variable. You should consider turning your error reporting higher to atleast E_ALL.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is this operator ".=" valid?

Post by jackpf »

I'm sure the .= operator has been around since at least before version 4, if not from the first "real" version.

It's a common operator found in most C style languages. Although obviously with a language specific "concatenation" symbol...in most cases it's a + (addition) symbol...but I guess PHP opted for the . (period).
Post Reply