sungpeng Posted April 26, 2012 Share Posted April 26, 2012 .$variable. I am also wondering why sometime need to add two dot before and after a variable ? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 26, 2012 Author Share Posted April 26, 2012 Sometime it don't require the two dots ? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 26, 2012 Author Share Posted April 26, 2012 why sometime written in this way  $message = '  Payment status: '.$payment_status.'  '; Sometime in another way $message ="  Payment status: $payment_status "; more simple and better   Quote Link to comment Share on other sites More sharing options...
requinix Posted April 26, 2012 Share Posted April 26, 2012 The first one uses concatenation: you start with one string, concatenate the variable, then concatenate a second string. The dot there is the operator that does string concatenation. The second one uses variable interpolation: PHP looks at what's in the string, sees a variable, and automatically "inserts" its value into that spot. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 26, 2012 Author Share Posted April 26, 2012 Just use the second one variable interpolation. Why people consider using the first one concatenation ? Much more lengthy. The two dots are with the variable and not with the string. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 26, 2012 Share Posted April 26, 2012 If that's how you feel about it then go right ahead. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 26, 2012 Author Share Posted April 26, 2012 Thank you Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 Using double quoted strings uses more processing than single quoted. If you're not putting a variable in them, there's no reason to use double quoted strings. If you are putting a variable in the string, sometimes you want to use " quotes in it. Now which looks better? <?php $str = "<div id=\"myDiv\"><p class=\"myClass\">This is a string with one $var in it. Someone said, \"This is a silly string\"</p></div>"; $str = '<div id="myDiv"><p class="myClass">This is a string with one '.$var.' in it. Someone said, "This is a silly string"</p></div>'; ?> Quote Link to comment Share on other sites More sharing options...
Nodral Posted April 26, 2012 Share Posted April 26, 2012 And if you're specifying a varibale from an array you cannot use it within double quotes.  eg "hello $array[1] " will fail  'hello'.$array[1] will work   Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted April 26, 2012 Share Posted April 26, 2012 And if you're specifying a varibale from an array you cannot use it within double quotes.  Wrong. You just need to use curly braces around the array variable:  echo "Look, I'm an array variable in a double-quoted string! {$arr['someKey']}"; Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 Which looks even worse IMO Quote Link to comment Share on other sites More sharing options...
Nodral Posted April 26, 2012 Share Posted April 26, 2012 Technically I was right tho. What I had written would fail!!! lol  Forgot about the curly braces!! oops Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted April 26, 2012 Share Posted April 26, 2012 Which looks even worse IMO  Eh, I like it better, especially when dealing with database CRUD. All those .'s bug me. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 26, 2012 Share Posted April 26, 2012 Using double quoted strings uses more processing than single quoted. If you're not putting a variable in them, there's no reason to use double quoted strings. 1. A negligible amount. It's a matter of a handful of added character comparisons (like character == '\\' || character == '$') which sum up to a grand total of negligible. 2. If you're concerned about efficiency, don't use PHP. There are plenty of other, real issues to focus on instead of which quoting style for strings is better. 3. I used to insist on single-quotes for variableless strings and double-quotes for other strings. Then I got tired of having to think ahead of time about whether I was going to put a variable into a string or not. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 That's of course true regarding the efficiency  Mostly I was pointing out it's a personal preference. You can't say definitively one is better than the other. It's like comparing if(true){ } to if(true) { }  People will have preferences on them and what you think looks better may look ugly to someone else. Just do what works for you and be consistent. I use single quotes for everything except SQL queries or other things I know have to have a ' in them. I have no problem going and changing a ' to a " so I don't worry about it too much ahead of time. Quote Link to comment Share on other sites More sharing options...
rythemton Posted April 26, 2012 Share Posted April 26, 2012 It really does come down to personal preference. I usually concatenate out of habit. It's taking enough effort sometimes to remember, should I be using a dot (.) or plus (+) to concatenate (PHP or JavaScript) let alone remember if a can use inline variables. It really comes down to, there is more than one way to do the same thing. Â All the following do the same thing, and I've used them all at one time or another depending on mood: echo 'This is my "text"!'; echo "This is my \"text\"!"; ?>This is my "text"!<?php Quote Link to comment Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 I use single quotes for everything except SQL queries or other things I know have to have a ' in them. I have no problem going and changing a ' to a " so I don't worry about it too much ahead of time. Â Why not use double quotes within query strings? SELECT `col` FROM `table` WHERE `val` = "string" is perfectly acceptable. Any escaping functions should treat single and double quotes the same. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 Again it's a personal preference. I prefer my queries to use single quotes. *shrug* Quote Link to comment Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 I understand, I was simply making sure you knew you didn't need to switch your conventions for queries Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 Ah yes, thank you! I guess I am breaking my own rule of consistency but oh well 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.