PHP functions, arrays

Source: Internet
Author: User

PHP functions

The real power of PHP comes from its functions
Functions are executed by calling functions and can be called anywhere on the page

PHP Function Guidelines:
Function name begins with a letter or underscore (cannot start with a number)
The name of the function should indicate its function

Four elements:
return type, function name, argument list, function body
(Weak types do not require return type)
Grammar:
1. Basic functions:
function functionname ()
{
echo "Hello"
}
FunctionName ();
2. function with return value to add return
3. parameter function definition to give a formal parameter, output with an actual parameter
4. Functions of variable parameters
function sum ()
{
$attr = Func_get_args (); (get: Get; args: Parameter)
$n = Func_get_args;

$sum = 0
(For Loop)
}

Array:
Arrays can store multiple values in a single variable
You can store any type of data in an array
The array is contiguous and the length is fixed
1. $attr = Array (for each);
2. $attr 1 = [1,2,3,4,];
3. (assigned value) $attr 3[0] = "Nihao"
$attr 3[1] = "HI"
Gets the length of the array: (count)
echo count ();

Array type:
Indexed array
$attr = Array (+/-);
Var_dump ($ATTR);

Associative arrays
$attr 1 = Array ("One" =>1, "=>2")

Iterating through an array
For loop traversal cannot traverse associative array
foreach loops (indexes, associations can traverse)
foreach (array as variable)
{
The echo variable. <br> ";
}

each ()
List ($1,$2,$3) = $attr assigns the elements inside the right array to the variables on the left
while (list (variable) = each (array))
{
echo "{variable} {variable}<br>"
}

Using pointers:
Current ()
Get the current pointer position content data
Key ()
Gets the index value of the current pointer position
Prev ()
Returns the internal pointer of the array back to a
Next ()
Moves the inner pointer in the array forward one
End ()
Point the inner pointer of an array to the last cell
Reset ()
Point the inner pointer of an array to the first cell

PHP functions, 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.