PHP Course Note 5

Source: Internet
Author: User
Tags http post

Lesson 109 The syntax structure of the list () function

1.list on the left side of the equals sign, only one array can be behind the equals sign.

2.list: Convert elements in an array to variable use

3.list: There are several elements in the array, there are several parameters in the list, and the parameters must be variables. But with fewer parameters, the elements of the array will not be available.

4. You can only convert an indexed array to a variable and must be contiguous.

5. The element in the array can be selectively received by a null item in the list parameter.

characteristics of each () function in class

1.each () is just a function, the parameter is an array as an argument, and the returned value is an array.

2. The returned value is an array, the array is fixed with 4 elements, and the subscript is fixed. 1 (value) value (value) 0 (subscript) key (subscript)

3.ecah () handles only the current element, converting the current element to array information. When finished, the pointer moves down one element.

4. If the pointer is already at the end position, return FALSE if you use each () to get the element.

Lesson 111 iterating through arrays using list (), each (), and while loops 1.

$arr = Array ("SCE", "Zaj", "Vicky", "Coin");

while (list ($key, $value) = each ($arr)) {

echo "{$key} = {$value} <br>"; }

Reset ($arr); Reset pointer

echo "

$arr = Array ("SCE", "Zaj", "Vicky", "Coin");

while (list ($key, $value) = each ($arr))

{echo "{$key} = {$value} <br>";}

Session The array is traversed using the internal pointer control function of the array

1.next (): Pointer moves the next prev (): Pointer moves forward one end (): pointer moves to the last reset (): pointer moves to the beginning of key (): Gets the current key (): Gets the current value

Lessons 113 PHP Hyper-global arrays (predefined variables) Overview

Hyper-Global arrays: In PHP scripts, variables that have already been declared can be used directly, and the name of the variable is already set.

1.$_server Server Variables

2.$_ENV Environment variables

3.$_post HTTP POST variables

4.$_get HTTP GET variables

5.$_request REQUEST Variable

6.$_files http Upload file variable

7.$_cookie

8.$_session Session Variables

9. $GLOBALS

An array (associative array), which is the same operation as an array of its own declaration

Two. Global: Triple: Super: Each predefined array has unique capabilities

Lesson $_server Server variables and environment variables $_env

// 定义一个函数getIP() function getIP() { global $ip ; if ( getenv ( "HTTP_CLIENT_IP" )) $ip getenv ( "HTTP_CLIENT_IP" ); else if ( getenv ( "HTTP_X_FORWARDED_FOR" )) $ip getenv ( "HTTP_X_FORWARDED_FOR" ); else if ( getenv ( "REMOTE_ADDR" )) $ip getenv ( "REMOTE_ADDR" ); else $ip "Unknow" ; return $ip ; } // 使用方法: echo getIP();
 getenv ("REMOTE_ADDR") is used to obtain the IP address of the client, but if the client is accessed using a proxy server, it is the IP address of the proxy server, not the real client IP address. To obtain the real IP address of the client through the proxy server, it is necessary to use getenv ("Http_x_forwarded_for") to read. However, if the client is not accessed through a proxy server, the value taken with getenv ("Http_x_forwarded_for") will be empty. else if (getenv ("Http_x_forwarded_for")) $ip = getenv ("Http_x_forwarded_for"); Indicates that if getenv ("Http_x_forwarded_for") takes a value that is not empty (that is, if the client uses a proxy server), the variable $ip equals the real IP value taken by getenv ("Http_x_forwarded_for"). The following $ip = getenv ("http_x_forwarded_for") will not be performed if the else if (getenv ("Http_x_forwarded_for") above has a null value (that is, no proxy server is used); In this case, it has been confirmed that the client is not using a proxy server, thus passing the else if (getenv ("REMOTE_ADDR")) $ip = getenv ("REMOTE_ADDR"); These two lines of statements get the IP address of the client as well as the real IP address.

The usage differences between getenv () and $_server in PHP:
GETENV obtains the environment variable of the system, the format of the environment variable is name=value.
Syntax: String getenv (string varname);
Return value: String function type: PHP system function
Content Description
If the environment variable varname is correctly obtained, the value of the variable is returned. Failure returns false.
The following example can get the URL of the machine where the user's browser
<?php
$ip = getenv ("REMOTE_ADDR");
?>
$_server is the server Super global variable array with $_server[' REMOTE_ADDR ') can also obtain the IP address of the client.
The difference between the two is that getenv does not support the IIS ISAPI way of running PHP

2. function GetIP () {if (!empty ($_server["HTTP_CLIENT_IP"])) {return $_server["http_client_ip"];} else if (!empty ($_server["Http_x_forward_for")) {return $_server["http_x_forward_for"];} else if (!empty ($_server["REMOTE_ADDR")) {return $_server["REMOTE_ADDR"];} else{return "Noknow";}} echo GetIP ();

Lesson 1 Introduction to PHP Hyper-global Arrays

1.$_get receiving parameters that users pass to the server via a URL

2.$_post receiving parameters that users pass through the HTTP protocol to the server

Class hours PHP Hyper Global Array Introduction 2

Class 117 The classification of PHP common array functions

Lesson 118 action functions in PHP arrays related to keys and values 1

1.array_value () changes the array to an indexed array, returning all the values of the array

2.array_keys () changes the array to an indexed array, returning all the key names of the array

Lesson 119 functions related to key-value search in PHP arrays 2

1.in_array (value, array, [true]) determines whether the value is in the array. Case-sensitive, and the third parameter is added after the even type is equal.

2.array_search () searches the array for the given value if the key value is returned successfully

3.array_key_exists () finds whether an array exists by the given key name

4.isset () If the array is empty, it will return false. So not accurate enough.

Class 3 operation function for key and value in PHP array

1.ARRAY_FILP ()

Swaps the keys and values of an array to generate a new array. If the value is duplicated, then the following value overrides the preceding. The original array of this function can only be strings and integers.

2.array_reverse ()

Reverse array, reverse the original array order, and then generate a new array.

The second parameter, if true, is reversed, but the key value pair is still the same. (Must be an indexed array to see it)

Lesson 121 The function of counting the number and uniqueness of array elements in PHP arrays

1.count () If used to calculate a string, only the int 1 is output, so it cannot be used to evaluate a string. String with strlen ();

2.count (Array, [1]) can be recursively computed array, that is, to calculate the multidimensional array, as long as the second argument to pass a 1 on it, the default of 0 is not calculated.

3.array_count_values ()

Count the occurrences of all values in an array

4.array_unique ()

Duplicates the values in the array to be deleted, leaving only the first occurrence.

Lesson 122 PHP uses callback functions to handle arrays of functions Array_filter ()

1.array_filter ()

Filters the cells in the array with a callback function. By default, values that are considered false in the element are filtered out. After you add the second parameter, you can define how to filter by using a callback function.

lesson 123 PHP uses callback functions to handle arrays of functions Array_walk ()

1.array_walk ()

The user function is applied to each member of the array. The first parameter is arrays, the second argument is an action, and can be a callback function.

Lesson 124 PHP uses callback functions to handle arrays of functions Array_map ()

1.array_map (callback function, array)

The callback function is scoped to the given array.

Arrar_may (null, $arr, $BRR); Causes the array to become a two-dimensional array. Make $arr, $brr merge.

Bubble (bubble) sorting algorithm in PHP

<?php

$arr = Array (0,1,2,3,4,5,6,7,8);

function Maopao ($arr) {

$len = count ($arr)-1;

for ($i = 0; $i < $len; $i + +) {

for ($j = 0; $j < $len-$i; $j + +) {

if ($arr [$j] < $arr [$j +1]) {

$tmp = $arr [$j +1];

$arr [$j +1] = $arr [$j];

$arr [$j] = $tmp;

}

}

}

return $arr;

}

Lesson 126 The principle of fast ordering by PHP dichotomy method

PHP Course Note 5

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.