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?
Variable Reminder?
Moderator: General Moderators
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
Re: Variable Reminder?
Well, in your case, if you echo $hi then you will notice that the result string is this: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?
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.
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.
Code: Select all
$number=1;
$number += 1;- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
Actually that would echo "Hellotheremyfriend" with no spaces.$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
I know that is rather "picking nits," but the unitiated might conclude from your sample that the dot operator inserts a space.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Some handy links from the manual:
http://www.php.net/manual/en/language.operators.php
and more to the point
http://www.php.net/manual/en/language.o ... string.php
Mac
http://www.php.net/manual/en/language.operators.php
and more to the point
http://www.php.net/manual/en/language.o ... string.php
Mac