"." for concatenation?

Discussion in 'PHP' started by Sleeping Troll, Aug 14, 2008.

  1. #1
    At first I thought, "well, thats easy". I am an old timer, my computer experience goes back to early '70's. "." for seperating entities goes back to the '60's! So php decides to use it for concatenation! no problem right? try using echo with a string that has a concatenated var that returns a filename such as "filename.txt", the period in the string gets parsed!

    Example:

    echo "This filename is ".$filename." some other text";
    Code (markup):
    Now that I have vented, does anyone know of a solution?
     
    Sleeping Troll, Aug 14, 2008 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It doesn't get parsed.
     
    LogicFlux, Aug 14, 2008 IP
  3. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    LogicFlux is correct. Though, just as an alternative, you can also do this:

    <?php
    echo "This filename is {$filename} some other text";
    PHP:
    This method will only work with double quotes. Not with single quotes.
     
    ahowell, Aug 14, 2008 IP
  4. is0is0

    is0is0 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4

    You quoted the variable inside the string, you don't need to quote the variable inside the string if your string is already quoted.

    
    echo "This filename is $filename  some other text";
    
    PHP:
    You don't even need to use concatenation for this either, your not joining a variable with anything else you can reference that variable in the my example.
     
    is0is0, Aug 14, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    You don't need to, but a lot of people do it because their syntax highlighters make the variables easier to spot in the code. And there's nothing wrong with this.

    Alternatively, you can use commas rather than the periods.
    
    echo 'This filename is ', $filename, ' some other text';
    
    PHP:
     
    nico_swd, Aug 15, 2008 IP