function simple_calculator ($number1, $operator, $number2
{
// calculator
switch ($operator) {
case "plus"
$res = $number1$number2
break
case "minus"
$res = $number1$number2
break
case "times"
$res = $number1$number2
break
case "divided by"
$res = $number1$number2
break
default
$res = 0;
}
return $res
}
Tag: Single Digit Calculator
eXorithm – Execute Algorithm: View / Run Algorithm show_address
function show_address ($address
{
$data = file_get_contents"http://maps.googleapis.com/maps/api/geocode/json?address="urlencode$address)."&sensor=false");
$obj = json_decode$data);
if ($obj) {
if (isset$obj->results[0]->geometry->location)) {
$loc = $obj->results[0]->geometry->location;
return array'latitude'=>$loc->lat, 'longitude'=>$loc->lng);
} else {
throw new Exception'Lookup failed and/or address does not exist!');
}
} else {
throw new Exception'Lookup failed and/or address does not exist!');
}
}
eXorithm – Execute Algorithm: View / Run Algorithm show_unix_time
function show_unix_time ($time, $format
{
return date$format,($time == 0) ? time() : $time);
}
eXorithm – Execute Algorithm: View / Run Algorithm scale_image
function scale_image ($image, $width, $height, $percent
{
$ewidth = imagesx$image);
$eheight = imagesy$image);
if ($percent) {
$width = round(($width/100)*$ewidth);
$height = round(($height/100)*$eheight);
}
$image2 = image_create_alpha$width, $height);
for ($x=0;$x$width$x++) {
$x1 = floor$x * ($ewidth$width));
for ($y=0;$y$height$y++) {
$y1 = floor$y * ($eheight$height));
$index = imagecolorat$image, $x1, $y1);
$color = imagecolorsforindex$image, $index);
$trans = imagecolorallocatealpha$image2
$color'red'],
$color'green'],
$color'blue'],
$color'alpha']);
imagesetpixel$image2, $x, $y, $trans);
}
}
return $image2
}
eXorithm – Execute Algorithm: View / Run Algorithm round_up
function round_up ($num, $precision
{
$pow = pow(10, $precision);
return ceil$num * $pow)/$pow
}
eXorithm – Execute Algorithm: View / Run Algorithm simple_recursor
function simple_recursor ($count
{
if ($count>=10) {
return $count
} else {
$x = simple_recursor$count+1);
return $x" - "$count
}
}
eXorithm – Execute Algorithm: View / Run Algorithm shorten_url
function shorten_url ($url, $limit, $dotdotdot
{
$length = strlen$url);
if ($length > $limit) {
$s = substr$url, 0, floor(2*$limit/3));
$e = substr$url, -1*floor$limit/3));
return $s$dotdotdot$e
} else {
return $url
}
}
eXorithm – Execute Algorithm: View / Run Algorithm sort_multi_array
function sort_multi_array ($array, $key
{
$keys = array();
for ($i=1;$ifunc_num_args();$i++) {
$keys$i-1] = func_get_arg$i);
}
// create a custom search function to pass to usort
$func = function ($a, $b) use ($keys) {
for ($i=0;$icount$keys);$i++) {
if ($a$keys$i]] != $b$keys$i]]) {
return ($a$keys$i]] < $b$keys$i]]) ? -1 : 1;
}
}
return 0;
};
usort$array, $func);
return $array
}
eXorithm – Execute Algorithm: View / Run Algorithm simple_sort
function simple_sort ($array
{
sort$array);
return $array
}
eXorithm – Execute Algorithm: View / Run Algorithm rot13
function rot13 ($string
{
for$i=0;$istrlen$string);$i++) {
$jord$string$i]);
if ((($j>=ord"n")) & ($j<=ord"z"))) | ($j>=ord"N")) & ($j<=ord"Z"))) {
$j$j-13;
}
elseif ((($j>=ord"a")) & ($j<=ord"m"))) | ($j>=ord"A")) & ($j<=ord"M"))) {
$j$j+13;
}
$new.=chr$j);
}
return$new);
}