// Electronic Power Calculator
// v 1.4
// (c) JWSmythe, http://jwsmythe.com
$page_name = "Calc V|A|W|Ω";
$debug = 0;
#include_once("../../header.php");
include_once("header.php");
// $in[] is the inbound value, which will be put in the Input column of the form.
// $out[] is the outbound value, calculated or passed through
// Sanitize field before processing.
if(!empty($_REQUEST['volts'])){ $in['volts'] = floatval($_REQUEST['volts']); }else{ $in['volts'] = ''; };
if(!empty($_REQUEST['amps'])) { $in['amps'] = floatval($_REQUEST['amps']); }else{ $in['amps'] = ''; };
if(!empty($_REQUEST['watts'])){ $in['watts'] = floatval($_REQUEST['watts']); }else{ $in['watts'] = ''; };
if(!empty($_REQUEST['ohms'])) { $in['ohms'] = floatval($_REQUEST['ohms']); }else{ $in['ohms'] = ''; };
#$in['volts'] = floatval($_REQUEST['volts']);
#$in['amps'] = floatval($_REQUEST['amps']);
#$in['watts'] = floatval($_REQUEST['watts']);
#$in['ohms'] = floatval($_REQUEST['ohms']);
// predefine out array
$out = array('volts' => '',
'amps' => '',
'watts' => '',
'ohms' => ''
);
// Clear zeroes. The above makes empty fields =0.
if ($in['volts'] == '0'){ $in['volts'] = ''; }else{ $out['volts'] = $in['volts']; };
if ($in['amps'] == '0'){ $in['amps'] = ''; }else{ $out['amps'] = $in['amps']; };
if ($in['watts'] == '0'){ $in['watts'] = ''; }else{ $out['watts'] = $in['watts']; };
if ($in['ohms'] == '0'){ $in['ohms'] = ''; }else{ $out['ohms'] = $in['ohms']; };
// Process through all possible options until something is found, or nothing.
// Run this a few times, just in case. Overkill, but may be useful later.
$count = 0;
while ($count <= 3){
$count++;
// ===== VOLTS ==================================
if ($out['volts'] == ''){
if ( ($in['amps'] != '') && ($in['ohms']) ){
$out['volts'] = $in['amps'] * $in['ohms'];
};
if ($debug == 1){
if ($out['volts'] != ''){ print "Conversion [V1] V=I x R : " . $out['volts'] . " = " . $in['amps'] . "*" . $in['ohms'] .";
"; };
};
};
if ($out['volts'] == ''){
if ( ($in['watts'] != '') && ($in['amps']) ){
$out['volts'] = $in['watts'] / $in['amps'];
};
if ($debug == 1){
if ($out['volts'] != ''){ print "Conversion [V2] V=P/R : ".$out['volts']." = ".$in['watts']." / ".$in['amps'].";
"; };
}
};
if ($out['volts'] == ''){
if ( ($in['watts'] != '') && ($in['ohms']) ){
$out['volts'] = sqrt($in['watts'] * $in['ohms']);
};
if ($debug == 1){
if ($out['volts'] != ''){ print "Conversion [V3] √ P x R : ".$out['volts']." = ".sqrt($in['watts']." * ".$in['ohms']).";
"; };
}
};
// ===== AMPS ==================================
if ($out['amps'] == ''){
if ( ($in['volts'] != '') && ($in['ohms']) ){
$out['amps'] = $in['volts'] / $in['ohms'];
};
if ($debug == 1){
if ($out['amps'] != ''){ print "Conversion [A1] I = V⁄R : ". $out['amps']." = ".$in['volts']." / ".$in['ohms'].";
"; };
}
};
if ($out['amps'] == ''){
if ( ($in['watts'] != '') && ($in['volts']) ){
$out['amps'] = $in['watts'] / $in['volts'];
};
if ($debug == 1){
if ($out['amps'] != ''){ print "Conversion [A2] I = P⁄V : ". $out['amps']." = ".$in['watts']." / ".$in['volts'].";
"; };
}
};
if ($out['amps'] == ''){
if ( ($in['watts'] != '') && ($in['ohms']) ){
$out['amps'] = sqrt($in['watts'] / $in['ohms']);
};
if ($debug == 1){
if ($out['amps'] != ''){ print "Conversion [A3] I = √ P⁄R : ".$out['amps']." = sqrt(".$in['watts']." / ".$in['ohms'].");
"; };
}
};
// ===== ['watts'] ==================================
if ($out['watts'] == ''){
if ( ($in['volts'] != '') && ($in['amps'] != '') ){
$out['watts'] = $in['volts'] * $in['amps'];
};
if ($debug == 1){
if ($out['watts'] != ''){ print "Conversion [W1] I = √ P⁄R : ".$out['amps']." = sqrt(".$in['watts']." / ".$in['ohms'].");
"; };
}
};
if ($out['watts'] == ''){
if ( ($in['volts'] != '') && ($in['ohms'] != '') ){
$out['watts'] = pow($in['volts'],2) / $in['ohms'];
};
if ($debug == 1){
if ($out['watts'] != ''){ print "Conversion [W2] P = V2⁄R : ".$out['watts']." = pow(".$in['volts'].",2) / ".$in['ohms'].";
";
}
};
};
if ($out['watts'] == ''){
if ( ($in['amps'] != '') && ($in['ohms'] != '') ){
$out['watts'] = pow($in['amps'],2) * $in['ohms'];
};
if ($debug == 1){
if ($out['watts'] != ''){ print "Conversion [W3] P = I2 x R : ".$out['watts']." = pow(".$in['amps'].",2) * ".$in['ohms'].";
"; };
}
};
// ===== OHMS ==================================
if ($out['ohms'] == ''){
if ( ($in['volts'] != '') && ($in['amps']) ){
$out['ohms'] = $in['volts'] / $in['amps'];
};
if ($debug == 1){
if ($out['ohms'] != ''){ print "Conversion [R1] R = V⁄I :".$out['ohms']." = ".$in['volts']." / ".$in['amps'].";
"; };
}
};
if ($out['ohms'] == ''){
if ( ($in['volts'] != '') && ($in['watts']) ){
$out['ohms'] = pow($in['volts'],2) / $in['watts'];
};
if ($debug == 1){
if ($out['ohms'] != ''){ print "Conversion [R2] R = V2⁄P : ".$out['ohms']." = pow(".$in['volts'].",2) / ".$in['watts'].";
"; };
}
};
if ($out['ohms'] == ''){
if ( ($in['watts'] != '') && ($in['amps']) ){
$out['ohms'] = $in['watts'] / pow($in['amps'],2);
};
if ($debug == 1){
if ($out['ohms'] != ''){ print "Conversion [R3] R = P⁄I2 : ".$out['ohms']." = ".$in['watts']." / pow(".$in['amps'].",2);
"; };
}
};
};
// Fix for PHP 8.0
if (is_numeric($out['volts'])){ $out['volts'] = round($out['volts'], 3); };
if (is_numeric($out['amps'])) { $out['amps'] = round($out['amps'], 3); };
if (is_numeric($out['watts'])){ $out['watts'] = round($out['watts'], 3); };
if (is_numeric($out['ohms'])) { $out['ohms'] = round($out['ohms'], 3); };
// Find our in/out highlighting colors.
if ($in['volts'] == $out['volts']){ $out_hl['volts'] = "outfield"; }else{ $out_hl['volts'] = "outfieldcalc"; };
if ($in['amps'] == $out['amps']){ $out_hl['amps'] = "outfield"; }else{ $out_hl['amps'] = "outfieldcalc"; };
if ($in['watts'] == $out['watts']){ $out_hl['watts'] = "outfield"; }else{ $out_hl['watts'] = "outfieldcalc"; };
if ($in['ohms'] == $out['ohms']){ $out_hl['ohms'] = "outfield"; }else{ $out_hl['ohms'] = "outfieldcalc"; };
print "
";
print "
Details
|
Voltage [V] |
The measure of the power differental between two points. It is the electromotive force. This is measured in volts [V]. |
Current Iintensity
[I] |
The flow of electric charge across a surface. This is measured in amps [A]. |
Power
[P] |
The rate of energy transfer. This is measured in ['watts'] [W]. |
Resistance
[R] |
The rate of energy transfer. This is measured in ohms [Ω]. |
|
";
?>
Formulas
|
To find |
Known |
Formula |
// ===== VOLTS ============================================= ?>
// Volts from Amps Resistance --------------------------------------------- ?>
Volts |
Amps [I]
Resistance Ω
|
V = I x R
|
// Volts from ['watts'] Amps --------------------------------------------- ?>
Volts |
Watts [P]
Amps [I]
|
V = P⁄I
|
// Volts from ['watts'] Resistance --------------------------------------------- ?>
Volts |
Watts [P]
Resistance Ω
|
√ P x R
|
// ===== AMPS ============================================= ?>
// Amps from Volts Resistance --------------------------------------------- ?>
Amps [I]
|
Volts
Resistance Ω
|
I = V⁄R
|
// Amps from ['watts'] Volts --------------------------------------------- ?>
Amps [I]
|
Watts [P]
Volts
|
I = P⁄ V
|
// Amps from ['watts'] Resistance --------------------------------------------- ?>
Amps [I]
|
Watts [P]
Resistance Ω
|
I = √ P⁄R
|
// ===== ['watts'] ============================================= ?>
// ['watts'] from Volts Amps --------------------------------------------- ?>
Watts [P]
|
Volts
Amps [I]
|
P = V x I |
// ['watts'] from Volts Ohms --------------------------------------------- ?>
Watts [P]
|
Volts
Resistance Ω
|
P = V2⁄R |
// ['watts'] from Amps Ohms --------------------------------------------- ?>
Watts [P]
|
Amps [I]
Resistance Ω
|
P = I2 x R |
// ===== OHMS ============================================= ?>
// Resistance from Volts Amps --------------------------------------------- ?>
Resistance Ω
|
Volts
Amps [I]
|
R = V⁄I
|
// Resistance from Volts ['watts'] --------------------------------------------- ?>
Resistance Ω |
Volts
Watts [P]
|
R = V2⁄P |
// Resistance from ['watts'] Amps --------------------------------------------- ?>
Resistance Ω |
Watts [P]
Amps [I]
|
R = P⁄I2 |
|
/*
A⁄B
*/
?>
Further reference:
|
Revision history:
- 2020-12-02 : v1.4 - Updated code for PHP 8.0
- PHP Warning: Undefined variable $count
defined.
- PHP Warning: Undefined array key "volts"
predefined $out array.
- PHP Fatal error: Uncaught TypeError: round(): Argument #1 ($num) must be of type int|float, string given
test is_numeric()
- 2020-12-01 : v1.3 - Fixed input bug. Watts field name mangled. Fixed.
- 2020-10-25 : v1.2 - Updated PHP, to correct for warnings for lazy coding
- "Use of undefined constant amps - assumed 'amps'"
- "syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),"
- 2017-07-29 : v1.1 - Fixed pow bug. x^2 != x*x . Use pow(x, 2).
Added if ($debug == 1), and index output of which formula(s) were used, with actual values.
- 2017-07-28 : v1.0 - Original work.
|
include_once("../../footer.php");
?>