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.
Syntax
A string literal can be specified in four different ways:
- single quoted
- double quoted
- heredoc syntax
- nowdoc syntax
But we will focus on single quotes and double quotes.
- 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 (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. |
ex –
$a = ‘car’; $amnt = ‘twenty dolar’; $name= ‘microcodes.in’; |
- Double quoted
If the string is enclosed in double-quotes (“), PHP will interpret the following escape sequences for special characters:
Ex –
$company = “Dell”; $name = “shimanta”; $website = “microcodes.in”; |

Source: https://www.php.net/manual/en/language.types.string.php
Let’s see how to create strings and display into screen.
Note: using gettype() function we can knows the current data-type of any variable.
File – code1.php
<?php $a=’microcodes’; echo ‘a is: ‘.$a; echo ‘<br>variable type: ‘; echo gettype($a); ?> |

There are many operations on strings we can do with strings…
Typecasting
PHP does not require explicit type definition in variable declaration. In this case, the type of a variable is determined by the value it stores. That is to say, if a string is assigned to variable $var, then $var is of type string. If afterwards an int value is assigned to $var, it will be of type int.
PHP may attempt to convert the type of a value to another automatically in certain contexts. The different contexts which exist are:
Numeric, String, Logical, Integral and string, Comparative, Function
The way by which PHP can assign a particular data type for any variable is called typecasting. The required type of variable is mentioned in the parenthesis before the variable. PHP does not support data type for variable declaration.
Let’s see some examples…
- int -> string typecasting
File – code2.php
<?php $val=200; echo “val is: $val”; echo “<br>type is: “.gettype($val); $val=(string)$val; echo “<br>val is: $val”; echo “<br>type is: “.gettype($val); ?> |

- double -> string typecasting
File – code3.php
<?php $val=45.6; echo “val is: $val”; echo “<br>type is: “.gettype($val); $val=(string)$val; echo “<br>val is: $val”; echo “<br>type is: “.gettype($val); ?> |

- double -> int -> string tyecasting
file – code4.php
<?php $val=45.6; echo “val is: $val”; echo “<br>type is: “.gettype($val); // float -> int typecasting $val=(int)$val; echo “<br>val is: $val”; echo “<br>type is: “.gettype($val); // int -> string typecasting $val=(string)$val; echo “<br>val is: $val”; echo “<br>type is: “.gettype($val); ?> |

Topic:
- Getting strings via HTML form – we consider 2 files here – form.html & send.php
File – form.html
<!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″> <meta http-equiv=”X-UA-Compatible” content=”IE=edge”> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>basic string</title> </head> <body> <form action=”send.php” method=”post”> <label for=”name”>Enter String</label> <input type=”text” name=”name” id=”name”> <br> <input type=”submit” value=”send”> </form> </body> </html> |

file – send.php
<?php error_reporting(0); $str = $_POST[‘name’]; echo ‘strings is: ‘.$str; ?> |
