The Space Character Trap in PHP $_GET Parsing
You are encountering a classic edge case in web development: string truncation caused by improper quote handling inside URL parameters. In your specific scenario, you have a company name like "Chiti Technologies Ltd", but when it is passed via the query string and retrieved using $_GET, only "Chiti" appears on the destination page.
The root cause lies in how the server interprets the data before it reaches your PHP script. The $_GET superglobal array does not store raw strings; it stores parsed data based on the URL encoding standard. When you echo a variable containing spaces directly into an href attribute without wrapping it in quotes, the browser's URL parser treats the space character as a delimiter between parameters rather than part of the value.
How the Browser Parses Unquoted Parameters
Consider the code snippet you provided:
echo "<a href=view_exp.php?compna=",$compname,">$compname</a>";
If $compname holds the value Chiti Technologies Ltd, the resulting HTML output looks like this:
<a href=view_exp.php?compna=Chiti Technologies Ltd>Chiti Technologies Ltd</a>
Notice the lack of quotes around the URL. When a browser encounters a space in an unquoted query string, it splits the string at that space to identify individual parameters. It interprets the segment Chiti as the first parameter for compna, and Technologies as a completely new, unexpected variable name. Consequently, $_GET['compna'] receives only "Chiti", while $_GET['Technologies'] might receive "Ltd".
This behavior is defined by RFC 3986 (Uniform Resource Identifier). While modern browsers are forgiving and often tolerate unquoted strings with spaces in simple cases, relying on this behavior for data transfer is a recipe for bugs, especially when values contain special characters like &, =, or #.
The Correct Approach: URL Encoding and Quoting
To reliably pass multi-word values, you must ensure the value is treated as a single unit. There are two primary ways to achieve this in PHP: using quotes in the output or using urlencode().
Option 1: Wrap the Parameter in Quotes
The simplest fix that requires no additional functions is to wrap the entire query string value in double quotes within the href attribute. This tells the browser explicitly that everything between the equals sign and the closing quote belongs to that specific parameter.
echo '<a href="view_exp.php?compna=' . $compname . '">' . $compname . '</a>';
By adding the quotes, the URL becomes:
<a href="view_exp.php?compna=Chiti Technologies Ltd">Chiti Technologies Ltd</a>
The browser now correctly identifies "Chiti Technologies Ltd" as the complete value for compna. On the second page, your original retrieval logic will work perfectly:
$compname = $_GET['compna'];
// $compname is now "Chiti Technologies Ltd"
Option 2: URL Encoding with urlencode()
A more robust practice, particularly for production applications, is to use PHP's built-in urlencode() function. This function converts spaces into %20 and escapes other special characters that might break the URL structure later. On the receiving end, you must use urldecode() to reverse this process.
// Link generation
$encodedName = urlencode($compname);
echo '<a href="view_exp.php?compna=' . $encodedName . '">' . $compname . '</a>';
// Retrieval
$rawValue = $_GET['compna']; // This will be "Chiti+Technologies+Ltd" or "%20..."
$decodedName = urldecode($rawValue);
While wrapping in quotes (Option 1) is sufficient for simple display links, urlencode() (Option 2) is essential if you plan to use these values in database queries, file paths, or any logic where the character set integrity is critical.
Security Considerations
Beyond functionality, passing unquoted data poses a security risk known as Cross-Site Scripting (XSS). If $compname contained user-generated content like <script>alert(1)</script>, and you output it directly without encoding, an attacker could execute JavaScript in the browser of anyone clicking that link.
Using htmlspecialchars() when echoing the display text ensures that special HTML characters are escaped:
echo '<a href="view_exp.php?compna=' . urlencode($compname) . '">'
. htmlspecialchars($compname, ENT_QUOTES, 'UTF-8') . '</a>';
This combination guarantees that the data is transmitted correctly across the space barrier and remains safe from injection attacks. For projects built on modern stacks, frameworks like Laravel handle this encoding automatically when generating links via url() helpers, abstracting away these low-level details.