PHP Data Types: Strings
Strings are a sequence of characters. In PHP, a character is the same as a byte, therefore there are exactly 256 different characters possible. The long string is supported in PHP, in fact, there is no practical bound to the size of strings. But PHP has no native support for Unicode.
A string literal can be specified in three different ways.
PHP Strings: Single quoted
The simple way to print a string is to enclose it in a single quote ( use the character ' ). If you want to print a single quote (') within a string then you must escape it with a backslash like many other languages. If a backslash (\) needs to print before a single quote or at the end of the string then the backslash must occur twice. At the end of the every string, a html break character has added for line breaking.
<?php echo 'One line simple string.<br />'; echo 'Two line simple string example<br />'; echo 'Tomorrow I \'ll learn PHP global variables.<br />'; echo 'This is a bad command : del c:\\*.* <br />'; ?>
The above echo statements display single quoted strings. In the second echo statement, we have written the string in two lines though it will be displayed in a single line. In the third echo statement a single quotation character (') is printed for the backslash character. In the fourth echo statement, a backslash character(\) has printed after c:, here we have taken two backslash characters together.
PHP Strings : Double quoted
When we want to print some special characters or the values of variables within a string then we enclose the string with double-quotes(") character.
itemscope itemtype="http://schema.org/WebPageElement/Heading">Following character sequences beginning with a backslash (\) are replaced with special character
|\r||carriage return (CR)|
|\t||horizontal tab (HT)|
|\v||vertical tab (VT)|
|\f||form feed (FF)|
<?php echo "Example ::\n Linefeed string <br />"; echo "Example ::\r Carriage return <br />"; echo "Example ::\t Horizontal tab <br />"; echo "Example ::\v Vertical tab <br />"; echo "Example ::\f Form feed <br />"; echo "Example ::\\ Backslash <br />"; echo "Example ::\" Double quote\" <br />"; echo "$ is replaced by the dollar sign itself <br />"; echo "Single quote within a double quote : I 'll go tomorrow.<br />" ; echo "This is bad command : del c:\*.*"; ?>
Variable in double quoted string :
Whenever a $ symbol appears within a string, PHP tries to read the immediate part followed the $ character as a variable name. If the variable type is a string, that string is inserted into the string in that position, if the variable type is a non-string then it is converted into string type.
<?php $samt=2000; $desc="My salary amount for this month is: "; echo "$desc $samt <br />"; // We can write the above example in the following way echo "My salary amount for this month is: $$samt"; ?>
PHP Strings: heredocs
In addition to the single-quote and double-quote syntaxes, there is an another way to embed large pieces of text in your scripts which may include lots of double and single quotes.
<<<identifier ....text here ....... ....text here ....... ....text here ....... identifier
<?php $mystring=<<<MYID "Tomorrow I 'll not go at your house" "may be another day." 'Thank you' MYID; echo $mystring; ?>
PHP: Tips of the Day
PHP is a weakly-typed language. It does not require explicit declaration of data types. The context in which the variable is used determines its data type; conversion is done automatically:
$x = "3"; // string $x = $x + 3; // integer (6) $x = $x + 0.5; // float (6.5) $x = 1 + "2 oranges"; // integer (3)
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework