Supposing I have a hex color code in a string such as:
$color = '#fda80d';
How could I convert this into an actual hex value such that:
$color = 0xfda80d;
So that it is not a string but an actual hex value?
Supposing I have a hex color code in a string such as:
$color = '#fda80d';
How could I convert this into an actual hex value such that:
$color = 0xfda80d;
So that it is not a string but an actual hex value?
$color = '#fda80d';
$color = hexdec(substr($color, 1));
// Prove it
echo $color == 0xfda80d ? "yes" : "failed";
The below code shows that PHP stores numbers as base 10 but you can do math on the number in any base. I've created a demo below that should give you a better idea of how you can use this to compare numbers in different bases against each other. You can also do math on numbers in different bases:
<?php
$color = '#fda80d'; //String color
$color = substr($color,1,6); //String color without the #
$color = '0x'.$color; //Prepend the 0x
$color += 0x00; //Force it to be a number
if($color === 0xfda80d) //Check the number is EXACTLY what we are expecting
echo 'Is hex<br />'; //This code is run, proving it is a hex ***number***
echo $color; //Outputs 16623629 (base 10)
#================================#
echo '<br /><br />';
$octal = 011; //Define an octal number
$octal += 01; //Add 1
if($octal === 012)
echo 'Is octal<br />';
echo $octal; //Outputs 10 (base 10)
#================================#
echo '<br /><br />';
$number = 12345; //Define a standard base 10 int
$number += 1; //Add 1 to the int
if($number === 12346)
echo 'Is base 10<br />';
echo $number; //Outputs 12346 (base 10)
#================================#
echo '<br /><br />';
if($color === 16623629 && $color === 077324015 && $color === 0xFDA80D)
echo '$color is base 10, octal and hex all at the same time<br />';
if($octal === 10 && $octal === 012 && $octal === 0xA)
echo '$octal is base 10, octal and hex all at the same time<br />';
if($number === 12346 && $number === 030072 && $number === 0x303A)
echo '$number is base 10, octal and hex all at the same time';
?>
Outputs:
Is hex
16623629
Is octal
10
Is base 10
12346
$color is base 10, octal and hex all at the same time
$octal is base 10, octal and hex all at the same time
$number is base 10, octal and hex all at the same time
You can check a base 10 number (or hex/oct), for example 10
, is equal to the hex number 0xA
on the fly without any conversion:
<?php
if(10 === 0xA)
echo 'Success'; //This code is run
?>
You can look into PHP's bin2hex(). http://php.net/manual/en/function.bin2hex.php
var_dump(dechex(hexdec(str_replace('#', '', '#fda80d'))))
is a string, so probably there is no way you can get 0xfda80d
like you wrote it.