0

I have the following code in php to get variables from a query string

 $first_name = $_GET['firstName'];
 echo $first_name;

when the query string only contains one value like

index.php/details?firstName=Austin

the value appears on the page like

but for some reason when I try and pass two separate values in the query string like

index.php/details?firstName=Austin&lastName=Davis

and trying to make the values of the variables appear on the next page like so

$first_name = $_GET['firstName'];
$last_name = $_GET['lastName'];
echo $first_name + $last_name;

the value zero appears on the webpage. Why can't pass two values in a query string using the & operator.

4

1 回答 1

2

+ is the addition operator in php what you are looking for is . which is the concatenation operator. See more on PHP's Operators

Change

echo $first_name+$last_name;

to

echo $first_name.$last_name;

or better yet

echo $first_name.' '.$last_name;

By using the addition operator you are basically casting the left and right hand side of the operator to numbers (both of which will result in 0 and the sum of which is 0).

于 2013-08-02T02:55:21.567 回答