PHP entry String, cookie,session

Source: Internet
Author: User
Tags urlencode
  1. String

    1. Gets the length of the string: strlen () function
      Get Chinese text long echo mb_strlen ($str, "UTF8");
    2. English string interception

      $str='i love you';

      Capture the letters of love
      Echo substr ($str, 2, 4);//Why the starting position is 2, because the SUBSTR function calculates that the string position is starting from 0, that is, the position of 0 is the space where the i,1 is, and the position of L is 2. Starting from position 2, take 4 characters, that is love
      Chinese string interception
      Mb_substr ();

    3. String Lookup
      Strpos (string to be processed, string to position, starting position of position [optional])
    4. Replace string
      Str_replace (The string to find, the string to replace, the string to be searched, and the replacement to count [optional])
    5. formatting strings

      $str'99.9';
      $resultsprintf('%01.2f'$str);

      echo $result;//Results show 99.90

    6. Merging strings

      $arrarray('Hello''World!');$result = implode(''$arr);print_r($result);//结果显示Hello World!
    7. Split string

      `$str` = 'apple,banana';`$result` = explode(',', $str);print_r($result);//结果显示array('apple','banana')
    8. String escape function Addslashes ()
      Function Description: Used to add an escape character to a special character , returns a string
      Return value: An escaped string
      Example:

      $str= "What ' s your name?";
      echo addslashes ($STR);//output: what\ ' s your name

  2. Cookie

    1. Common parameters
      Name (cookie name) can be accessed via $_cookie[' name ']
      value (value of cookie)
      Expire (Expiration time) UNIX timestamp format, default is 0, indicating that the browser is off or dead
      Path (valid path) if the path is set to '/', the entire site is valid
      domain (valid domain) the default entire domain name is valid, if ' www ' is set. Imooc.com ', the
      2 is valid only in the WWW subdomain.
      There is also a function in PHP that sets a cookie Setrawcookie,setrawcookie is basically the same as Setcookie, the only difference is that value values are not automatically urlencode. So when needed, manually urlencode
    2. Delete and set expiration
      Setcookie (' Test ', ', Time ()-1);
    3. Valid path
      Setcookie (' Test ', Time (), 0, '/path '),//Set the path and its subdirectories are valid
  3. Session
    1. Using the session in PHP is very simple, first executing the Session_Start method to open the session, and then through the global variable $_session session read and write.

      session_start();
      $_SESSION['test'] = time();
      var_dump($_SESSION);
    2. The session automatically encode and decode the values to be set, so the session can support any data type, including data and objects.
    3. Delete
      Deleting a session value can use PHP's unset function, which is removed from the global variable $_session and cannot be accessed

      session_start();
      $_SESSION['name'] = 'jobs';
      unset($_SESSION['name']);
      echo $_SESSION['name']; //提示name不存在

      If you want to delete all sessions, you can use the Session_destroy function to destroy the current Session,session_destroy delete all data, but session_id still exists
      Session_destroy does not immediately destroy the values in global variables , it $_SESSION is only empty the next time they are accessed, $_SESSION so if you need to destroy $_session immediately, you can use the unset function.
      If it is necessary to destroy the session_id in the cookie at the same time, usually when the user exits, you also need to explicitly call the Setcookie method to delete the session_id cookie value.

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP entry string, cookie,session, including special characters, global variables, and I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.