TheCarver
TheCarver

Reputation: 19733

PHP - Problems calling a function with parameters

I need to call a function, in PHP, that accepts 3 parameters, RGB Values. This function converts RGB color values to HSL values, so (R,G,B) is needed in the parenthesis.

This is my function:

function RGBtoHSL($red, $green, $blue) {
  // convert colors
}

Which, if I make a test call of the following, it works just fine:

RGBtoHSL(255,0,0);

and also works like this:

RGBtoHSL(255,000,000);

Now, further down my page I have a variable $displayRGB which holds the current pixels RGB values in this format xxx,xxx,xxx. I've echoed this variable to test the format matches my requirements and it does, but when I try and add this variable to my function caller, it fails with the error "Missing argument 2, Missing argument 3" and points to this line:

RGBtoHSL($displayRGB);

I'm still teething in PHP (come from ASP), can somebody please help point me in the right direction and pass me my dummy?

Upvotes: 2

Views: 128

Answers (6)

w43L
w43L

Reputation: 565

try this instead of eval

call_user_func_array('RGBtoHSL', explode(',', $displayRGB));

Upvotes: 1

David
David

Reputation: 3821

You can't put a string in a function and expect it to explode the string for you.

You need to go like this:

$string = '255,0,0';
$array = explode(',', $string);
RGBtoHSL($array[0], $array[1], $array[2]);

Upvotes: -2

Tim Withers
Tim Withers

Reputation: 12069

I will probably get flamed for this, but this would definitely work:

eval("RGBtoHSL($displayRGB);");

Don't do it. It will work... but don't do it.

Upvotes: -2

Jerzy Zawadzki
Jerzy Zawadzki

Reputation: 1985

what's your $displayRGB value?

if it's "255,0,0" you should first do "explode"

e.g.

<?php

list($r,$g,$b)=explode(',',$displayRGB);
RGBtoHSL($r,$g,$b);

Upvotes: 0

Mekswoll
Mekswoll

Reputation: 1433

Your $displayRGB is a single variable (of type string, I presume). What you can do is split this string into an array:

$rgbArray = explode(',', $displayRGB);

Then pass it to your function

RGBtoHSL($rgbArray[0], $rgbArray[1], $rgbArray[2]);

Upvotes: 4

Matt
Matt

Reputation: 7050

You can't pass in an array (I assume $displayRGB is an array) as "all three arguments" in PHP. Try

RGBtoHSL($displayRGB[0], $displayRGB[1], $displayRGB[2]);

or modify your function to accept an array.

If $displayRGB is a string of "xxx,yyy,zzz" you can run an explode on it

$colors = explode(",", $displayRGB);

and it will set $colors as an array with indices containing xxx, yyy and zzz.

Then pass it as I mentioned above.

Upvotes: 3

Related Questions