PHP Tutorial
How do I create a string variable using PHP?
A string is a collection of characters that are enclosed in a single or double
quotation marks. The information in a string is treated as a scalar value, or a
single piece of information. The quotation marks identify the beginning and end
of a string and allow PHP to work with the string as one piece of information.
The following is an example of creating a string.
<html>
<head>
<title>String Variable</title>
</head>
<body>
<? //Use the double quotes (" ") if you want the output to be evaluated
$website = "<b>My Website</b>"; //The output will be: Welcome to My Website
$welcome = "Welcome to $website<br>"; //The output will be: Welcome to $website
$variable = 'Welcome to $website<br>'; //Prints out on the screen the value of
welcome
print $welcome; //Prints out on the screen the value of variable
print $variable;
?>
</body>
</html>
| Escape
Sequence |
Description |
| \n |
Starts a new line |
| \r |
Inserts a carriage return |
| \t |
Inserts a tab |
| \\ |
Inserts a backslash
|
| \" |
Inserts a double quotation mark |
| \$ |
Inserts a dollar sign
|
| \012 |
Use an octal ASCII value
|
| \x0A |
Use a hexadecimal ASCII value
|
Strings can be joined together using the
concatenation operator (.). The concatenation operator can be used to join 2
strings, such as "Tom" . "Smith", or combination of string sand variables, such
as $myString = "I am " . $myAge . " years old".
PHP is often used to output HTML code. When assigning an HTML tag, such as <img
src="image.gif" width="100" height="100"> to a string variable, you must use
escape sequences to include any special characters, such as quotation marks,
within the tag. The HTML code with in the string will be processed by the web
server and applied to the output for a client's web browser. An example of this
is:
$myImageTag = "<img src=\"image.gif\" width=\"100\" height=\"100\">";