The string variables can contain alphanumeric characters. Strings are created when;
You declare variable and assign string characters to it
You can directly use PHP Strings with echo statement.
PHP String functions are language construct, it helps capture words.
Learning how strings work in PHP and how to manipulate them will make you a very effective and productive developer.
In this PHP Tutorial, you will learn-
What is String in PHP? PHP Create Strings Using Single quotes with Example PHP Create Strings Using Double quotes with Example PHP Heredoc with Example PHP Nowdoc with Example PHP String Function Examples
PHP Create Strings Using Single quotes with Example Let’s now look at the four different ways of creating PHP string functions and string manipulation in PHP. Creating PHP Strings Using Single quotes: The simplest way to create a string is to use single quotes. Let’s look at an example that creates a simple string in PHP. Output: If the single quote is part of the string value, it can be escaped using the backslash. The code below illustrates how to escape a single quote. Output:
PHP Create Strings Using Double quotes with Example The double quotes are used to create relatively complex strings compared to single quotes. Variable names can be used inside double quotes and their values will be displayed. Let’s look at an example.
HERE,
The above example creates a simple string with the value of Alicia.
The variable name is then used in the string created using double quotes and its value is interpolated at run time.
Output: In addition to variable interpolations, the double quote string can also escape more special characters such as “\n for a linefeed, $ dollar for the dollar sign” etc. More examples Let’s suppose that we have the following code Output: executing the above codes issues a notice “Notice: Undefined variable”. This is because $word is treated as a variable. If we want the dollar sign to be treated as a literal value, we have to escape it. Output:
PHP Heredoc with Example This heredoc methodology is used to create fairly complex strings as compared to double quotes. The heredoc supports all the features of double quotes and allows creating string values with more than one line without PHP string concatenation. Using double quotes to create strings that have multiple lines generates an error. You can also use double quotes inside without escaping them. The example below illustrates how the Heredoc method is used to create string values. HERE, «<EOT is the string delimiter. EOT is the acronym for end of text. It should be defined in its on line at the beginning of the string and at the end. Note: you can use anything you like in place of EOT
Output:
PHP Nowdoc with Example The Nowdoc string creation method is similar to the heredoc method but works like the way single quotes work. No parsing takes place inside the Nowdoc. Nowdoc is ideal when working with raw data that do not need to be parsed. The code below shows the Nowdoc implementation Output:
PHP String Function Examples String functions in PHP are used to manipulate string values. We are now going to look at some of the commonly used string functions in PHP For a complete list of PHP strings, check https://php.net/manual/en/ref.strings.php
Summary
Define string in PHP: A string function in PHP is a set of characters
Explain string function in PHP: Strings are created when you declare a variable and assign string characters to it.
Single quotes are used to specify simple strings in PHP
Double quotes are used to create fairly complex strings in PHP
heredoc is used to create complex strings
Nowdoc is used to create strings that cannot be parsed.