PHP Code:
$my_string = "Tizag - Unlock your potential!"; echo "Tizag - Unlock your potential!"; echo $my_string;
Display:
Elearn - Unlock your potential!
Elearn - Unlock your potential!
PHP - String Creation Single Quotes
Thus far we have created strings using double-quotes, but it is just as correct to create a string using single-quotes, otherwise known as apostrophes.PHP Code:
$my_string = 'Elearn - Unlock your potential!'; echo 'Elearn - Unlock your potential!'; echo $my_string;
echo 'Elearn - It\'s Neat!';
PHP - String Creation Double-Quotes
We have used double-quotes and will continue to use them as the primary method for forming strings. Double-quotes allow for many special escaped characters to be used that you cannot do with a single-quote string. Once again, a backslash is used to escape a character.PHP Code:
$newline = "A newline is \n"; $return = "A carriage return is \r"; $tab = "A tab is \t"; $dollar = "A dollar sign is \$"; $doublequote = "A double-quote is \"";
These escaped characters are not very useful for outputting to a web page because HTML ignore extra white space. A tab, newline, and carriage return are all examples of extra (ignorable) white space. However, when writing to a file that may be read by human eyes these escaped characters are a valuable tool!
The Concatenation Operator
There is only one string operator in PHP.The concatenation operator (.) is used to put two string values together.
To concatenate two string variables together, use the concatenation operator:
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?
The output of the code above will be:
Hello World! What a nice day!
The reference contains a brief description, and examples of use, for each function!
There are a few very important things to remember when using heredoc.
Complete PHP String Reference
For a complete reference of all string functions, go to our complete PHP String Reference.The reference contains a brief description, and examples of use, for each function!
PHP - String Creation Heredoc
The two methods above are the traditional way to create strings in most programming languages. PHP introduces a more robust string creation tool called heredoc that lets the programmer create multi-line strings without using quotations. However, creating a string using heredoc is more difficult and can lead to problems if you do not properly code your string! Here's how to do it:PHP Code:
$my_string = <<<TEST elearnsite.com Webmaster Tutorials Unlock your potential! TEST; echo $my_string;
- Use <<< and some identifier that you choose to begin the heredoc. In this example we chose TEST as our identifier.
- Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was TEST;
- The closing sequence TEST; must occur on a line by itself and cannot be indented!
Display:
elearnsite.com Webmaster Tutorials Unlock your potential!
Once again, take great care in following the heredoc creation guidelines to avoid any headaches.
0 comments:
Post a Comment