Functions and Arrays of PHP

Source: Internet
Author: User

PHP functions
1. Simple functions
Four elements: return type, function name, argument list, function body

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

Show (); */

2. Functions with return values
/*function Show ()
{
return "Hello";
}

Echo Show (); */

3. Function with parameters

/*function Show ($a)
{
echo $a;
}

Show ("Hello"); */

4. Functions of variable parameters
/*function Sum ()
{
$attr = Func_get_args ();
$n = Func_num_args ();

$sum = 0;
for ($i =0; $i < $n; $i + +)
{
$sum + = $attr [$i];
}
Echo $sum;
}
Sum (1,2,3,4); */

Array
$attr = Array (for each);
$attr 1 = [1,2,3,4,5];
$attr 2[0] = "Hello";
$attr 2[1] = "World";
Var_dump ($attr 2);

1. Indexed arrays
$attr = Array (+/-);
Var_dump ($ATTR);

2. Associative arrays
$attr 1 = Array ("One" =>1, "one" =>2, "3" =>3);
Var_dump ($attr 1);
echo $attr 1[3];

Characteristics:
1. The array can store any type of data
2. Arrays do not create a contiguous area of storage in memory

Iterating through an array
1.for loop traversal, can only traverse indexed array

/*for ($i =0; $i <count ($attr); $i + +)
{
echo $attr [$i]. " <br> ";
}*/

2.foreach traversal, index associations can traverse
/*foreach ($attr as $v)
{
echo $v. " <br> ";
}*/

/*foreach ($attr 1 as $k = $v)
{
echo "{$k}--{$v}<br>";
}
*/

3.each () and list () are combined to iterate through the array//var_dump ($attr 1); Takes the element that the current pointer points to in the array
Var_dump (each ($attr 1));
Var_dump (each ($attr 1));

List ($a, $b, $c, $d) = $attr; Assigns the elements inside the right array to the variables in the argument list


/*while (List ($k, $v) = each ($attr 1))
{
echo "{$k}--{$v}<br>";
}*/

4. Using pointers to iterate through an array
Echo current ($attr 1); Takes the value of the current element pointed to by the pointer
Echo Key ($attr 1); Takes the key of the current element pointed to by the pointer
Next ($attr 1); The pointer is lowered by one
Next ($attr 1);
Prev ($attr 1); Adjust the pointer up one
Echo Key ($attr 1);
End ($attr 1); Move the pointer to the last element
Reset ($attr 1); Reset the pointer

/*for ($i =0; $i <count ($attr 1); $i + +)
{
Echo Key ($attr 1);
Next ($attr 1);
}*/

/*do
{
Echo Key ($attr 1);
}
while (Next ($attr 1)) */
$attr = Array (+/-);
Var_dump ($ATTR);

Functions and Arrays of PHP

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.