SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Who Me? JeremyL's Avatar
    Join Date
    Dec 1999
    Location
    Dallas, TX
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    echo($var1 . " rules!");
    // Outputs "PHP rules!"

    Is the same thing as

    echo("$var1 rules!");
    // Outputs "PHP rules!"

    ------------------
    Jeremy Luebke --&gt; jeremy@coolresources.com
    CoolResources.com --&gt; Part of the W3Nation Network

    [This message has been edited by JeremyL (edited August 09, 2000).]

  2. #2
    Who Me? JeremyL's Avatar
    Join Date
    Dec 1999
    Location
    Dallas, TX
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK let me see if I can get this straight.

    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?

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    if ($address2 != "") {
    $address = $address . " " . $address2;
    }

  4. #4
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Christian


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •