the .= operator

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
sutejok
Forum Commoner
Posts: 37
Joined: Wed Mar 24, 2004 4:08 pm

the .= operator

Post by sutejok »

Hi,

can anyone tell me what does the ".=" operator do? or probably how does it work..?

$themelist .= "$file ";



thx[/quote]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

.= means add it to a varible

Code: Select all

<?

$variable .= 'hello ';
$variable .= 'Johnny';

echo $varable;

//outputs Hello Johnny

?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

.= is a string operator.

an example using your vars:
$themelist = "hello ";
$themelist .= "tim.";

// this would output: hello tim.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

the . is a concatenation operator:

Code: Select all

$num = 1;
$text = "Number: " . $num;
echo $text;
//will output: Number: 1
the = is an asignment operator:

Code: Select all

$text = "Number 1";
echo $text;
//will output Number 1
when combining them together it is like saying
$text = $text . $num ::

Code: Select all

$text = "Number ";
$num = 1;
$text .= $num; //same as $text = $text . $num
//will output Number 1
there are also others like
+=
-=
/=
*=
++
--
Last edited by Illusionist on Wed Apr 07, 2004 9:47 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Illusionist wrote: when combining them together it is like saying
is saying its a string operator, lol.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

yes it is a string operator, but more specifically the concatenating assignment operator. But using that operator is the same as saying $text = $text . $num
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

i left out a few on my other assignment operators above, here is a more coimplete list from here:

Code: Select all

See the Arithmetic Operators page (http://www.php.net/manual/en/language.operators.arithmetic.php)
Assignment    Same as:
$a += $b    $a = $a + $b    Addition
$a -= $b    $a = $a - $b    Subtraction
$a *= $b    $a = $a * $b    Multiplication
$a /= $b    $a = $a / $b    Division
$a %= $b    $a = $a % $b    Modulus

See the String Operators page(http://www.php.net/manual/en/language.operators.string.php)
$a .= $b    $a = $a . $b      Concatenate

See the Bitwise Operators page (http://www.php.net/manual/en/language.operators.bitwise.php)
$a &= $b    $a = $a & $b    Bitwise And
$a |= $b    $a = $a | $b      Bitwise Or
$a ^= $b    $a = $a ^ $b      Bitwise Xor
$a <<= $b    $a = $a << $b    Left shift
$a >>= $b    $a = $a >> $b      Right shift
Post Reply