CakePHP If Else Statement

Usually developer love to use data transformation for example gender input. There is two types of gender and the Male is represented with number 0 (0==Male) and female is represented as number 1 (1==Female) or the status of the record for example 1 is equivalent to active (1==Active) and 0 is equivalent to inactive (0==Inactive). Actually it is just a variables and by using the 0/1 coding scheme is essentially useful when applying regression models among others.

Rather than saving full variables value, it is strongly recommended to use data transformation variable if neccessary. PHP have if else statement where developer can make use of it to display the transformed data to become more readable for example if the list show gender as 0 and 1, it should show as Male and Female. To convert the variables to readable value, simply use these code as shown below:

<?php if ($user['User']['gender']=='0') {
		echo  "Male"; 
	} elseif ($user['User']['gender']=='1') {
		echo  "Female";
	} else
		echo "Error";
?>

Above code will check the value of the variables and will display the readable value eg: Male/Female if it meet the condition. If the value is not 0/1, then it will print 'Error'. If there is more then one condition for example status; 1) 0==Pending; 2) 1==Active; 3) 2==Inactive, the code will look like these:

<?php if ($user['User']['status']=='0') {
		echo  "Pending"; 
	} elseif ($user['User']['status']=='1') {
		echo  "Active";
	} elseif ($user['User']['status']=='2') {
		echo "Inactive";
	} else
		echo "Error";
?>

Developer can add symbol for example from font awesome to make it more meaningful and attractive. That all and happy coding :)

Tags: , ,