PHP Variables doubt?

Posted in Help the coder! on Jul 2, 2009 at 10:13 IST (8 months ago). Subscribe to this post Bookmark and Share Email
Showing comments 1 to 4 of total 4 on page 1 of 1
Tagged php, Variable
Post reply
« Previous1Next »

kiddinkid
Rank: 34

What is the difference between $varName = "$var \n"; from that with a period: $varName. = "$var \n"? quite confusing.

Posted by kiddinkid on Thursday, July 2, 2009, 10:13 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

lemondri...
Rank: 55

= is for attributing a value .= is for concatenating

$var = "a"; $var = "b"; // $var will be "b";

$var = "a"; $var .= "b"; // $var will be "ab";

Posted by lemondrizzle on Thursday, July 2, 2009, 12:24 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

mindhacker
Rank: 38

The . operator in PHP means concatenation.

You can use operators with the assignment operator (=) to accomplish both affects.

So these are the same:

$varname .= "stuff";
$varname = $varname . "stuff";
Posted by mindhacker on Thursday, July 2, 2009, 1:56 pm

The first one is assigning a value to another variable.

The second one is used to concatenate strings. consider a scenario where you need to concatenate strings within a loop. This one is the best way to do so.

foreach(iteration)
{
$some_variable .= $some_other_variable .",";
}

output is like this : val1,val2,val3 etc..
Posted by rajkumar_pb on Wednesday, October 7, 2009, 2:49 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment
Pages: « Previous1Next »

Post your comment (No registration required)

  Add my comment  

TechieDesi Community

Not signed in (Sign-in or Register)
Be a true TechieDesi!
Top 10 Users
Spread the word
Invite your friends
Fan stuff
Help us improve
Need Help
FAQ's
Search tips
Found a bug? Report!
Feeds and letters
Subscribe via RSS
Archives
Subscribe to newsletter
Unsubscribe e-mail
Miscellaneous
Privacy policy
Visit rootnerve
About us
About us
Support the development
Official Blog
Advertise with us
Careers
Copyright (c) 2008, TechieDesi.com. All rights reserved | About us | Do-Not-Disturb registry | Powered by rootnerve | Page rendered in 0.125 seconds