Home >Backend Development >PHP Tutorial >Getting started with php (string, cookie, session)
Getting started with PHP (strings, cookies, sessions), friends in need can refer to it. String Get the length of a string: strlen() functionGet the Chinese character length echo mb_strlen($str,”UTF8”); English string interception
//Intercept the letters love strpos(string to be processed, string to be positioned, starting position of positioning [optional]) Replace string str_replace(string to find, string to replace, string to be searched, replacement to count [optional]) Format string
echo $result;//The result shows 99.90 Merge strings
split string
String escape function addslashes() $str = “what’s your name?”; cookie Common parametersname (Cookie name) can be accessed through $_COOKIE[‘name’] value (Cookie value) expire (expiration time) Unix timestamp format, the default is 0, which means it will expire when the browser is closed path (valid path) If the path is set to '/', the entire website is valid Domain (valid domain) defaults to the entire domain name being valid. If 'www.imooc.com' is set, it is only valid in the www subdomain 2. There is also a function setrawcookie in PHP that sets cookies. Setrawcookie is basically the same as setcookie. The only difference is that the value will not be automatically urlencoded, so it must be urlencoded manually when needed. Delete and set expiration time setcookie(‘test’, ”, time()-1); Valid path setcookie(‘test’, time(), 0, ‘/path’);//The path and the subdirectories under it are set to be valid session Using session in PHP is very simple. First execute the session_start method to open the session, and then read and write the session through the global variable $_SESSION. session_start(); $_SESSION['test'] = time(); var_dump($_SESSION); Session will automatically encode and decode the value to be set, so session can support any data type, including data and objects. Delete To delete a session value, you can use PHP's unset function. After deletion, it will be removed from the global variable $_SESSION and cannot be accessed session_start(); $_SESSION['name'] = 'jobs'; unset($_SESSION['name']); echo $_SESSION['name']; //Prompt name does not exist If you want to delete all sessions, you can use the session_destroy function to destroy the current session. session_destroy will delete all data, but the session_id still exists session_destroy will not immediately destroy the value in the global variable $_SESSION. Only when it is accessed next time, $_SESSION will be empty. Therefore, if you need to destroy $_SESSION immediately, you can use the unset function. If you need to destroy the session_id in the cookie at the same time, which may usually be used when the user logs out, you also need to explicitly call the setcookie method to delete the cookie value of the session_id |