iwato Posted August 8, 2019 Share Posted August 8, 2019 (edited) 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 August 8, 2019 by iwato Found a solution and added a new question. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 8, 2019 Share Posted August 8, 2019 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. Link to comment Share on other sites More sharing options...
iwato Posted August 9, 2019 Author Share Posted August 9, 2019 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 More sharing options...
Ingolme Posted August 9, 2019 Share Posted August 9, 2019 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. 1 Link to comment Share on other sites More sharing options...
iwato Posted August 9, 2019 Author Share Posted August 9, 2019 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 More sharing options...
justsomeguy Posted August 9, 2019 Share Posted August 9, 2019 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. 1 Link to comment Share on other sites More sharing options...
iwato Posted August 9, 2019 Author Share Posted August 9, 2019 2 hours ago, justsomeguy said: Try it and see what happens. Yes, it works! Hooray! Hooray! Roddy Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now