dennismonsewicz Posted September 29, 2008 Share Posted September 29, 2008 What is the difference here: $var1 = "SELECT * FROM $tbl_name WHERE id = {$_POST['id']}"; VS $var1 = "SELECT * FROM $tbl_name WHERE id = '" . $_POST['id'] . "'"; Do the { brackets acheive the same as the period? Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted September 29, 2008 Share Posted September 29, 2008 As far as I know ya, its a personal preference. What I tend to do is asign the $_POST to a variable $id = $_POST['id']; $var1 = "SELECT * FROM $tbl_name WHERE id = '$id'"; Quote Link to comment Share on other sites More sharing options...
Barand Posted September 29, 2008 Share Posted September 29, 2008 http://www.php.net/manual/en/language.types.string.php Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 29, 2008 Share Posted September 29, 2008 By default you can use variables in double brackets. <pre><?php $test = 'World'; echo "Hello $test \n"; # Won't work! echo 'Hello $test \n'; echo "\n"; $arr = array( 'str' => 'World' ); # echo "Hello $arr['world']" is a little complex with the mixed quotes... as # echo "Hello $arr["world"]" is acceptable as well... to avoid making exceptions, # you use echo "Hello {$arr["str"]} \n"; $arr2 = array( 'World' ); # You don't have to with numeric keys echo "Hello $arr2[0] \n"; ?></pre> Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted September 29, 2008 Share Posted September 29, 2008 There is another difference. The first example does not produce a query string where single quotes enclose the $_POST['id'] Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 29, 2008 Share Posted September 29, 2008 The first syntax results in fewer syntax errors (at least in posts seen in these forums) because there are less different elements involved. The first syntax places the variable inside of the double-quoted string and forces php to recognize where the variable starts and ends and it allows the single-quotes around the index string name to be used as is. I don't recall any people that use this simpler looking syntax needing help with syntax errors. The second syntax places the variable outside of the double-quoted string by closing the double-quote, concatenate the variable with it, then concatenates another double-quoted string after the variable. For a single variable it is usually not a problem, but for multiple variables, this syntax is hard to read and apparently hard to type based on the number of people posting in forums that have syntax errors and cannot see what is wrong. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted September 29, 2008 Author Share Posted September 29, 2008 Thanks everyone... I have just seen it used here lately when I am editing smarty templates (which I hate) and I was just curious Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.