PHP - Switch case example

0 votes
414 views
added Mar 7, 2019 in PHP by LC Marshal Captain (25,790 points)
edited Aug 9, 2019 by LC Marshal
<?php
function translate_lease_term( $lease_term_id = NULL ){
  $lease_term_id = trim( $lease_term_id );
  if ( !isset( $lease_term_id ) || $lease_term_id === '' )
    return FALSE;

  switch ( $lease_term_id ) {
    case 'fh':
    return 'Freehold';
    case '999lh':
    return '999 years';
    case '99lh':
    return '99 years';
    case '30lh':
    return '30 years';
    case '60lh':
    return '60 years';
    case 'lh':
    return 'Leasehold';
    case 'mrl':
    return 'Malay Reserved Land';
//    default will output if all cases not exist
    default:
    return FALSE;
  }

}
?>

 

2 Responses

0 votes
responded Aug 9, 2019 by LC Marshal Captain (25,790 points)
// switch case for time update
<?php
  $popularList = json_decode($popularListJson);
  foreach($popularList as $news):
  $ago = $news->days_ago;
  switch ($ago){
    case 0:
      $days_ago = 'Today';
      break;
    case 1:
    $days_ago = $ago.' day ago';
      break;
    default:
    $days_ago = $ago.' days ago';
  }			
?>

 

0 votes
responded Nov 5, 2019 by LC Marshal Captain (25,790 points)
<?php

switch ($color) {
  
case "silver":
  echo "Defender";
  break;
  
case "black":
  echo "Escape";
  break;
  
case "dull black":
  echo "Terios";
  break;
}

?>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...