Jump to content
Larry Ullman's Book Forums

Splitting Hairs With Variables/arrays

Recommended Posts

From the book:


Create two different name variables, using the existing first- and last-name variables:
$ name1 = '$ first_name $ last_name';
$ name2 = "$ first_name $ last_name";
Technically speaking-- why aren't $name1 and $name2 arrays?
I keep telling myself that arrays are variables with two or more values.
Link to comment
Share on other sites

Arrays are variables that can contain multiple values (but they can also contain just one value (e.g., $arr = array(5)), or no values (e.g., $arr = array())).


However, adding multiple values together does not equal an array. The result of adding two values together depends on the types of the two values being added together.

For example, if you add two strings together, you get a single string, which is a combination of the two strings that were added together. If you add two numbers together, you get the summation of those numbers. Also note that numbers are added together with a plus sign, and strings are added together with a period. (The combining of strings is often referred to as "string concatenation".)


As an example, let's say we have the following:

$first_name = 'Bill';
$last_name = 'Smith';

$full_name = $first_name . ' ' . $last_name;

With that code, $full_name will now be equal to the single string 'Bill Smith'. Note that $full_name is a string, not an array.

Also note that the following two statements will result in the same string being assigned to $full_name:

$full_name = $first_name . ' ' . $last_name;
$full_name = "$first_name $last_name";

That make sense?

  • Upvote 2
Link to comment
Share on other sites


  • Create New...