Forum Moderators: coopster

Message Too Old, No Replies

Variable Operators in PHP

Possible within a function?

         

knighty

3:22 pm on Jan 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




I've got som code like :

if ($myrow[$x]<20) { $color="green";}

I have tons of these staements which basically give a cell a certain colour depending on the value of $myrow[$x]

Anyway - trying to functionize it with :

$green="<20"
if ($myrow[$x]$green) { $color="green";}

But it aint working, can't find the answer is it possible?

the value of green could be ==20, <=20, <20 etc.

lorax

5:38 pm on Jan 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is possible but you need to declare the variables outside of the function otherwise they exist only within the function. Check out: [php.net...] and [php.net...]

toadhall

6:12 pm on Jan 16, 2003 (gmt 0)

10+ Year Member



True about global variables and functions, but the first problem is the comparison operator "<" stored in a variable.

As it's written the less-than symbol will be taken literally; it won't be read as an operator.

T

andreasfriedrich

6:42 pm on Jan 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



toadhall is perfectly right there.

Use eval [php.net] to evaluate the expression in the strings.

if (eval("$myrow[$x]$green;")) { $color="green";}

I´m not sure though whether that is the best way to achieve what you want. Since I´m not exactly sure what you want either it is really hard to go beyond the advice above.

Andreas

lorax

6:44 pm on Jan 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Gaack... how did I miss that? :)

knighty

8:53 am on Jan 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lorax - I already have the variables set as global

toadhall, andreasfriedrich - thanks for the heads up, looks promising I'll let you know how I get on.

Basicaly I have a loop that color codes a table cell depending on the value, the color will either be red, green or yellow. Then I have 19 subsections to the table each with their rules, ie section 1 green might be <20 while for section 2 green could be >=45 etc.

Hopefully for each section I will just have a function where I can pass the operators and thier values.

suggestions welcome :)

knighty

5:09 pm on Jan 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



***UPDATE***

ARRGGHHHH

Can't get eval to work properly the code doesn't seem to notice the operator

<?
function larger($x,$y,$z)
{
if (eval("$x $z $y;"))
{echo "$x is the same as $y";}
echo "<p>$x $z $y<p>";
}
larger(2,2, '==');
?>

Its probbably just syntax or something - dont I just want to eval $z?

andreasfriedrich

5:19 pm on Jan 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry knighty, I was thinking Perl when I suggested that code.

In PHP 4, eval() returns NULL unless return() is called in the evaluated code, in which case the value passed to return() is returned.

[php.net...]

So your code should probably read something like this:

if (eval("return($x $z $y);"))

Andreas

knighty

5:24 pm on Jan 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WooHoo!

Thanks Andreas,

works on that example - will try a more advanced script after the weekend.

Its Friday and home time!

I did read up on the eval function but must have missed the part about it not returning a value. DOH!

andreasfriedrich

5:28 pm on Jan 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Happy weekend then.

lorax

6:07 pm on Jan 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it!

One last comment on returning a value from a function - it will only return one value. So if you need to return more than one, build an array and return it.

knighty

4:13 pm on Jan 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


OK...another dumb qustion

made a function which seems to work buyt only if I put the echo statment in the last braces and then I get more than 13 results (its in a loop)

function color_code($heading, $green, $amber, $red)
{
global $heading, $x, $code, $light, $color, $myrow;
while ($x < $heading) {
if ($myrow[$x]!="$code"[smilestopper]) {
if ($myrow[$x]!=""[smilestopper]) {
if (eval("return($myrow[$x] $green);"[smilestopper])) { $light="green"; $color[$x]=100;}
if (eval("return($myrow[$x] $amber);"[smilestopper])) { $light="amber"; $color[$x]=50;}
if (eval("return($myrow[$x] $red);"[smilestopper])) { $light="red"; $color[$x]=0;}
}
else { $light="white"; $color[$x]="";}
echo "<td class='$light'>$myrow[$x]%</td>\n";
}
}
}

color_code(13, '<20', '==20', '>25');

What the best way to do this?

get $light and $myrow into an array then echo those results outside of the array?

Or use return;? or is there another better way to do this? I've read php.net and can't seem to find a decent example.