Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  agyat : (24 May 2013 - 05:15 PM) O Dear, Where Are You? Without Your Words This Sb Is ..
@  agyat : (23 May 2013 - 01:23 AM) Wow! Mr. Sb Back Home.
@  OpaQue : (23 May 2013 - 12:44 AM) Ting
@  OpaQue : (24 April 2013 - 02:44 PM) I guess, Time to run Mycent script.
@  OpaQue : (24 April 2013 - 02:43 PM) wow.. not much spam. except habatt posting lot of links.. :P
@  yordan : (23 April 2013 - 01:04 PM) You're welcome, agyat. Nice to have been helpful. Second lesson: try full words, "you" instead of "EW".
@  agyat : (23 April 2013 - 05:03 AM) @YORDAN: tHANK EW FOR YOUR FIRST LESSON.   :D
@  yordan : (22 April 2013 - 09:43 PM) @agyat : "why don't you help me", or "please help me", or "please teach us"
@  yordan : (22 April 2013 - 09:42 PM) welcome back, velma
@  velma : (22 April 2013 - 07:51 AM) **yawns** Good to be back, wonder what is going on here :)
@  agyat : (22 April 2013 - 03:50 AM) Oh! so, why don't help me learn english..
@  yordan : (21 April 2013 - 08:38 PM) The goal mentioned by shiu : "learning english, learning computer"
@  agyat : (21 April 2013 - 06:31 PM) WHAT GOAL?
@  yordan : (20 April 2013 - 10:39 AM) yes, that's our goal. simultaneouly learning English and teaching/learning computer using.
@  shiyu : (20 April 2013 - 07:30 AM) learning english,learning computer
@  yordan : (19 April 2013 - 01:11 PM) Oh, I see, it's just a trick in order to force people looking at your texte. Somehow smart, maybe.
@  agyat : (19 April 2013 - 02:54 AM) And of course I know it is not SEO friendly.
@  agyat : (19 April 2013 - 02:52 AM) There may be two possible answers for that ....


1) Shout was posted using mobile keypad.

2) To force people read content carefully and/or with more concentration.
@  agyat : (19 April 2013 - 02:49 AM) There may be two possible answers for that ....
@  yordan : (18 April 2013 - 09:35 PM) however, why this mixing of capital letters in the middle of your text?

Replying to Php Variable Concatenation


Post Options

    • Can't make it out? Click here to generate a new image

  or Cancel


Topic Summary

Posted 23 June 2011 - 10:01 AM

string (number concatenatio)?Php Variable Concatenation

Pleas go through the following query What is echo $a, $b and $c ?

-reply by harish


vujsa

Posted 20 February 2005 - 11:24 PM

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

szupie

Posted 19 February 2005 - 01:46 PM

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?

mastercomputers

Posted 19 February 2005 - 06:39 AM

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

avalon

Posted 18 February 2005 - 06:35 PM

Cool! Never thought of using that them, maybe I should start using some of it.

szupie

Posted 18 February 2005 - 05:03 PM

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!

miCRoSCoPiC^eaRthLinG

Posted 18 February 2005 - 04:57 PM

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....148

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

marijnnn

Posted 18 February 2005 - 04:53 PM

that's in fact a basic programming trick.

just like this will give 9:

$number = 5;
$number +=4;

echo $number;

same thing for -=, /=, *=

szupie

Posted 18 February 2005 - 04:02 PM

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.

Review the complete topic (launches new window)