PHP string tutorial
A detailed description how to use PHP strings and string manipulation functions.
|Name:||PHP string tutorial|
Bookmark PHP string tutorial
Step 1 - PHP string basic
PHP string tutorial
String is a type in PHP and it is a series of characters. There are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.
However I have a good news as well as there is no limitation in string length in PHP (Of course available memory and PHP ini settings can reduce it).
You can set a string in three different ways. It's up to you what method do you use, all have pros.
- The first and most known way is to specify a string in single quotes. Similar to other programing languages you need to use backslash '\' if you want to display a single quote in your text.
- The second option to use double quotes. Between double quotes you can use more escape character in your string. Besides this if you put a variable in the string then PHP will interpret it and display the content of the variable.
Here the most important part is the string defined as $str_3 and its output in line 8 will result this:Output:This is a Internal string
- And last you can use heredoc as well. In this case you define your text between heredoc identifiers. In this case it is DEMO. You need to start it with the operator <<< and then the identifier. If you are ready you need to close the heredoc part by adding the identifier again at the beginning of a new line like this:
As you can see the output is in the same formatting - text indent, new lines - as it was defined. You can use variables in case of heredoc as well as in case of double quoted strings.
The 2. case is the most interesting string definition version so let's see a bit more how to insert a variable inside a string. (You can use variables in case 3. as well but heredoc is not so common.)
Next Step of PHP string tutorial
Tags: php string, php string function, string, php