Home >Backend Development >PHP Tutorial >Getting started with php (string, cookie, session)

Getting started with php (string, cookie, session)

WBOY
WBOYOriginal
2016-07-25 08:46:24963browse

Getting started with PHP (strings, cookies, sessions), friends in need can refer to it.


String

Get the length of a string: strlen() function
Get the Chinese character length echo mb_strlen($str,”UTF8”);

English string interception

  1. $str='i love you';
Copy 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();

String search
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

  1. $str = '99.9';
Copy code
  1. $result = sprintf('%01.2f', $str);
Copy code

echo $result;//The result shows 99.90

Merge strings

  1. $arr = array('Hello', 'World!');
  2. $result = implode('', $arr);
  3. print_r($result);//The result shows Hello World!
Copy Code

split string

  1. `$str` = 'apple,banana';
  2. `$result` = explode(',', $str);
  3. print_r($result);//The results display array('apple','banana ')
Copy 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

Common parameters
name (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
php, cookie, session


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn