Question:

"." for concatenation in php?

by  |  earlier

0 LIKES UnLike

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";

Now that I have vented, does anyone know of a solution?

 Tags:

   Report

7 ANSWERS


  1. It's more like...

    echo "This file name is" . $filename . " some other text";

    Spaces may be needed, I am almost sure. Try and and give me your feedback. :)


  2. Use double quotes if you want the text parsed and single quotes if you want it left alone.

  3. First thing: PHP is GREAT language. Don't get discouraged!

    Now, your MySQL_Query is "dangerous"... You should NEVER set a $_GET variable directly in a SQL statement: you could get hacked REALLY easily. You should always use the recommended methods of PHP.net. (look at the function's name, you find great examples.)

    I've never seen a problem where PHP has interpreted a "." in a string. Look elsewhere for you problem. Your code actually looks reasonably good, so look more for a missing database $link or something like that.

    And by the way, I recommend using mysql_fetch_assoc instead of mysql_fetch_array (unless you use the array feature).

    Also, remember that strings within single quotes will NOT be expanded (e.g. interpreted) where as strings in double quotes WILL. SO:

    $name = "John";

    print "Hello $name.";

    ... returns Hello John.

    Where as

    $name = "John";

    print 'Hello $name';

    ... returns Hello $name.

    There is a minor performance improvement when using single quotes since no variable parsing will take place.

    Good luck!

  4. This worked fine for me, maybe its an ini setting?

    <?php

    $filename = 'test.txt';

    echo "This filename is ".$filename." some other text";

    ?>

  5. Your query is terribly wrong !!!

    Allow me to re-write it: (pay attention to ` ' " , remove spaces)

    "

    $ref = $_GET [ " Reg " ] ;

    $sql = "select ` filename ` from ` tablename ` where ` fieldname ` =

    ' " . $ref . " ' " ;

    $list = mysql_query ($sql) or die ( " ERROR: " . mysql_error ( ) );

    while ($row = mysql_fetch_array ( $list ) )

    {

    echo "<img src = ' Images/Templates/ " . $row [ 0 ]  . " ' width = ' 100 ' > \r ";

    }

    mysql_free_result ( $list );

    "


  6. I have not heard of this ever happening. The period should not get parsed. There is likely a syntax mistake. I'd be happy to look at the code for you.

    You may also like to know that when using double quotes this works as well:

    echo "This filename is $filename some other text";

  7. echo 'This filename is ' . $filename . ' some other text';

    that should do it." and ' are different in php.

Question Stats

Latest activity: earlier.
This question has 7 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions