0

I am trying to include two .php files into another .php file that's included in index.php.

Their contents is irrelevant to the issue because with just one include it works fine.

I have the following files:

index.php

<!DOCTYPE html>
<html>    

<?php
    include('inputs.php');
?>
.
.
.
</html>

inputs.php

<?php

include('hex_to_rgb.php');
// include('rgb_to_hex.php');
.
.
.
?>

hex_to_rgb.php

<?php

class RGB
{
    public $R;
    public $G;
    public $B;
}

function HexadecimalToDecimal($hex)
{
    $hex = strtoupper($hex);

    $hexLength = strlen($hex);
    $dec = 0;

    for ($i = 0; $i < $hexLength; $i++)
    {
        $b = $hex[$i];

        if ($b >= 48 && $b <= 57)
            $b -= 48;
        else if ($b >= 65 && $b <= 70)
            $b -= 55;

        $dec += $b * pow(16, (($hexLength - $i) - 1));
    }

    return (int)$dec;
}

function HexadecimalToRGB($hex) {
    if ($hex[0] == '#')
        $hex = substr($hex, 1);

    $rgb = new RGB();

    $rgb->R = floor(HexadecimalToDecimal(substr($hex, 0, 2)));
    $rgb->G = floor(HexadecimalToDecimal(substr($hex, 2, 2)));
    $rgb->B = floor(HexadecimalToDecimal(substr($hex, 4, 2)));

    return $rgb;
}

?>

rgb_to_hex.php

<?php

class RGB
{
    public $R;
    public $G;
    public $B;
}

function DecimalToHexadecimal($dec)
{
    if ($dec < 1) return "00";

    $hex = $dec;
    $hexStr = "";

    while ($dec > 0)
    {
        $hex = $dec % 16;

        if ($hex < 10)
            $hexStr = substr_replace($hexStr, chr($hex + 48), 0, 0);
        else
            $hexStr = substr_replace($hexStr, chr($hex + 55), 0, 0);

        $dec = floor($dec / 16);
    }

    return $hexStr;
}

function RGBToHexadecimal($rgb) {
    $rs = DecimalToHexadecimal($rgb->R);
    $gs = DecimalToHexadecimal($rgb->G);
    $bs = DecimalToHexadecimal($rgb->B);

    return "#" . $rs . $gs . $bs;
}

?>

The only problem that I see is that both hex_to_rgb.php and rgb_to_hex.php declare and use similar variables, methods and classes. But the thing is that I don't use any of those variables, methods or classes inside inputs.php.

I've added the following code inside index.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
?>

<?php
   print_r(array_keys(get_defined_vars()));
   print_r(array_values(get_defined_vars()));
?>

I don't see any error when it's just one include. But when I have two includes, I don't see anything, because everything is white. It's almost hypnotic.

What do you think ?

EDIT:

I've ended up deleting both hex_to_rgb.php and rgb_to_hex.php and modifying and using just one function that suits my needs:

function HexadecimalToRGB($hex) {
    if ($hex[0] == '#')
        $hex = substr($hex, 1);

    $rgb = floor(hexdec(substr($hex, 0, 2)))." ".
    floor(hexdec(substr($hex, 2, 2)))." ".
    floor(hexdec(substr($hex, 4, 2)));
    
    return $rgb;
}
Community
  • 1
  • 1
bleah1
  • 471
  • 3
  • 18
  • 1
    You're declaring the `RGB` class twice, you can't do that. Simplest solution would be to put the RGB class into its own separate (i.e., third) include file and remove it from both the other two. – Alex Howansky Jun 13 '19 at 15:02
  • 1
    Also see [hexdec](https://www.php.net/manual/en/function.hexdec.php) and [dechex](https://www.php.net/manual/en/function.dechex.php). – Alex Howansky Jun 13 '19 at 15:05
  • Yes. I've included the RGB class in `inputs.php`. But hexdec and dechex don't really help me. Because it implies a whole-other implementation to go from decimal to RGB. – bleah1 Jun 13 '19 at 15:46
  • As @AlexHowansky says, the problem is that you are including TWO files with TWO definitions of RGB class. Take a look at PHP errors next time (you are getting a blank page because you are not displaying errors) and they will give you the hint. – Natxet Jun 13 '19 at 16:33
  • Reading your [**PHP error log**](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel) will resolve your issue. – Martin Jun 13 '19 at 17:28

2 Answers2

2

You have 2 classes with the same name, RGB. Change their name or use just one of them, having the class in a single file.

Edit: As said by user @Martin:

PHP can have the same function name as long as they are in different namespaces.

It is not the case of the post question, but that is an extra information that can help people in the future.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • While this is the correct answer, for the sake of completeness I would like to add that PHP can have the same function names [as long as they are in **different namespaces**](https://www.php.net/manual/en/language.namespaces.basics.php). – Martin Jun 13 '19 at 17:36
  • 1
    Agreed Martin, that fits well the answer. I am going to edit it and add your commend on it. For future user's who come in this post. – Marcus V. B. Siqueira Jun 13 '19 at 17:38
1

The only problem that I see

And therein lies your problem.

Without leaving your browser you could have selected "view source" and seen whether anything was returned by the script (in this case it would not have been).

PHP is reporting errors but you have configured your server to hide them from you. If this is a development machine then add...

error_reporting(E_ALL);

at the top of your PHP code. Ideally you should also set up error logging.

symcbean
  • 47,736
  • 6
  • 59
  • 94