Variable Reminder?

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
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Variable Reminder?

Post by icesolid »

I am trying to remember how variables work agian as far as using the same one more than once.

$hi = "something";
$hi. = "something else";
$hi. = "something else else";

Is it like that?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: Variable Reminder?

Post by protokol »

icesolid wrote:I am trying to remember how variables work agian as far as using the same one more than once.

$hi = "something";
$hi. = "something else";
$hi. = "something else else";

Is it like that?
Well, in your case, if you echo $hi then you will notice that the result string is this:

somethingsomething elsesomething else else

So try this out:

$hi = "Hello ";
$hi.= "there ";
$hi.= "my ";
$hi.= "friend";

echo $hi; // this prints "Hello there my friend"

Another way to do the same thing is this:

echo "Hello"."there"."my"."friend"; // echos the same thing as above

Sometimes the second version comes in handy:

echo "You PHP version is ".phpversion()." so you are cool!";

Any time you have the .= all you are doing is padding the end of the original string with whatever is after the .=
Last edited by protokol on Wed Jul 24, 2002 12:19 am, edited 1 time in total.
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

Your example is the concatenation operator for strings. The "." just links two strings together. Then when you use it in front of the assignment operator "=" it becomes a unary operator that appends all strings to the right of the = to the end of the string contained in the variable. Like the following

Code: Select all

$number=1;
$number += 1;
if you where to echo $number after the above code, it would display 2 because the + infront of the = makes it a unary operator and simply adds the number on the right of = to the number found in the variable.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

$hi = "Hello";
$hi.= "there";
$hi.= "my";
$hi.= "friend";

echo $hi; // this prints "Hello there my friend"

echo "Hello"."there"."my"."friend"; // echos the same thing as above
Actually that would echo "Hellotheremyfriend" with no spaces.

I know that is rather "picking nits," but the unitiated might conclude from your sample that the dot operator inserts a space.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

good point .. typo on my part .. meant to put a space after each one

i fixed the previous post to show the correct form
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Post Reply