Jump to content

Version Change - What Changed?


iwato

Recommended Posts

BACKGROUND:  The following newly created function works splendidly with PHP 5.3.8, but it fails nearly totally in PHP 7.2.  Still there is no error message.

The FUNCTION:

	function random_color($base = true, $type = true){
		$result = array( 'rgb' => '', 'hex' => '' );
		foreach( array('r', 'b', 'g') as $col){
			$rand = mt_rand(0, 255);
			$result['rgb'][$col] = $rand;
			$dechex = dechex($rand);
			if(strlen($dechex) < 2){
				$dechex = '0' . $dechex;
			}
			$result['hex'] .= $dechex;
		}
		if ($base == true) {
			if ($type == true) {
				return $result['rgb'];
			} else {
				return join(",", $result['rgb']);
			} 
		} else {
			return strtoupper($result['hex']);
		}
	}

QUESTION:  What must I change to get it to work with PHP 7.2.

HINT ONE:  The function delivers well when the $base parameter is set to false.  Otherwise, it returns only one of the three RGB colors no matter the value of $base or $type.

HINT TWO:  There is an error message after all. Simply, I was looking in the wrong place.

[08-Aug-2019 18:04:40 UTC] PHP Warning:  Illegal string offset 'r' in ..../includes/random_color.incl.php on line 15
[08-Aug-2019 18:04:40 UTC] PHP Warning:  Illegal string offset 'b' in ..../includes/random_color.incl.php on line 15
[08-Aug-2019 18:04:40 UTC] PHP Warning:  Illegal string offset 'g' in ..../includes/random_color.incl.php on line 15

ANSWER:

	function random_color($base = true, $type = true){
		$result = array( 'rgb' => '', 'hex' => '' );
		$keys = ['r', 'b', 'g'];
		foreach( $keys as $key => $value ){
			$rand = mt_rand(0, 255);
			$result['rgb'][$value] = $rand;
			$dechex = dechex($rand);
			if(strlen($dechex) < 2){
				$dechex = '0' . $dechex;
			}
			$result['hex'] .= $dechex;
		}
		if ($base == true) {
			if ($type == true) {
				return $result['rgb'];
			} else {
				return join(",", $result['rgb']);
			} 
		} else {
			return strtoupper($result['hex']);
		}
	}

NEW QUESTION:  But why?

Roddy

 

Edited by iwato
Found a solution and added a new question.
Link to comment
Share on other sites

So, are you saying that $result['rgb'][$col] is being read as NULL[$col]?

If so, what happened between PHP 5.3.8 and PHP 7.2 that changed $result['rgb'] from an element of an array to a string?

Roddy

Link to comment
Share on other sites

No, the problem is that you declared $result['rgb'] as a string ('rgb' => '') instead of an array, so it cannot have strings (such as 'r', 'g' or 'b') as an index. This would also be an issue in PHP 5, but you probably had the settings hiding the warnings.

  • Thanks 1
Link to comment
Share on other sites

So, if I had written

$result = array( 'rgb' => [], 'hex' => '' );

then I would not have had to rewrite the function.

Is this correct?

Roddy

Link to comment
Share on other sites

Try it and see what happens.

You can use array indices with a string to access different characters of it, e.g.:

$string = 'some string';
	$string[4] = '-';
	echo $string;

But you're trying to use an array index which is a string:

$string = 'some string';
	$string['r'] = '!';

While it shouldn't be hard to figure out which character in that string is in the 4th position, it doesn't make much sense to try and figure out which character is in the rth position.

  • Like 1
Link to comment
Share on other sites

2 hours ago, justsomeguy said:

Try it and see what happens.

Yes, it works!  Hooray! Hooray!

Roddy

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...