A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation marks ('), like this:
$my_string = 'Hello World';
You can also use double quotation marks ("). However, single and double quotation marks work in different ways. Strings enclosed in single-quotes are treated almost literally, whereas the strings delimited by the double quotes replace variables with the string representations of their values as well as specially interpreting certain escape sequences.
A PHP string is a sequence of characters i.e. used to store and manipulate text. There are 4 ways to specify string in PHP.
The escape-sequence replacements are:
Type String Method
We can create a string in PHP by enclosing text in a single quote. It is the easiest way to specify string in PHP.
<?php
$sVal='Hello Single Quoted String';
echo $sVal;
?>
Output: Hello Single Quoted String
Double Quoted PHP String
In PHP, we can specify string through enclosing text within double quote also. But escape sequences and variables will be interpreted using double quote PHP strings.
<?php
$sDouble="Hello Php within double quote";
echo $sDouble;
?>
Output: Hello Php within double quote
Double Quoted PHP String Working Step :
The above example creates a simple string with the value of sDouble.
The variable name is then used in the string created using double quotes and its value is interpolated at run time.
In double quoted strings, variable will be interpreted.
<?php
$num=10;
echo "Number is: $num";
?>
Output: Number is: 10
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning