Jump to content
iwato

Version Change - What Changed?

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.

Share this post


Link to post
Share on other sites

Because you're trying to access or set things like $result['rgb']['r'], where $result['rgb'] is a string.  You probably meant to make it an array instead of a string.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
2 hours ago, justsomeguy said:

Try it and see what happens.

Yes, it works!  Hooray! Hooray!

Roddy

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...