<?php
echo ohmsLaw_1(12, 220, "")."\n";
echo ohmsLaw_1(230, "", 2) ."\n";
echo ohmsLaw_1("", 220, 0.02) ."\n";
echo ohmsLaw_1("", "", 10)."\n";
echo ohmsLaw_1(500, 50, 10)."\n";
function ohmsLaw_1($voltage, $resistance, $current) {
if ($voltage == "" && $resistance == "" ||
$voltage == "" && $current == "" ||
$resistance == "" && $current == "" ||
$voltage != "" && $resistance != "" && $current != "") {
return "Invalid";
}
if ($voltage == "") {
$res = $resistance*$current;
}
if ($resistance == "") {
$res = $voltage/$current;
}
if ($current == "") {
$res = $voltage/$resistance;
}
return $res;
}
//using empty function
function ohmsLaw_2($voltage, $resistance, $current) {
if (empty($voltage) && empty($resistance) ||
empty($voltage) && empty($current) ||
empty($resistance) && empty($current) ||
!empty($voltage) && !empty($resistance) && !empty($current)
) {
return "Invalid";
}
if (empty($voltage)) {
$res = $resistance*$current;
}
if (empty($resistance)) {
$res = $voltage/$current;
}
if (empty($current)) {
$res = $voltage/$resistance;
}
return round($res, 2);
}