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:

  • 'Joe'

  • "In watermelon sugar"

  • '1,000'

  • 'January 9, 2003'

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

$first_name = 'Joe';
$today = 'January 9, 2003';

To print out the value of a string, use either echo() or print(), along with double quotation marks or none at all:

echo "Hello, $first_name";
echo $first_name;

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

To use strings:

1.
Create a new HTML document in your text editor, along with the PHP tags (Script ...

Get PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide 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.