Jump to content
MakeWebGames

area (quick version)


Zero-Affect

Recommended Posts

function area($vals, $func='square') {
# base first in all calculations
/*
# This could of been used to identify what shape but i thought it would be easier to just simply type it out, you could edit this though.
$array = array(
1 => 'square', 
2 => 'rectangle', 
3 => 'parallelogram', 
4 => 'triangle'
);
*/
if ( !preg_match('/^[0-9]+(,[0-9]+)*$/', $vals) ) {
$ret_txt = 'You didn\'t input the correct value format.'; // could also be FALSE
}
if ( in_array($func, array('square','rectangle','parallelogram','triangle')) ) {
$ret_txt = 'You can only get the area of the following shapes, square, rectangle, parallelogram, triangle.'; // could also be FALSE
}
	$split_vals = explode(',', $vals);
  if ( in_array($func, array('rectangle','parallelogram')) && count($split_vals) == 2 ) {
$ret_txt = ($split_vals[0]) * ($split_vals[1]);
  } elseif ( $func == 'square' && count($split_vals) == 1 ) {
$ret_txt = ($split_vals[0]) * ($split_vals[0]);
  } elseif ( $func == 'triangle' && count($split_vals) == 2 ) {
$ret_txt = ($split_vals[0] / 2) * ($split_vals[1]);
  } else {
$ret_txt = 'You didn\'t input the correct values.'; // could also be FALSE
  }
return $ret_txt;
}
  echo area('3','square'); // results in 9
echo '
';
  echo area('2,4','triangle'); // results in 4
echo '
';
  echo area('3,5', 'rectangle'); // results in 15
echo '
';
  echo area('7,9', 'parallelogram'); // results in 63
echo '
';
  echo area('1,2', 'square'); // fails
echo '
';
  echo area('1', 'noidea'); // fails

May come in useful to people who need it, have fun...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...