: This article mainly introduces php entry strings, cookies, and Sessions. For more information about PHP tutorials, see.
String
- Obtain the string length: strlen () function
Obtain the text length echo mb_strlen ($ str, "UTF8 ");
English string truncation
$str='i love you';
// Intercept the letters love.
Echo substr ($ str, 2, 4); // why is the start position 2? because the position of the string calculated by the substr function starts from 0, that is, the position 0 is I, the position of 1 is a space, and the position of l is 2. Start from position 2 and take 4 characters, that is, love.
Chinese string truncation
Mb_substr ();
- String search
Strpos (the string to be processed, the string to be located, and the starting position to be located [optional])
- Replace string
Str_replace (string to be searched, string to be replaced, string to be searched, replacement for counting [optional])
Format a string
$str = '99.9';
$result = sprintf('%01.2f', $str);
Echo $ result; // The result is 99.90.
Merge strings
$ Arr = array ('hello', 'World! '); $ Result = implode ('', $ arr); print_r ($ result); // The result shows Hello World!
Split string
'$ Str' = 'Apple, banana'; '$ result' = explode (', ', $ str); print_r ($ result ); // The result shows array ('apple', 'banana ')
String escape function addslashes ()
Function description: usedSpecial charactersReturns a string with an escape character.
Returned value: an escaped string.
Example:
$str
= "What's your name ?";
Echo addslashes ($ str); // output: what \'s your name
Cookie
- Common parameters
Name (Cookie name) can be accessed through $ _ COOKIE ['name ']
Value (Cookie value)
Expire (Expiration Time) Unix timestamp format. the default value is 0, indicating that the browser is disabled or becomes invalid.
Path (valid path) if the path is set to '/', the entire website is valid.
Domain (valid domain) is valid for the entire domain name by default. if 'www .imooc.com 'is set, it is valid only in the www subdomain.
2.
There is also a function setrawcookie in PHP to set cookies. setrawcookie is basically the same as setcookie. The only difference is that the value does not automatically perform urlencode. Therefore, you need to manually perform urlencode when necessary.
- Delete and set expiration time
Setcookie ('test', ", time ()-1 );
- Valid Path
Setcookie ('test', time (), 0, '/path'); // you can specify the path and its subdirectories.
- Session
- It is very easy to use session in PHP. first execute the session_start method to start the session, and then useGlobal variables$ _ SESSION reads and writes sessions.
session_start();
$_SESSION['test'] = time();
var_dump($_SESSION);
- The session automatically performs encode and decode on the value to be set. Therefore, the session can support any data type, including data and objects.
- Delete
You can use the PHP unset function to delete a session value.Global variables$ _ Removed from the SESSION and cannot be accessed
Session_start ();
$ _ SESSION ['name'] = 'job ';
Unset ($ _ SESSION ['name']);
Echo $ _ SESSION ['name']; // the system prompts that the 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 deletes all data, but session_id still exists.
Session_destroy will not be destroyed immediatelyGlobal variables$_SESSION
Value in, only when the next visit,$_SESSION
Therefore, if you need to destroy $ _ SESSION immediately, you can use the unset function.
If you need to destroy session_id in the cookie at the same time, it is usually used when the user exits, you also need to explicitly call the setcookie method to delete the cookie value of session_id
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above introduces the php entry string, cookie, session, including special characters, global variables, and hope to help those who are interested in the PHP Tutorial.