Home >Backend Development >PHP Tutorial >Custom functions and arrays developed in php

Custom functions and arrays developed in php

伊谢尔伦
伊谢尔伦Original
2016-12-01 11:18:461065browse

This article will introduce some knowledge related to custom functions and arrays in PHP development. PHP developers can pay attention to it.

1. Custom function

A custom function is a function that we define ourselves. The format of a custom function in PHP is as follows:


function funname(arg1, arg2, arg3......){
 //TODO
 return values;
}
   
<?php
function fun($m, $n){
if($m==0 || $n==0){
return 0;
}else{
$a=$m*$n;
return $a;
}
}
 
$p=2;
$h=3;
echo $p."*".$h."=".fun($p,$h);
?>

Output result: 2*3=6

Here is another variable Parameter function

<?php
/*
function fun($m, $n){
if($m==0 || $n==0){
return 0;
}else{
$a=$m*$n;
return $a;
}
}
 
$p=2;
$h=3;
echo $p."*".$h."=".fun($p,$h); */
 
function fun($m, $n=1, $x=2){
$a=$m*$n*$x;
return $a;
}
 
$p=2;
echo fun($p)."<br>";          // 2*1*2 = 4
echo fun($p, 3)."<br>";       // 2*3*2 = 12
echo fun($p, 3, 3)."<br>";    // 2*3*3 = 18
?>


Output result:

4

12

18

Let’s take a look at custom function reference passing

<?php 
    /*  
    function fun($m, $n){
        if($m==0 || $n==0){
            return 0;
        }else{
            $a=$m*$n;
            return $a;
        }
    }
 
    $p=2;
    $h=3;
    echo $p."*".$h."=".fun($p,$h); 
    */ 
 
    /*
    function fun($m, $n=1, $x=2){
        $a=$m*$n*$x;
        return $a;
    }
 
    $p=2;
    echo fun($p)."<br>";          // 2*1*2 = 4
    echo fun($p, 3)."<br>";       // 2*3*2 = 12
    echo fun($p, 3, 3)."<br>";    // 2*3*3 = 18
    */ 
 
    function fun(&$n){ 
        $n=$n*$n; 
    } 
    $p=2; 
    fun($p); 
    echo $p; 
?>

Output result: 4

2. Array definition assignment

1. Array Basic writing format

Simple form: array(value 1, value 2, value 3, …….)

array(“aa”, 12, true, 2.2, “test”, 50); //By array subscript Get data

Full form: array(key 1=>value 1, key 2=>value 2, …)

array(“title”=>”aa”, “age”=>20) ; //Data can only be obtained through key names

2. Ways to create arrays


//First method

$arr1=array(11, 22, 33, "44");

// The second type

$arr2=array('a'=>'11', 'b'=>'22');

//The third type

$arr3[0]='20';

$arr3[1]='30';

3. Array operations

1. Modify

$arr=array(11, 22, 33, 44);
 
$arr[0]=55;  //数组变为$arr=array(55, 22, 33, 44);

2. Delete

$arr=array(11, 22, 33, 44);
 
unset($arr[0]);    //数组变为$arr=array(22, 33, 44);

3. Use

$arr=array(11, 22, 33, 44);
 
echo $arr[0];
 
$arr=array(&#39;a&#39;=>11, &#39;b&#39;=>22, &#39;c&#39;=>33, &#39;d&#39;=>44);
 
echo $arr[&#39;b&#39;]];

4. Traverse

$arr=array(&#39;a&#39;=>11, &#39;b&#39;=>22, &#39;c&#39;=>33, &#39;d&#39;=>44);
 
foreach($arr as $value){ //无键名
 
echo $value."<br>";
 
}
 
foreach($arr as $id=>$value){  //输出键和值
 
echo $id."__".$value."<br>";
 
}

4. Two-dimensional array

$arr=array(array("1","11","111"), array("2","22","222"));
 
echo $arr[1][2];

5. Array function

(1) array_change_key_case(array, case)

array: required, array.

case: Optional, CASE_LOWER (default value, lowercase letters return the keys of the array), CASE_UPPER (uppercase letters return the keys of the array)

Function: Convert all KEYs of the array to uppercase or lowercase.

10c76b3704db3682efbd84368e2e9c49"Cat","b"=>"Dog","c"=>"Horse");

3.

print_r(array_change_key_case($a,CASE_UPPER));

4.

?>

5.

Result: Array ( [A] => Cat [B] => Dog [C] => Horse )

(2) array_chunk(array, size, preserve_key)

array: required.

size: required, specifies how many elements each new array contains.

preserve_key: optional, true (preserve key name), false (new index)

Function: Divide an array into new array blocks.

<?php 
    //array_chunk(array,size,preserve_key) 
 
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow"); 
    print_r(array_chunk($a1,2)); 
 
    $a2=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow"); 
    print_r(array_chunk($a2,2,true)); 
 
?>

Result:

Array ( [0] => Array ( [0] => Cat [1] => Dog ) [1] => Array ( [0] => Horse [1 ] => Cow ) )

Array ( [0] => Array ( [a] => Cat [b] => Dog ) [1] => Array ( [c] => Horse [d] => Cow ) )

…….

There are many functions like this, you can check them when you use them.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn