Home >Backend Development >PHP Tutorial >PHP entry string, cookie, session
String
English string interception
<code><span>$str</span>=<span>'i love you'</span>;</code>
//Intercept the letters love
echo substr($str, 2, 4);//Why is the starting position 2? Because the substr function calculates the string position starting from 0, that is, the position of 0 is i, the position of 1 is a space, and the position of l It's 2. Take 4 characters starting from position 2, which is love
Chinese string interception
mb_substr();
Format string
<code><span>$str</span> = <span>'99.9'</span>;</code>
<code><span>$result</span> = <span>sprintf</span>(<span>'%01.2f'</span>, <span>$str</span>);</code>
echo $result;//The result shows 99.90
Merge strings
<code><span>$arr</span> = <span>array</span>(<span>'Hello'</span>, <span>'World!'</span>); <span>$result</span> = implode(<span>''</span>, <span>$arr</span>); print_r(<span>$result</span>);<span>//结果显示Hello World!</span></code>
Split strings
<code><span>`$str` = '</span>apple,banana'; <span>`$result` = explode('</span>,<span>', $str); print_r($result);//结果显示array('</span>apple',<span>'banana'</span>)</code>
String escape function addslashes()
Function description: Used to add escape characters to special characters and return a string
Return value: an escaped string
Example:
$str
= “what’s your name?”;
echo addslashes($str);//Output: what’s your name
cookie
<br>
session_start(); <br>
$_SESSION['test'] = time(); <br>
var_dump($_SESSION); <br>
<br>
session_start(); <br>
$_SESSION['name'] = 'jobs'; <br>
unset($_SESSION['name']); <br>
echo $_SESSION['name']; //Prompt name does not exist <br>
$_SESSION
immediately. Only when it is accessed next time, $_SESSION
will be empty, so if you need to destroy $_SESSION immediately, you can use the unset function. The above has introduced PHP entry strings, cookies, and sessions, including special characters and global variables. I hope it will be helpful to friends who are interested in PHP tutorials.