PHP Arrays and Functions

  1. Arrays
    1. PHP arrays are an ordered map (a type that that associates keys and values)
    2. Arrays can have any size and contain any type of value; no danger of going beyond array bounds
      $my_array[0] = 25;
      $my_array[1] = "Bisons";
      
    3. Arrays that use non-integer indexes/keys are often called associative arrays
      $capitals["CO"] = "Denver";     
      $capitals["AR"] = "Little Rock";
      
    4. Initialize an array
      $colors = array("red", "green", "blue");
      echo $colors[0];      // red
      echo count($colors);  // 3
      
      $capitals = array("CO" => "Denver", "AR" => "Little Rock");
      echo $capitals["CO"];   // Denver
      echo count($capitals);  // 2
      
    5. Print contents of an array for debugging
      print_r($colors); 
      
      produces:
      Array
      (
          [0] => red
          [1] => green
          [2] => blue
      )
      
      print_r($capitals); 
      
      produces:
      Array
      (
          [CO] => Denver
          [AR] => Little Rock
      )
      
      
    6. Looping through an array
      // Traditional array
      foreach ($colors as $c) 
      	echo "$c<br>";
      
      // Associative array
      foreach ($capitals as $state => $city) 
      	echo("$city is the capital of $state.<br>";
      
    7. Sorting an array
      sort($colors);    // ("blue", "green", "red)
      rsort($colors);   // ("red", "green", "blue")
      
    8. Summary of array functions
  2. Functions
    1. Function reference
    2. May be declared anywhere
    3. Function names are case-insensitive though it is usually good form to call functions as they appear in their declaration
    4. Parameters may be passed by value or by reference
      // Pass by value
      function sum($a, $b) 
      {
      	return $a + $b;
      }
      
      // Pass by reference
      function swap(&$a, &$b) 
      {
      	$temp = $a;
      	$a = $b;
      	$b = $temp;
      }
      
    5. Default parameter values
      // Default arguments must be on right side
      function greet($name, $greeting="Hello") 
      {
      	echo "$greeting, $name!";
      }
      
      greet("Susan");         // Hello, Susan!
      greet("Rita", "Hola");  // Hola, Rita!
      
    6. Functions and arrays
      1. Pass by value
        // Pass by value
        function sum_array($values) 
        {
        	$sum = 0;
        	foreach ($values as $num)
        		$sum += $num;
        	return $sum;
        }
        
        $nums = array(1, 2, 3);
        print "Sum of array = " sum_array($nums);  // 6
        
      2. Pass by reference
        // Pass by reference
        function randomize(&$nums) 
        {
        	for ($i = 0; $i < 10; $i++)
        		$nums[$i] = rand(0, 100);  // 0-100
        }
        
        $n = array();
        randomize($n);   // Place 10 random nums in $n 
        
      3. Returning an array
        // Return an array				
        function special_nums() 
        {
        	return array(3.142, 2.718, 1.618);
        }
        
        list($pi, $euler, $phi) = special_nums();
        
    7. Global variables
      1. All variables have local scope
      2. Use global keyword to indicate global var is being used
        $x = "test";
        
        function display() {
           global $x;
           echo $x;
        }