PHP code to calculate the resistance value of a resistor based on its color bands
Mark E.
Here's an example PHP code to calculate the resistance value of a resistor based on its color bands:
<?php
$color_codes = [
'black' => 0,
'brown' => 1,
'red' => 2,
'orange' => 3,
'yellow' => 4,
'green' => 5,
'blue' => 6,
'violet' => 7,
'gray' => 8,
'white' => 9
];
$first_band = 'brown'; // replace with the actual color of the first band
$second_band = 'black'; // replace with the actual color of the second band
$third_band = 'red'; // replace with the actual color of the third band
$multiplier = 1;
$tolerance = 0.05;
$first_digit = $color_codes[$first_band];
$second_digit = $color_codes[$second_band];
$third_digit = $color_codes[$third_band];
$ohms = ($first_digit * 10 + $second_digit) * $multiplier;
$ohms_string = number_format($ohms, 2);
echo "Resistance value: {$ohms_string} ohms (+/-{$tolerance})";
?>