0

I am starting to learn PHP and encounter the issue below:

function dechex_helper($dec){
    $hex = dechex($dec);
    if($hex == 0){
        return '00';
    }
    return $hex;
}

function webColors($red, $green, $blue){
    return '#'.dechex_helper($red).dechex_helper($green).dechex_helper($blue);
}

echo 'webColors(255, 0, 0)'.': '.webColors(255, 0, 0).'<br>';
echo 'webColors(0, 0, 255)'.': '.webColors(0, 0, 255).'<br>';
echo 'webColors(255, 0, 191)'.': '.webColors(255, 0, 191).'<br>';
echo 'webColors(0, 0, 0)'.': '.webColors(0, 0, 0).'<br>';

Result:

webColors(255, 0, 0): #000000
webColors(0, 0, 255): #000000
webColors(255, 0, 191): #000000
webColors(0, 0, 0): #000000

but if I change the line:

if($hex == 0){

To:

if($hex === '0'){

The result will be correct:

webColors(255, 0, 0): #ff0000
webColors(0, 0, 255): #0000ff
webColors(255, 0, 191): #ff00bf
webColors(0, 0, 0): #000000

Can someone explain what happens with it?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
MsLate
  • 149
  • 2
  • 10

0 Answers0