0505 php-arrays, control statements, functions

array

(definition, use, assignment, traversal, classification, bubble sort)

1. Array includes elements, subscripts, array length

2. The length of the array in php is $len = count("$array name");

3. Define an array: $arr = array("a"=>"aa", "b"=>"bb" );

4. Classification of arrays:

  According to the key-value relationship, it can be divided into index array and associative array. The index array can be traversed by for and foreach, and the associative array can only be traversed by foreach.

  According to the array level, it can be divided into one-dimensional array, two-dimensional array and multi-dimensional array.

5.foreach traversal

  foreach($array name as $k =>$v){

    execute statement;

  }

6. Bubble sort:

  for($i = 0; $i < $len-1; $i++){

    for($k = 0; $k < $len-1-$i; $k++){

      if($cj[$k] < $cj[$k+1]){
                   $temp = $cj[$k];
                   $cj[$k] = $cj[$k+1];
                     $cj[$k+1] = $temp;

       }
            }

  }

control statement

What?

if、for、switch、while、do while

Keywords related to loops: break (abort), continue (skip)

pay attention:

  die ("output content") meaning: terminate the running of the php script (subsequent code is no longer executed), and output the content

  sleep($n);    sleep(3)

  //3 seconds meaning: stop the php script for $n seconds, and then continue execution.

Alternative syntax for partial flow control:

  if ( ... ) :
    //statement block
  endif;

 

  while(...):
    //statement block
  endwhile;

function

1. Definition of the function:

  function function name(parameter 1, parameter 2, .... ){
    //function body (code block)
  }

2. Formal parameters:

1. The formal parameter must be a variable name!
2. The variable name can only be a variable name that is valid in the function;
3. It is only valid when the function is called and executed, and the function ends, usually these variables are "destroyed".

  Arguments can have default values:
  default values ​​can only be constant expressions, or constants, and formal parameters with default values ​​are placed on the far right.

3. Actual parameters:

The actual parameter can be a "direct data" (such as 5, "abc"), or it can be data stored in a variable.
The role of an actual parameter is to "assign" its data to a formal parameter variable.
There is usually a "one-to-one correspondence" between actual parameters and formal parameters

4. Function parameter passing by value

The actual parameter is direct data: there is no value passing problem, it is a direct assignment

The actual parameter is a variable: the default value is passed, and & can be changed to pass by reference

* The number of actual parameters should be at least not less than the number of non-default value parameters in the formal parameters.

5. Number of free parameters and related functions

Number of free parameters: When defining, you can give no formal parameters, but when calling, you can give any number of actual parameters.

func_get_args(); //Get all the actual parameter data received by a function, and the result is an array

func_get_arg(n); //Get the actual parameter data of the nth song received by a function (n starts from 0)

func_num_args(); //Get the number of all actual parameter data received by a function

6. Return value: return

end function. Returns a data by value (direct data, variable data, expression result data).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325322440&siteId=291194637