About Strings

The first—arguably most important—variable type I’ll go into is strings. A string is merely a quoted chunk of letters, numbers, spaces, punctuation, and so forth. These are all strings:

  • 'Tobias'

  • "In watermelon sugar"

  • '1,000'

  • 'February 3, 2005'

To make a string variable, assign a string value to a valid variable name:

$first_name = 'Tobias';

$today = 'February 3, 2005';

To print out the value of a string, use either echo() or print():

echo $first_name;

To print the value of string within a context, use double quotation marks:

echo "Hello, $first_name";

In a way, you’ve already worked with strings once—when using the predefined variables in the preceding section.

To use strings

1.
Create a new HTML document in your text editor, ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.