Page 1 of 1
Variable Reminder?
Posted: Tue Jul 23, 2002 7:20 pm
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?
Re: Variable Reminder?
Posted: Tue Jul 23, 2002 10:05 pm
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 .=
Posted: Tue Jul 23, 2002 10:42 pm
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
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.
Posted: Wed Jul 24, 2002 12:14 am
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.
Posted: Wed Jul 24, 2002 12:19 am
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
Posted: Wed Jul 24, 2002 1:56 am
by twigletmac