Functions and Arrays

Source: Internet
Author: User

First, the function
Function three elements: 1. Function name 2. parameter List 3. function body
1. Simple functions

function Show () {    echo "Hello";} Show ();

The result of the operation is to display hello on the page

2. Functions with return values

function Show () {   return "Hello";  return value}echo Show ();

The result of the run is the value returned in return value

3. Function with parameters

function Show ($a) {echo $a;} Show ("Hello");

The result of the operation is not a formal parameter, but an argument: Hello

4. Functions of variable parameters

function Sum () {    $attr =func_get_args ();   Gets the parameter Args:func_get_args () passed by the get user;    $n =func_num_args ();    Get quantity Func_num_args ();   Traverse parameter    $Sum =0;   for ($i =0; $i < $n; $i + +)    {       $Sum + = $attr [$i];    }   echo $Sum;} Sum (1);   Values can be changed so that the corresponding values are also changed

The result of the operation is the value in the value

Second, array
Note: 1. Can store any type of data;
2. Not a contiguous area of storage in memory (not contiguous in memory)

$attr =array ();   Defines an empty array $attr1=[1,2];   Defines the array $attr2[0]= "Hello"; Assignment definition array: 0 index Assignment "Hello"

A. Array type:
1. Indexed arrays

$attr = Array (1,2,3,4); Echo $attr [2];   The value that the output index number refers to

2. Associative arrays

$attr 1=array ("One" =>1, "one" =>2, "3" =>3);   K is the one string, value is 1 values,=> conforms to echo $attr 1["one"];  Take value

echo $attr 1[3];  No problem, because the type can be converted automatically, so there is no error

B. Iterating through an array
1.for loops: Only "indexed arrays" can be traversed
COUNT () array length


2.foreach traversal: Both associative arrays and indexed arrays can traverse

Associative arrays:
$attr 1=array ("One" =>1, "one" =>2, "3" =>3); foreach ($attr 1 as $a) {echo $a. <br> ";}

Indexed arrays:
$attr = Array (1,2,3,4), foreach ($attr as $a) {echo $a. <br> ";}

$attr 1=array ("One" =>1, "one" =>2, "3" =>3); foreach ($attr 1 as $k = + $a) {echo $a = "{$k}--{$a}<br>";}

The 3.each () and list () mates iterate through the elements pointed to by the current pointer in the array

$attr 1=array ("One" =>1, "one" =>2, "3" =>3); Var_dump (each ($attr 1));    The first element "one" and 1var_dump (each ($attr 1));   The second element "two" and the 2var_dump (each ($attr 1));   The third element, "3" and 3
$attr = Array (1,2,3,4); list ($a, $b, $c) = $attr;   The assignment is the value in the attr array. The list method is the echo $a to the left of the equals sign;

4. Use the pointer method

The pointer position does not change
Current (); Takes the value of the current element pointed to by the pointer
Key (); Takes the key value of the current element pointed to by the pointer


Variable pointer position
Next (); Executed once, to the downward one

Prev (); Executed once, to raise a

End (); The pointer is transferred directly to the last

Reset (); Pointer reset

Functions and Arrays

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.