Day2 with PHP! Array and Function (My personal testing only!)

<?php
  function writeMsg(){
  echo "Hello world!";
  }
  writeMsg();
  echo "<br>";
  function familyName($fname){
  echo "$fname Refsnes.<br>";
  }
  familyName("Jani");
  familyName("Hege");
  familyName("Stale");
  familyName("Kai Jim");
  familyName("Borge");

  function familyGroup($fname, $year){
  echo "$fname Set. Born in $year.<br>";
  }
  familyGroup("Say", "1999");
  familyGroup("Yeoun","3000");
  familyGroup("Kann","2013");

  function setHeight($minheight = 50){
  echo "The height is : $minheight<br>";
  }
  setHeight(300);
  setHeight();
  setHeight(2000);
  
  function sum($x, $y){
  $z = $x + $y;
  return $z;
  }
  echo "5+10=".sum(5,10);
//-------------------------------------------------------

/*
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
*/

  $cars = array("Volvo" ,"BMW","Toyota" );
  echo "<br>".count($cars);
  $arrlength = count($cars);
  for ($x=0; $x <$arrlength ; $x++) { 
  echo $cars[$x];
  echo "<br>";
  }

  //Associative arrays are arrays that use named key that you assign to them
  $age  = array('Peter' =>'33' ,'Ben' =>'12','Thida' =>'55' );
  echo "Peter is ".$age['Peter']."years old.";
  echo "<br>";
  foreach ($age as $key => $value) {
  echo "Key = ".$key.", Value= ".$value."<br>";
  }  

  // Sort Array in Ascending Order 
  $cars = array("Volvo","BMW","Toyota");
  sort($cars);// sort in ascending order -sort()
  $arrlength = count($cars);
  for ($x=0; $x <$arrlength ; $x++) { 
  echo $cars[$x]."<br>";
  }

  $numbers = array(45,13,8,90,70 );
  rsort($numbers);//sort in descending order -rsort()
  $arrlength = count($numbers);
  for ($x=0; $x <$arrlength ; $x++) { 
  echo $numbers[$x]."<br>";
  }

  
  $age = array('Veasna' =>'44' ,'Sokha' =>'4','Dara' =>'22' );
  asort($age);//Sort Array in ascending order, according to value -asort()
  foreach ($age as $key => $value) {
  echo "Key= ".$key.", Value= ".$value."<br>";
  }
  ksort($age);//Sort array in ascending order, according to key -ksort()
  foreach ($age as $key => $value) {
  echo "Key= ".$key.", Value".$value."<br>";
  }

?>
Previous Post Next Post