|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Understanding String Literals and Operations
By: FYICenter.com
Part:
1
2
3
4
A collection of 14 tips on PHP string literals, operations and conversaion. Clear explanations and tutorial exercises are provided on single-quoted strings, double-quoted strings, string elements, concatenation, converting values to strings, converting strings to values.
Topics included in this collections:
- What Are the Special Characters You Need to Escape in Single-Quoted Stings?
- Can You Specify the "new line" Character in Single-Quoted Strings?
- How Many Escape Sequences Are Recognized in Single-Quoted Strings?
- What Are the Special Characters You Need to Escape in Double-Quoted Stings?
- How Many Escape Sequences Are Recognized in Double-Quoted Strings?
- How To Include Variables in Double-Quoted Strings?
- How Many Ways to Include Variables in Double-Quoted Strings?
- How Many Ways to Include Array Elements in Double-Quoted Strings?
- How To Access a Specific Character in a String?
- How To Assigning a New Character in a String?
- How To Concatenate Two Strings Together?
- How To Compare Two Strings with Comparison Operators?
- How To Convert Numbers to Strings?
- How To Convert Strings to Numbers?
What Are the Special Characters You Need to Escape in Single-Quoted Stings?
There are two special characters you need to escape in a single-quote string: the single quote (')
and the back slash (\). Here is a PHP script example of single-quoted strings:
<?php
echo 'Hello world!';
echo 'It\'s Friday!';
echo '\\ represents an operator.';
?>
This script will print:
Hello world!It's Friday!\ represents an operator.
Can You Specify the "new line" Character in Single-Quoted Strings?
You can not specify the "new line" character in a single-quoted string. If you don't believe, try this script:
<?php
echo '\n will not work in single quoted strings.';
?>
This script will print:
\n will not work in single quoted strings.
How Many Escape Sequences Are Recognized in Single-Quoted Strings?
There are 2 escape sequences you can use in single-quoted strings:
- \\ - Represents the back slash character.
- \' - Represents the single quote character.
What Are the Special Characters You Need to Escape in Double-Quoted Stings?
There are two special characters you need to escape in a double-quote string: the double quote (")
and the back slash (\). Here is a PHP script example of double-quoted strings:
<?php
echo "Hello world!";
echo "Tom said: \"Who's there?\"";
echo "\\ represents an operator.";
?>
This script will print:
Hello world!Tom said: "Who's there?"\ represents an operator.
How Many Escape Sequences Are Recognized in Double-Quoted Strings?
There are 12 escape sequences you can use in double-quoted strings:
- \\ - Represents the back slash character.
- \" - Represents the double quote character.
- \$ - Represents the dollar sign.
- \n - Represents the new line character (ASCII code 10).
- \r - Represents the carriage return character (ASCII code 13).
- \t - Represents the tab character (ASCII code 9).
- \{ - Represents the open brace character.
- \} - Represents the close brace character.
- \[ - Represents the open bracket character.
- \] - Represents the close bracket character.
- \nnn - Represents a character as an octal value.
- \xnn - Represents a character as a hex value.
How To Include Variables in Double-Quoted Strings?
Variables included in double-quoted strings will be interpolated. Their values will be concatenated
into the enclosing strings. For example, two statements in the following PHP script will print out the same string:
<?php
$variable = "and";
echo "part 1 $variable part 2\n";
echo "part 1 ".$variable." part 2\n";
?>
This script will print:
part 1 and part 2
part 1 and part 2
(Continued on next part...)
Part:
1
2
3
4
|