Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

PHP - Switch case example

0 votes
46 views
added Mar 7 in PHP by LC Marshal Captain (25,420 points)
edited Aug 9 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 by LC Marshal Captain (25,420 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 by LC Marshal Captain (25,420 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.
...