Jump to content
Larry Ullman's Book Forums

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Nice explanation, I especially like this part:

Also note that numbers are added together with a plus sign, and strings are added together with a period.

Makes it click in your head nicely!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...