Jump to content
xisto Community
Sign in to follow this  
szupie

Php Variable Concatenation Something New I learned Today :P

Recommended Posts

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... ;POh, btw, a dot on the first = works too. It still "concatenates" the nothingness before it and the new value.

Edited by szupie (see edit history)

Share this post


Link to post
Share on other sites

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

http://forums.xisto.com/topic/81969-topic/?findpost=

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: |=

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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:

$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

Share this post


Link to post
Share on other sites
string (number concatenatio)?Php Variable Concatenation

Pleas go through the following query <?php$a = "1"+"2";$b = '1'+'2';$c = '1 + 2';?>What is echo $a, $b and $c ?

-reply by harish

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.