PHP "Functions"

Source: Internet
Author: User

Directories: "PHP functions", "PHP arrays"

PHP common functions (same as JS)
Four elements of a function:
① return type
Name of ② function
③ parameter types
④ function Body
Because PHP is a weakly typed language, it is not possible to write the return type, but the other three features must be written.


1. The simplest function notation

" Hello " ;} Show ();

2. function with return value
is to add a return to the function body.
return: Returns.

function Show () {return"hello";} Echo Show ();

3. Function with parameters
Requires a parameter, because it is a weakly typed language and does not need to be given a data type, directly written parameters can be.

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

So you need to give an argument when the output is called .


4. Functions with variable parameters (in PHP)
That is, when the function is called, the parameters can be changed. Can not give parameters, can give a pair of three can be. In other languages, this mutable parameter requires the use of object-oriented knowledge, which is not required in PHP.
Examples of how mutable parameters are defined:
A pass-through parameter makes an additive function, passing over several parameters to make a summation sum, and then outputting the final result.
Func_get_args: In the function body, where to get the parameters given by the user.
Func: The abbreviation of the function.
Get: Get.
Args: Parameters.
If this method is called, an array is recalled, which is the parameter given by all users.

= Func_get_args (); Var_dump ($attr);} SUM ()

① if sum has no arguments, the result is:
(There is no element inside the array, it is empty .) )

Array: Arrays
If the value passed in sum is the result of:
SUM (1,2,3,4);

The index is 0,1,2,3. The number becomes four. So this method can dynamically get the number of user-transmitted parameters, and then put the whole into an array inside. Now that we can get the user's parameters, then we can do the following:

You can use the For loop to iterate through each parameter to make an accumulation:

= Func_get_args ();  for ($i =0; $i <count ($attr))}sum (1,2,3,4)

PS: How to get the array length
①

= Func_get_args ();  for ($i =0; $i <count ($attr))}sum ()

② getting the number of parameters
Func_num_args (); then hand over to a variable.

==0;  for ($i =0; $i < $n; $i ++ + + $attr [$i];} echo $sum;} Sum ();

This is a full version of the PHP variable parameter function, there is no specified parameter is how much, the user can be arbitrary when calling parameters.
+ =: itself plus the value behind.

Second, the PHP array
Arrays are structures that are professionally used to store data.
Features of the array:
① array can store any type of data
The ② array does not create a contiguous area of storage in memory.

1. How the array is defined
The ①php array is simply defined as follows:
$attr = Array ();
Var_dump ($ATTR);
Variable (the future is an array) = array (parameter)
②[] Way Definition
$attr = Array (for each);
$attr 1 = [1,2,3,4];
Var_dump ($attr 1);

PS: Because we use the DW, rather than the PHP professional development tools, so this time to write the code will show an error, but the output is not wrong, so can be ignored.

If you want to ask for the 4 of the assignment in this array?
$attr 1 = [1,2,3,4];
echo $attr 1[3];

③ Assignment Definition
$attr = Array (for each);
$attr 2[0] = "Hello";
$attr 2[1] = "World";
Var_dump ($attr 2);

$attr 2[0] = "Hello";: Assign the location of index 0 a hello.

2. Indexed array
$attr = Array (+/-);
Var_dump ($ATTR);
Because when we output or value, we can see the previous index number: 0, 1, 2 and so on.

3. Associative arrays
Compared to a collection in other languages, the previous index number position becomes a string, which is equivalent to a name for the value that follows.
$attr 1 = Array ("One" =>1, "=>2");
Var_dump ($attr 1);

The difference between associative arrays and indexed arrays is the difference between K. That is, the "name" in front of the value is different.
The index number is automatically queued, and the associative array is what we consider to be obtained.
Value: Echo $attr 1[one];


4. Iterating through an array
①for looping through an array (associative arrays cannot be used)

$attr = Array (1,2,3= Array ("One"+ =1, "  Both "=2for ($i =0; $i <count ($attr); $i + +) {echo $attr [$i]. " <br> " ;}

Dot.: Represents a concatenation of strings.

②foreach traversal
Indexes and associations are all conveniently available
(1)

 $attr = Array (1 , 2 , 3   = array ( " Span style= "COLOR: #800000" >one   " =>1 ,  " two   " =>2 , "  3   " =>3   foreach  ($attr 1 as   $v) {echo $v.   " <BR>   " }  

($attr 1 as $v): The first time execution is to throw the first element in the array to the variable $v, every time.

(2) value and K

$attr = Array (1,2,3); $attr 1= Array (" One"=1," Both"=2,"3"=3); foreach($attr 1 as$k =$v) {echo"{$k}--{$v}<br>";}

{$k}--{$v}<br>: Parses a variable in a string. After parsing the rest, only {$k} and {$v} are changed.
----------the effect of dividing, can also add = = also can not add.
③, each (), and List () to iterate through an array
Each method can return the K and value of an element in the $ATTR1 array.

$attr = Array (1,2,3= Array ("One"+ =1, "  Both "=2,"3"=3); Var_dump (each ($attr 1));

Var_dump (each ($attr 1)); Takes the element that the current pointer points to in the array.
Var_dump (each ($attr 1) [1]), and if you take index 1, you can fetch the value.
Var_dump (each ($attr 1) ["Key"]), or you can take the index value if you take the key.
Other words:
It returns the first element in the current array, $attr1 the index and value of the first element, and then creates a new array, in which the array is used for convenience, so there are both indexes and associations. In other words, the data inside the array is redundant and can be taken to the value.

④list () method Special
All other methods are written on the right side of the equals sign. List is the only one written on the right side of the equals sign.
(1) Assign the elements in the right array to the variables in the parameter list.

$attr = Array (1,2,3= Array ("One"+ =1, "  Both "=2,"3"=3); List ($a, $b, $c)=$attr; echo $a

Remove each element in the right attr to each parameter in the left argument list. It is used to assign a value to the variable, and the value assigned is the value of the element in the array.
So, if the output is $ A, it is 1.
(2) How to coordinate the traversal of an array.

 while (List ($k, $v) ="{$k}--{$v}<br>";}

Not used. Remember, you can.

⑤
(1) Use pointers to iterate through the array.

$attr = Array (1,2,3= Array ("One"+ =1, "  Both "=2,"3"=3); Echo current ($attr 1);

Takes the value of the current element pointed to by the pointer. Just take the value and don't move the pointer.
(2) Key method
Eco Key ($attr 1); Takes the key of the current element pointed to by the pointer. Just take the value and don't move the pointer.
(3) Next () adjusts the pointer.
(4) Adjust the pointer to one.

$attr = Array (1,2,3= Array ("One"+ =1, "  Both "=2,"3"=3); Next ($attr 1); next ($attr 1); Echo key ($attr) ;

(5) Adjust the pointer up one.

$attr = Array (1,2,3= Array ("One"+ =1, "  Both "=2,"3"=3); Next ($attr 1);p rev ($attr 1); Echo key ($attr);

Next: Next page.
Prev: Previous page.
(6) End moves the pointer to the last element.
(7) Reset: Resets the pointer.

PHP function

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.