Quote from Kevin's Article
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote/font><HR>
Getting back to the four statements above, the operators used allow you to add, subtract, multiply, and divide numbers. Among others, there is also an operator for sticking strings of text together:
// Assigns a value of "Hi there!".
$testvariable = "Hi " . "there!";
<HR></BLOCKQUOTE>
What I don't understand is when the . operator is needed since
The . operator is not necessary to print out results by combining a variable and a string
such as:
========
echo("$var1 rules!");
// Outputs "PHP rules!"
What it is necessary for is to combine variables that are not being printed and instead are reassigning the value of another variable (as in freddies example).
And also to combine a string and a function to create another string.
Or to make it simpler. A string and a variable can be combined without the . but a variable and a variable, a string and a function, and a variable and a function must all use the .
Am I missing something or am I getting most of it?
I use it when I need to concatenate strings together like for instancesay I wanted to have a form where it took someones address and it had another field for address2
I could check to see if address2 was not blank and then concatenate them together for use later on ie:
Sometimes you need to use the . operator, because you are joining a string and a function returning a string.
Example:
$result = mysql_query("SELECT name FROM customers");
$data = mysql_fetch_array($result);
echo "Customer Name: $data['name']"; # This will NOT produce the desired result!
echo "Customer Name: ".$data['name']; # This WILL produce the correct result.
-- OR --
echo "The following error occured: urldecode($err)"; # Incorrect.
echo "The following error occured: ".urldecode($err); # Correct.
Bookmarks