Is this operator ".=" valid?
Moderator: General Moderators
- becky-atlanta
- Forum Commoner
- Posts: 74
- Joined: Thu Feb 26, 2009 6:26 pm
- Location: Atlanta, GA
Is this operator ".=" valid?
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.
If it is a valid code, how to use it properly?
I appreciate your help.
Re: Is this operator ".=" valid?
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?
= 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.
will output "bed"
will output "waterbed"
.= 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;
Code: Select all
$variable = "water";
$variable .= "bed";
echo $variable;
Re: Is this operator ".=" valid?
To avoid the error, define the variable before you use the .= operator.
For example:
For example:
Code: Select all
$var = NULL;
//or
$var = '';
//then you can append to it...
$var .= 'something';- becky-atlanta
- Forum Commoner
- Posts: 74
- Joined: Thu Feb 26, 2009 6:26 pm
- Location: Atlanta, GA
Re: Is this operator ".=" valid?
Thank you for all your reply, especially Jack's example. It surely helped to fix the error.
Thank you again for helping this newbie out.
Thank you again for helping this newbie out.
Re: Is this operator ".=" valid?
Cool, no problem.
-
SimonMayer
- Forum Commoner
- Posts: 32
- Joined: Wed Sep 09, 2009 6:40 pm
Re: Is this operator ".=" valid?
I have never had to define a variable before using .=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';
Is this version-specific or controlled by an ini setting?
It would be interesting to know incase I come across it.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Is this operator ".=" valid?
It will throw a notice using an undefined variable. You should consider turning your error reporting higher to atleast E_ALL.
Re: Is this operator ".=" valid?
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).
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).