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.Code:
- <?php
- $str_1 = 'Demo text';
- $str_2 = 'Demo text with single quote(\')';
- echo $str_1;
- echo $str_2;
- ?>
PHP F1 - 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.Code:
- <?php
- $str_1 = "Demo text";
- $str_2 = "Demo text with double quote(\")\r\n";
- $demo = "Internal";
- $str_3 = "This is a $demo string";
- echo $str_1;
- echo $str_2;
- echo $str_3;
- ?>
PHP F1
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:Code:
- <?php
- $str_1 = <<<DEMO
- This is a heredoc message
- Text.
- DEMO;
- echo $str_1;
- ?>
PHP F1
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.)
Step 2 - Variable parsing in string
PHP string tutorial
Time to time you need to compose a complete text from sub strings and variables. For example if you want to greet your visitor you want to display a text like:
"Hello Peter, nice to see you again!"
Of course you can not hard code the user name but you want to use a variable like $userName. If you use single quoted strings then you can display your text like this:
Code:
<?php $userName = "Peter"; echo 'Hello '.$userName.', nice to see you again!'; ?>PHP F1
Using double quotes you can make it more simple:
Code:
<?php $userName = "Peter"; echo "Hello $userName, nice to see you again!"; ?>PHP F1
The more variable you need to use the bigger is the difference.
After the basic case let's see some more complex variable parsing. What if you want to display the text:
"Peters website"
Using the above examples the $userName variable contains only the string Peter and if you compose your string as before:
Code:
<?php $userName = "Peter"; echo "$userNames website"; ?>PHP F1
You will get a warning and the result is not what you expected:
Output:Notice: Undefined variable: userNames in Z:\wdocs\test.php on line 4 website
To solve this problem you need to enclose the variable in curly braces like in the following example:
Code:
<?php $userName = "Peter"; echo "${userName}s website"; echo "{$userName}s website"; ?>PHP F1
As you can see it is possible to enclose only the variable name without the $ sign or the complete variable.
You have to use this solution in case of arrays as well in the following way:
Code:
<?php $users = array('U1' => "Peter"); echo "Hello {$users['U1']}, nice to see you again!"; ?>PHP F1
You can read about string concatenation in the next section.
Step 3 - String concatenation, manipulation
PHP string tutorial
As you could see on the previous page we used string concatenation in our example codes. Now let's see how we can compose a string in details.
You can concatenate string in PHP using the dot (.) operator. You can do this more ways as you can see in the following example code:
Code:
<?php // Use only one type of quoting $str_1 = "This "."is"." a "."test message"; // Use mixed quoting $str_2 = 'This '."is a".' test message'; // Or devide it into more lines $str_3 = "This"; $str_3 .= " is a"; $str_3 .= " test message"; $str_4 = "This" . " is a" . " test message"; ?>PHP F1
String modification
Here I only mention the string modification by characters. As you know strings a a series of characters. It means you can use it as a string. So if you have a string variable $name with a value "Peter" then you can access the firs letter (P) as $name[0].
Code:
<?php $name = 'Peter'; for ($i = 0; $i< 5; $i++){ echo "{$name[$i]}<br/>"; } ?>PHP F1
And if you can access it you can of course modify the elements one by one like this:
Code:
<?php $name = 'Peter'; $name[0] = 'M'; $name[4] = 'o'; $name[5] = 'r'; echo $name ?>PHP F1
For more special string manipulation function visit our other specialized tutorials (coming soon)
discuss this topic to forum
