Question:
"." for concatenation in php?
Sleeping Troll
2008-08-14 14:47:16 UTC
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?
Seven answers:
Bernz
2008-08-14 15:24:20 UTC
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!
Alex B
2008-08-14 21:54:25 UTC
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";
just "JR"
2008-08-14 23:24:58 UTC
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 " \r ";

}

mysql_free_result ( $list );

"
Nick
2008-08-14 21:53:00 UTC
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. :)
Tsion
2008-08-14 21:56:53 UTC
This worked fine for me, maybe its an ini setting?




$filename = 'test.txt';

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



?>
2008-08-14 22:01:11 UTC
Use double quotes if you want the text parsed and single quotes if you want it left alone.
Jim L
2008-08-14 21:55:47 UTC
echo 'This filename is ' . $filename . ' some other text';



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


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...