If you’ve been following the beginner tutorial, you have already been using PHP strings. A string, also known as a character string, is a series of characters (letters, numbers, punctuations).
This is a string: <?php $variable = “Hello universe.”; ?>
This is a string: <?php $variable = ‘Hello universe.’; ?>
This is a string: <?php $variable = “30″; ?>
This is NOT a string: <?php $variable = 30; ?>
Notice in the last line that 30 has no single or double quotes around it. Without the quotes, PHP won’t store this value as a character in a string. It will store it as a number, which is useful if we plan to use any arithmetic with the numbers.
You may be wondering when to use single versus double quotes. There are some differences on how they each work and ultimately it is up to the programmer’s choice. Let’s explore some of the differences now.
1. If WampServer is not running, go ahead and start it. Then open your code editor, create a new file called strings.php and save it to your “introduction” directory under c:\wamp\www.
2. Type in the code below (with or without the comments — your choice):
<html> <head> <title>Strings</title> </head> <body> <?php $age = 20; // Set the $age variable to 20. echo 'I am {$age} years old.'; // This is using single quotes. echo "<br />"; // This HTML code inserts a new line. echo "I am {$age} years old."; // This is using double quotes. ?> </body> </html>
When including a variable inside of a string, it is best practice to surround it with curly brackets like {$variable}.
3. Save the file. In your Web browser, go to: http://localhost/introduction/strings.php
Note the difference between the first line and the second. Double quotes allow us to output the variable data whereas single quotes shows us the variable name itself.
Let’s say we just want to show the variable name itself using double quotes. We can do that too:
<html> <head> <title>Strings</title> </head> <body> <?php $age = 20; // Set the $age variable to 20. echo "I am \$age years old."; // This is using double quotes. ?> </body> </html>
By preceding the $age variable with a backslash (\), we are able to echo the variable name itself.
4. If we want to, we can use single quotes and output the variable data as well. Try this in your strings.php file:
<html> <head> <title>Strings</title> </head> <body> <?php $age = 20; // Set the $age variable to 20. echo 'I am ' . $age . ' years old.'; // This is using single quotes. ?> </body> </html>
If you view this page in the Web browser, it shows the $age data correctly. This is an example of using periods to concatenate your code, which we originally discussed in PHP Statements.
5. By using concatenation, we can also span our strings across multiple lines or combine variables. Here’s an example to try:
<html> <head> <title>Strings</title> </head> <body> <?php $dog1 = "Spot"; // First dog is named Spot $dog2 = "Buddy"; // Second dog is named Buddy $dogs = $dog1 . " and "; $dogs .= $dog2; // Output the string to the browser using the $dogs variable. echo $dogs; ?> </body> </html>
With the .=, we are able to use multiple lines. If you have a very long string, this is a good thing to do for readability. If you save and load the strings.php file, you’ll see it prints: Spot and Buddy.
Go back to Building PHP Scripts.