Peter B. was an out-of-work PHP developer looking for contract work in early 2005. A recruiter he'd worked with in the past emailed him some information regarding a possible position. Reading the job description, Peter thought he'd be a good fit, so he submitted his resume and got a response via email a few days later.
The hiring manager described their typical process; Peter would have to answer a screening question to determine his skill level, and if his answer was satisfactory, they'd schedule a face-to-face interview. With a little trepidation, Peter said he was ready for the question. He was concerned that it could be about a complex topic that he wasn't very familiar with. A few hours later, an email arrived with the subject "SCREENING QUESTION," flagged with high importance.
His mouse hovering over the email, he expected to open it and have to answer "on a PB349 microprocessor, if memory address 0xa9f00c contains a MOV instruction to memory address 0x8ad9da, what is the magnetic force dispensed by a 64KB memory module for the next 600 instructions? You have thirty seconds."
Peter took a deep breath and clicked on the email. Here is the exact question he was asked: "Describe what concatenation is, how it applies to PHP, and how you've used it in the past."
Peter was surprised. It was a question that anyone with any basic exposure to any modern programming language could answer. It would've been harder to describe what multiplication was and how he'd used it in the past.
Still, he wanted to show that he understood the concept, so he played along. He typed up a detailed response.
String concatenation is often used in PHP to build a string of HTML for output to the client (browser). This is common in prodecural-based PHP code. However, I should note that oftentimes, that using concatenation for HTML generation is inefficient or can be better served by some other design pattern - particulary if the developer is using concatention during an echo operation (in this case, comma-separating the tokens is faster).
Another common use for string concatenation is the generation of dynamic SQL queries. For example, if I had a CMS that was to pull all articles written by a certain user, the code might look something like this
$sql = "SELECT article_id, article_body FROM Articles WHERE author_id = " . $User->getID() . " ORDER BY article_date DESC";
As you can see, the above code combines three tokens to generate a complete SQL query.
- A SQL fragment
- The user's ID as pulled from a custom User object
- A SQL fragment
SQL queries are rarely generated without some sort of dynamic data to alter their structure, so this is a very common task that I've used in just about every web application I've written. Some other simple examples include cookie generation, error message generation, email headers, and dynamic URL construction.
I hope this sufficiently explains concatenation in general, how it relates to PHP, and my experience with using this basic operator.
Peter sent the email and got a phone call a few days later.
Lisa: Hi, Peter? This is Lisa from Concatcorp.
Peter: Oh, hi! Good to hear from you! I hope you have news about the job.
Lisa: Well, yes, but...
Lisa: We're going to offer the position to another candidate.
Peter: I see... may I ask why? I thought I did a pretty thorough job answering the screening question...
Lisa: Well, that's just it. The problem is that they think your answer was too good. They think you plagiarized it. I'm sorry.
It was then that Peter realized he was probably better off without that job.