Add double quotes to string php
When i echo "$time"; - The output is 2015-07-27 18:17:47 But i need to output as "2015-07-27 18:17:47". Show I have been trying various string concatenations such as : echo "."$time"."; But couldn't get the desired output? What is the best way to do it? asked Jul 27, 2015 at 23:25
Try this concatenation
or use
answered Jul 27, 2015 at 23:29
RiggsFollyRiggsFolly 91.1k20 gold badges101 silver badges145 bronze badges Just escape them:
You could also use single around the double quotes:
See here for more info on escape sequences when using double quotes. answered Jul 27, 2015 at 23:28
rjdownrjdown 8,9823 gold badges28 silver badges44 bronze badges 0 A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. See details of the string type.
SyntaxA string literal can be specified in four different ways:
Single quoted The simplest way to specify a string is to enclose it in single quotes (the character To specify a literal single quote, escape it with a backslash (
'You can also have embedded newlines in Double quotedIf the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters: Escaped characters
As in single quoted strings, escaping any other character will result in the backslash being printed too. The most important feature of double-quoted strings is the fact that variable names will be expanded. See string parsing for details. Heredoc A
third way to delimit strings is the heredoc syntax: The closing identifier may be indented by space or tab, in which case the indentation will be stripped from all lines in the doc string. Prior to PHP 7.3.0, the closing identifier must begin in the first column of the line. Also, the closing identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. Example #1 Basic Heredoc example as of PHP 7.3.0
Output of the above example in PHP 7.3: If the closing identifier is indented further than any lines of the body, then a ParseError will be thrown: Example #2 Closing identifier must not be indented further than any lines of the body
Output of the above example in PHP 7.3: PHP Parse error: Invalid body indentation level (expecting an indentation level of at least 3) in example.php on line 4 If the closing identifier is indented, tabs can be used as well, however, tabs and spaces must not be intermixed regarding the indentation of the closing identifier and the indentation of the body (up to the closing identifier). In any of these cases, a ParseError will be thrown. These whitespace constraints have been included because mixing tabs and spaces for indentation is harmful to legibility. Example #3 Different indentation for body (spaces) closing identifier
{
Output of the above example in PHP 7.3: PHP Parse error: Invalid indentation - tabs and spaces cannot be mixed in example.php line 8 The closing identifier for the body string is not required to be followed by a semicolon or newline. For example, the following code is allowed as of PHP 7.3.0: Example #4 Continuing an expression after a closing identifier
Output of the above example in PHP 7.3: array(2) { [0] => string(11) "a b c" [1] => string(5) "d e f" } Warning If the closing identifier was found at the start of a line, then regardless of whether it was a part of another word, it may be considered as the closing identifier and causes a ParseError. Example #5 Closing identifier in body of the string tends to cause ParseError
Output of the above example in PHP 7.3: PHP Parse error: syntax error, unexpected identifier "ING", expecting "]" in example.php on line 6 To avoid this problem, it is safe for you to follow the simple rule: do not choose the closing identifier that appears in the body of the text. Warning Prior to PHP 7.3.0, it is very important to note that the line with the closing identifier must contain no other characters, except a semicolon ( If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line. Example #6 Invalid example, prior to PHP 7.3.0
Example #7 Valid example, even if prior to PHP 7.3.0
Heredocs containing variables can not be used for initializing class properties. Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings. Example #8 Heredoc string quoting example
__construct() The above example will output: My name is "MyName". I am printing some Foo. Now, I am printing some Bar2. This should print a capital 'A': A It is also possible to use the Heredoc syntax to pass data to function arguments: Example #9 Heredoc in arguments example
It's possible to initialize static variables and class properties/constants using the Heredoc syntax: Example #10 Using Heredoc to initialize static values
$baz = << The opening Heredoc identifier may optionally be enclosed in double quotes: Example #11 Using double quotes in Heredoc
Nowdoc Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML A nowdoc is identified with the same Example #12 Nowdoc string quoting example
The above example will output: Example of string spanning multiple lines using nowdoc syntax. Backslashes are always treated literally, e.g. \\ and \'. Example #13 Nowdoc string quoting example with variables
__construct() The above example will output: My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital 'A': \x41 Example #14 Static data example
Variable parsingWhen a string is specified in double quotes or with heredoc, variables are parsed within it. There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort. The complex syntax can be recognised by the curly braces surrounding the expression. Simple syntax If a dollar sign (
"He drank some $juice juice.".PHP_EOL; The above example will output: He drank some apple juice. He drank some juice made of . He drank some juice made of apples. Similarly, an array index or an object property can be parsed. With array indices, the closing square bracket ( Example #15 Simple syntax example
"He drank some $juices[0] juice.".PHP_EOL; The above example will output: He drank some apple juice. He drank some orange juice. He drank some purple juice. John Smith drank some apple juice. John Smith then said hello to Jane Smith. John Smith's wife greeted Robert Paulsen. Robert Paulsen greeted the two . As of PHP 7.1.0 also negative numeric indices are supported. Example #16 Negative numeric indices
The above example will output: The character at index -2 is n. Changing the character at index -3 to o gives strong. For anything more complex, you should use the complex syntax. Complex (curly) syntaxThis isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. Any scalar variable, array element or object property with a string representation can be included via this syntax. The expression is written the same way as it would appear outside the string, and then wrapped in
"This works too: {$obj->values[3]->name}"; It is also possible to access class properties using variables within strings using this syntax.
The above example will output:
beers { String access and modification by characterCharacters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. The functions substr() and substr_replace() can be used when you want to extract or replace more than 1 character.
Warning Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits Warning Internally, PHP strings are byte arrays. As a result, accessing or modifying a string using array brackets is not multi-byte safe, and should only be done with strings that are in a single-byte encoding such as ISO-8859-1.
Example #17 Some string examples
String offsets have to either be integers or integer-like strings, otherwise a warning will be thrown. Example #18 Example of Illegal String Offsets
The above example will output: string(1) "b" bool(true) Warning: Illegal string offset '1.0' in /tmp/t.php on line 7 string(1) "b" bool(false) Warning: Illegal string offset 'x' in /tmp/t.php on line 9 string(1) "a" bool(false) string(1) "b" bool(false)
Converting to string A value can be converted to a string using the A bool An int or float is converted to a string
representing the number textually (including the exponent part for floats). Floating point numbers can be converted using exponential notation (
Arrays are always converted to the string In order to convert objects to string, the magic method __toString must be used. Resources are always converted to strings with the structure As stated above, directly converting an array, object, or resource to a string does not provide any useful information about the value beyond its type. See the functions print_r() and var_dump() for more effective means of inspecting the contents of these types. Most PHP values can also be converted to strings for permanent storage. This method is called serialization, and is performed by the serialize() function. Details of the String Type The string in PHP is implemented as an array of bytes and an integer indicating the length of the
buffer. It has no information about how those bytes translate to characters, leaving that task to the programmer. There are no limitations on the values the string can be composed of; in particular, bytes with value This nature of the string type explains why there is no separate “byte” type in PHP – strings take this role. Functions that return no textual data – for instance, arbitrary data read from a network socket – will still return strings. Given that PHP does not dictate a specific encoding for strings, one might wonder how string literals are encoded. For instance, is the string Of course, in order to be useful, functions that operate on text may have to make some assumptions about how the string is encoded. Unfortunately, there is much variation on this matter throughout PHP’s functions:
Ultimately, this means writing correct programs using Unicode depends on carefully avoiding functions that will not work and that most likely will corrupt the data and using instead the functions that do behave correctly, generally from the intl and mbstring extensions. However, using functions that can handle Unicode encodings is just the beginning. No matter the functions the language provides, it is essential to know the Unicode specification. For instance, a program that assumes there is only uppercase and lowercase is making a wrong assumption. John ¶ 5 years ago
gtisza at gmail dot com ¶ 10 years ago
garbage at iglou dot eu ¶ 6 years ago
Ray.Paseur sometimes uses Gmail ¶ 3 years ago
vseokdog at gmail dot com ¶ 3 years ago
og at gams dot at ¶ 15 years ago
sideshowAnthony at googlemail dot com ¶ 6 years ago
lelon at lelon dot net ¶ 17 years ago
steve at mrclay dot org ¶ 14 years ago
necrodust44 at gmail dot com ¶ 8 years ago
atnak at chejz dot com ¶ 18 years ago
php at richardneill dot org ¶ 9 years ago
chAlx at findme dot if dot u dot need ¶ 14 years ago
nospam at nospam dot com ¶ 6 years ago
?>
headden at karelia dot ru ¶ 13 years ago
c($a, $b) { return $a+$b; }// Usage Richard Neill ¶ 15 years ago
jonijnm at example dot com ¶ 5 years ago
sayHelloStatic() { greenbluemoonlight at gmail dot com ¶ 2 years ago
$smith = "Smith"; shd at earthling dot net ¶ 12 years ago
bishop ¶ 16 years ago
_EOC; mark at manngo dot net ¶ 5 years ago
"This is a $fruit['a']"; // T_ENCAPSED_AND_WHITESPACE Hayley Watson ¶ 4 years ago
Can you use double quotes in PHP?Yes. It is slightly faster to use single quotes. PHP won't use additional processing to interpret what is inside the single quote. when you use double quotes PHP has to parse to check if there are any variables within the string.
How do you put quotation marks in a string in PHP?Single quoted ¶
The simplest way to specify a string is to enclose it in single quotes (the character ' ). To specify a literal single quote, escape it with a backslash ( \ ). To specify a literal backslash, double it ( \\ ).
What is the difference in strings written in single and double quotes PHP?Double-quoted strings: By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences.
What is interpolation PHP?Variable interpolation is adding variables in between when specifying a string literal. PHP will parse the interpolated variables and replace the variable with its value while processing the string literal.
|