Php entry (string, cookie, session)

Source: Internet
Author: User
Php entry (string, cookie, session)

Php entry (string, cookie, session). If you need it, refer.


String

Obtain the string length: strlen () function
Obtain the text length echo mb_strlen ($ str, "UTF8 ");

English string truncation

  1. $ 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, the starting position of the positioning [optional]) to replace the string
Str_replace (string to be searched, string to be replaced, string to be searched, replacement for counting [optional])

Format a string

  1. $ Str = '99. 9 ';

  1. $ Result = sprintf ('% 01.2f', $ str );

Echo $ result; // The result is 99.90.

Merge strings

  1. $ Arr = array ('hello', 'World! ');
  2. $ Result = implode ('', $ arr );
  3. Print_r ($ result); // The result shows Hello World!

Split string

  1. '$ Str' = 'Apple, banana ';
  2. '$ Result' = explode (', ', $ str );
  3. Print_r ($ result); // The result shows array ('apple', 'banana ')

String escape function addslashes ()
Function description: used to add escape characters to special characters and return a string.
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 delete urlencode and set the expiration time when necessary.
Setcookie ('test', ", time ()-1); valid path
Setcookie ('test', time (), 0, '/path'); // you can specify a valid session for the path and its subdirectories.
It is very easy to use session in PHP. first, execute the session_start method to enable the session, and then use the global variable $ _ SESSION to read and write the session.

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. after deletion, it will be removed from the global variable $ _ 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 does not immediately destroy the value in the global variable $ _ SESSION. it is null only when the next access is made. 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

Php, cookie, session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.