dizzy1 Posted January 19, 2009 Share Posted January 19, 2009 Hi ALL, Basically all i want to do is join strings like this: $mysql = " Select * from "; $mysql = $mysql + " Person "; $mysql = $mysql + " Where Name like '%$SearchA%' "; Thanks in Advance Dizzy1 Quote Link to comment https://forums.phpfreaks.com/topic/141476-solved-string-concatenation/ Share on other sites More sharing options...
dizzy1 Posted January 19, 2009 Author Share Posted January 19, 2009 Just Figured it out, ARHHHH. $mysql = " Select * from "; $mysql = $mysql .= " Person "; $mysql = $mysql .= " Where Name like '%$SearchA%' "; Thanks Anyway Quote Link to comment https://forums.phpfreaks.com/topic/141476-solved-string-concatenation/#findComment-740553 Share on other sites More sharing options...
DarkWater Posted January 19, 2009 Share Posted January 19, 2009 No. You're making that WAY harder than it has to be: $mysql = "Select * from "; $mysql .= "Person "; $mysql .= "Where Name like '%$SearchA%'"; . is the concatenation operator as opposed to +. Use . where you would normally use + and you'll be fine. Quote Link to comment https://forums.phpfreaks.com/topic/141476-solved-string-concatenation/#findComment-740556 Share on other sites More sharing options...
dizzy1 Posted January 19, 2009 Author Share Posted January 19, 2009 Cheers as u can see i am new to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/141476-solved-string-concatenation/#findComment-740562 Share on other sites More sharing options...
.josh Posted January 19, 2009 Share Posted January 19, 2009 Just Figured it out, ARHHHH. $mysql = " Select * from "; $mysql = $mysql .= " Person "; $mysql = $mysql .= " Where Name like '%$SearchA%' "; Thanks Anyway Though that technically works, I thought I'd tell you why it works, as well as how to do it better. the dot (.) is the concatenation operator. It is javascript's equivalent of the plus sign (+). So looking at your original attempt of: $mysql = $mysql + " Person "; It really should have been $mysql = $mysql . " Person "; That is to say: concatenate the current value of $mysql and the string " Person " and assign the result to $mysql. A better way to do it that doesn't require as much resources and time, is to use the shorthand version: $mysql .= " Person "; This says: append the string " Person " to the end of $mysql. The advantages to this are: - Code is shorter shorter (less code to be parsed), so the script executes faster. - Concept is less abstract. If you visualize a string variable as lego blocks being stacked up. It's easy to visualize just adding another block or two on top of the tower you're building (or whatever kids build these days). Using the same lego blocks analogy with the previous example $mysql = $mysql . " Person ";, you would have to visualize your current tower of blocks, build another one that's a copy of it. Add the new blocks to that. And then move the new tower to the other tower's place and get rid of the other tower. So you see, it's easier to visualize what's going on in the code with the shorthand version. - Less resource consuming. The computer is just adding more legos to the existing tower, instead of taking the time and effort to build another tower and all that noise. Here's the solution you came up with: $mysql = $mysql .= " Where Name like '%$SearchA%' "; This is even worse than doing $mysql = $mysql . " Person "; This code causes a copy of $mysql to be created twice. The computer has to perform two separate operations in your code. The inner operation is to concat the string to $mysql but first php has to make a copy of $mysql to do that inner operation. Then another temp var is made, which is the result of that inner operation. Then it is finally using in the outer operation, by being assigned to the original $mysql. Note: differences in execution time, memory/resource consumption, etc.. are negligible. Your computer probably has a dim idea of the difference, as it might struggle to perform the math at that kind of precision. You and your users certainly will not notice the difference. So saying your solution is "bad" wouldn't entirely be fair. But it is the difference between good code and great code. Quote Link to comment https://forums.phpfreaks.com/topic/141476-solved-string-concatenation/#findComment-740565 Share on other sites More sharing options...
dizzy1 Posted January 19, 2009 Author Share Posted January 19, 2009 Just Figured it out, ARHHHH. $mysql = " Select * from "; $mysql = $mysql .= " Person "; $mysql = $mysql .= " Where Name like '%$SearchA%' "; Thanks Anyway Though that technically works, I thought I'd tell you why it works, as well as how to do it better. the dot (.) is the concatenation operator. It is javascript's equivalent of the plus sign (+). So looking at your original attempt of: $mysql = $mysql + " Person "; It really should have been $mysql = $mysql . " Person "; That is to say: concatenate the current value of $mysql and the string " Person " and assign the result to $mysql. A better way to do it that doesn't require as much resources and time, is to use the shorthand version: $mysql .= " Person "; This says: append the string " Person " to the end of $mysql. The advantages to this are: - Code is shorter shorter (less code to be parsed), so the script executes faster. - Concept is less abstract. If you visualize a string variable as lego blocks being stacked up. It's easy to visualize just adding another block or two on top of the tower you're building (or whatever kids build these days). Using the same lego blocks analogy with the previous example $mysql = $mysql . " Person ";, you would have to visualize your current tower of blocks, build another one that's a copy of it. Add the new blocks to that. And then move the new tower to the other tower's place and get rid of the other tower. So you see, it's easier to visualize what's going on in the code with the shorthand version. - Less resource consuming. The computer is just adding more legos to the existing tower, instead of taking the time and effort to build another tower and all that noise. Here's the solution you came up with: $mysql = $mysql .= " Where Name like '%$SearchA%' "; This is even worse than doing $mysql = $mysql . " Person "; This code causes a copy of $mysql to be created twice. The computer has to perform two separate operations in your code. The inner operation is to concat the string to $mysql but first php has to make a copy of $mysql to do that inner operation. Then another temp var is made, which is the result of that inner operation. Then it is finally using in the outer operation, by being assigned to the original $mysql. Note: differences in execution time, memory/resource consumption, etc.. are negligible. Your computer probably has a dim idea of the difference, as it might struggle to perform the math at that kind of precision. You and your users certainly will not notice the difference. So saying your solution is "bad" wouldn't entirely be fair. But it is the difference between good code and great code. LOL cheers for the brill explanation i always try to code to shortest way i know possible, as I get more used to php i will be able to find quicker ways of coding. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/141476-solved-string-concatenation/#findComment-740626 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.