PHP strings are defined inside
<?PHP $name="John Smith"; $str="His name is $name"; //His name is John Smith $str2='His name is $name'; //His name is $name ?>
If string contains special characters such as ", \, $ etc, they should be escaped by a slash
<?PHP $name="John Smith"; echo "His name is \"$name\""; //His name is "John Smith" echo 'His nmae is \'$name\''; //His name is '$name' echo "His nmae is '$name'"; //His name is 'John Smith' ?>
PHP string can also be defined by <<< followed by a symbol, the symbol is self defined, here we use STR, and the string should be ended by the same symbol and a semicolon. Special characters inside the string do not need to be escaped. Variables inside the string will be substitute by their values, except associated array elements.
<?PHP $str=<<<STR His name is "John Smith"; STR; echo "$str"; //His name is "John Smith" ?>
PHP string can be used as an array of characters.
<?PHP $name="John Smith"; echo $name[0]; //J echo $name[2]; //h ?>
<?PHP $str='<table><tr><td>name<td>address</table>'; $str2=strip_tags($str); //nameaddress ?>
<?PHP $str="His name is \"John Smith\""; echo $str; //His name is "John Smith" echo addslashes($str); //His name is \"John Smith\" ?>
<?PHP $str="His name is \"John Smith\""; echo strlen($str); //24 ?>
<?PHP $str="His name is John Smith"; echo strtolower($str); //his name is john smith echo strtoupper($str); //HIS NAME IS JOHN SMITH echo ucfirst($str); //His name is John Smith echo ucwords($str); //His Name Is John Smith ?>
<?PHP $str=" this is php tutorial "; echo "|" . trim($str) . "|"; //|this is php tutorial| echo "|" . chop($str) . "|"; //| this is php tutorial| echo "|" . ltrim($str) . "|"; //|this is php tutorial | ?>
<?PHP $str='<table><tr><td>name<td>address</table>'; echo $str; //<table><tr><td>name<td>address</table> echo htmlspecialchars($str); //<table><tr><td>name<td>address</table> ?>
More string functions include str_replace, substr_replace, strcmp(), strpos(),
strrpos(), substr(), strrev(), strtr etc.