Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Php Variable Concatenation, Something New I learned Today! :P
szupie
post Feb 18 2005, 04:02 PM
Post #1


S.P.A.M.S.W.A.T.
Group Icon

Group: Members
Posts: 814
Joined: 22-January 05
From: San Antonio, Texas (No, I'm not dumb. I just moved here...)
Member No.: 2,284



I was coding one of my php page today, when I realized that I had to add multiple values to a variable (now that I think of it, a solution with arrays is possible too). But, the problem is that I have to add them in different parts of the code, so the new line that defines the variable will cover up the previous one. I played around with the code, and I finally got this solution:


$message = "test1";

$message .= "test2";

$message .= "test3";

echo $message;


By adding a dot in front of the = sign, I could concatenate the previous value with the new one and put them in the same variable. So... Yeah... This is just something I figured out, and I'm sure most people know about this. I just had to share... ;P

Oh, btw, a dot on the first = works too. It still "concatenates" the nothingness before it and the new value.

This post has been edited by szupie: Feb 18 2005, 04:22 PM
Go to the top of the page
 
+Quote Post
marijnnn
post Feb 18 2005, 04:53 PM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 336
Joined: 22-September 04
Member No.: 798



that's in fact a basic programming trick.

just like this will give 9:

$number = 5;
$number +=4;

echo $number;

same thing for -=, /=, *=

Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Feb 18 2005, 04:57 PM
Post #3


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411
myCENTs:84.36



There's another post that demonstrates the usage of this ".=" operator for concatenating strings... You might want to take a look at it:

http://www.astahost.com/index.php?showtopi...148&#entry18148

These are standard operators (Assignment OPs) found in almost any programming languages... Here's a brief list:

Addition: +=
Subtraction: -=
Multiplication: *=
Division: /=
Modulus: %=
Left shift assignment: <<=
Right shift assignment: >>=
Bitwise-AND: &=
Bitwise-exclusive-OR: ^=
Bitwise-inclusive-OR: |=
Go to the top of the page
 
+Quote Post
szupie
post Feb 18 2005, 05:03 PM
Post #4


S.P.A.M.S.W.A.T.
Group Icon

Group: Members
Posts: 814
Joined: 22-January 05
From: San Antonio, Texas (No, I'm not dumb. I just moved here...)
Member No.: 2,284



Heh, just what I thought: A simple basic code. I have seen the .=/-=/+=/etc operators before, but I've never thought of using them. This proves that writing your own programs lets you learn fast!
Go to the top of the page
 
+Quote Post
avalon
post Feb 18 2005, 06:35 PM
Post #5


Advanced Member
Group Icon

Group: Members
Posts: 160
Joined: 27-October 04
Member No.: 1,260



Cool! Never thought of using that them, maybe I should start using some of it.
Go to the top of the page
 
+Quote Post
mastercomputers
post Feb 19 2005, 06:39 AM
Post #6


PESTICIDAL MANIAC
Group Icon

Group: Members
Posts: 626
Joined: 1-September 04
From: Auckland, New Zealand
Member No.: 27



Glad you've learnt concatenation, and the correct term too.

Other methods, without using the .= operator is we can join different variables into another variable.

e.g.

$string1 = 'Hello, ';
$string2 = 'World!';
$string3 = $string1 . $string2;
$string4 = $string1 . 'and welcome to my ' . $string2;

echo $string3 . ' ' . $string4;
//results: Hello, World! Hello, and welcome to my World!

The period/dot is our means of joining or linking our variables/strings/etc.
$var .= 'hey'; // means $var = $var . 'hey';

When you talk about concatenating NULL (nothingness) initialised variables, you should really make sure that is the case. All variables should be initialised as NULL but there's no gaurantee that this would be the case and if you concatenate a variable that you expected should have been NULL but wasn't, you might get undesirable or incorrect results.

It's safer to initialise the variable first then concatenate onto it.

Cheers,


MC
Go to the top of the page
 
+Quote Post
szupie
post Feb 19 2005, 01:46 PM
Post #7


S.P.A.M.S.W.A.T.
Group Icon

Group: Members
Posts: 814
Joined: 22-January 05
From: San Antonio, Texas (No, I'm not dumb. I just moved here...)
Member No.: 2,284



Yeah, I've learned how to concatenate the way you just said, MC, that's why I knew the word. The first time I've heard of it, it was confusing (the word).

The +=/-=/%=/etc ones don't work for strings, right? They only work with numbers?
Go to the top of the page
 
+Quote Post
vujsa
post Feb 20 2005, 11:24 PM
Post #8


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714
myCENTs:35.43



On a related topic,
.= has a derivitive.
. (dot) can be used in complex variable assignments.

For example if you wanted to use an inline function use the following:
$html = "PI = ".number_format(M_PI,"2",".","")." = ".M_PI."!";

Where the RED code is outside of the quotes so that the function will execute correctly. Otherwise the output would read:
PI = number_format(M_PI,"2",".","") = M_PI!
Instead of:
PI = 3.14 = 3.14159265359!

Other wise written:
CODE
$html = "PI = ";
$html .= number_format(M_PI,"2",".","");
$html .= " = ";
$html .= M_PI;
$html .= "!";


Just to recap.

The . (dot) means plus or and.

This allows strings to be mixed with functions etc...
Saves from repetitive coding and as a result time. Makes the code cleaner and easier to read when an entire string "sentence" is all together on the same line.

Speaking of eliminating repetitive coding, see the following post of mine:Rapid HTML code generation using simple PHP
Many other good PHP links in the same forum.

Good luck
vujsa
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Living Without Computer Today - Is It Possible?(41)
  2. Want To Make Your Oun PC Games Then Read This(6)
  3. Help: $_post Variable For Options From Select Types?(6)
  4. Help: Smf Sources.php File Producing Parse Error(5)
  5. How To Reset The Server Variable Php_auth_user(9)
  6. Firefox 2.0 Beta 2 Released Today!(2)
  7. Are There Mail Server Issues Today?(4)
  8. Me And Xoops Are Not Getting Along Today!(0)
  9. CType With Variable In VB.NET(5)
  10. Variable From Line Further Then Current Line?(13)
  11. Quickly Create Form Variables(5)
  12. Firefox Updated To FX Ver 2.0.0.1(6)
  13. OSCommerce Errors(5)
  14. Enumeration Enum = .....(4)
  15. Things I Learned From Watching Happy Feet 8 Times(20)
  1. I Learned(1)
  2. Php Any Variable In String.(1)
  3. Unexpected Error(2)
  4. Send Php Variable To Javascript(5)
  5. Website Navigation Hover Buttons Stick So Made Css Today(7)
  6. A Few Thoughts On Freelance Programming(9)
  7. Simple Java Question(3)
  8. What I Learned About The Increadible Hulk Movie(4)
  9. What I Learned...(3)
  10. What I Learned About Dark Knight(11)
  11. Permanent Variable(7)
  12. Which Language Is Easy And Secure Today For Web Development(2)
  13. Cross Over Office For Free(0)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 04:37 PM