Forum Moderators: coopster

Message Too Old, No Replies

Variable condition statements

         

Jabocra

12:32 pm on Nov 9, 2003 (gmt 0)

10+ Year Member



I'm trying to build a conditional statement based on the script users settings which is then used in an if() construct.

for example:
$lines = "$condition1==apples ¦¦ $condition2==pears ¦¦ $condition3==plums"

I know that the conditional statement is getting built properly because I was able to echo it to make sure. The problem is that I can't get the string in $lines to be evaluated inside the if() condition. I have a feeling that I may need to use the eval() function, but am not sure how to do this. Help please :)

The code in question looks like this

if($lines) {
Do something....
}else{
Do something else
}

Thanks

lorax

1:15 pm on Nov 9, 2003 (gmt 0)

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



Welcome to WebmasterWorld Jacobra!

I think you're on the right track. Your example of if($lines) will fail because the var $lines is not being read. I haven't run into this situation but I think you're on the right track. Have you tried using eval() to see what you get?

Jabocra

4:15 pm on Nov 9, 2003 (gmt 0)

10+ Year Member



Thnx for the welcome lorax :)

Yes, I've been trying to find a way to use eval().

The snippet of code I posted earlier is in a loop, this is a more accurate representation of what I am trying to do.


$lines = "$condition==2 ¦¦ $condition==6 ¦¦ $condition==15";
$list=40;

for ($condition=1; $condition<=$list; $condition++) {

if (eval("\$lines=\"$lines\";")) {
Do something....
}else{
Do something else...
}

}

the problem I have is that the value of $condition doesn't increase in the eval'd statement, it stays at "1" for each occurrence of $condition in $lines, which kinda makes sense...

the more I play with this I am thinking maybe I am approaching it from the wrong angle?

dcrombie

6:19 pm on Nov 9, 2003 (gmt 0)



Have you tried using the PHP switch statement? I'm not sure exactly what you're trying to do so this might be on the wrong track as well.

[php.net ]

coopster

2:20 am on Nov 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



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

<?php
$lines = '$condition==2 ¦¦ $condition==6 ¦¦ $condition==15';
$list=40;
for ($condition=1; $condition<=$list; $condition++) {
if (eval("return $lines;")) {
// Do something
} else {
// Do something else
}
}
?>

Jabocra

2:42 am on Nov 10, 2003 (gmt 0)

10+ Year Member



ahhhh, beautiful coopster!

works like a charm, thanks ever so much!

lorax

3:12 am on Nov 10, 2003 (gmt 0)

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



Darn... this is the umpteenth time I've missed out on participating in the solution! Gotta stop going to family functions, sleep, dinner, walks.... ;)

Nice one coopster. Jabocra - sorry about the mispelling in my first post.

Jabocra

3:24 am on Nov 10, 2003 (gmt 0)

10+ Year Member



np lorax - I'm sure I'll be back with another dilemma you can take a crack at. ;)

thnx to everyone who replied.