-
Dynamic operators in if conditionals
I am attempting to make an operator in an if conditional
a variable like so:
$x=1;
$y=2;
$operator='!=';
if ($x $operator $y) {
echo "Not equal!\n";
}
But obviously that doesnt work.
Is it possible to convert a string to an operator?
Any help would be appreciated.
Thanks,
Aphex_
-
-
I don't mean to distract from your particular question, but this has bugged me for some time.
In specific situations, it's nice to have variables actually change the php code itself.
For example, it would be helpful if you could get a chunk of php from an outside source, like a database, depending on the situation.
This operator question is a good example of it.
At one point, I was playing around with a user-submitted math function (form with a text box they could enter "x+1" and expect a resulting set of numbers).
But... didn't work, because I never was able to figure out how to set it up so that the input worked as math. I suppose with complex enough if statements and loops, I could use php to interpret the input, finding the various symbols and such, but that would be a lot of work.
Unless someone knows something more, I think the best way to solve your problem if to just use a bunch of manually coded if statements.
if ($operator == "!=" && $x != $y) {
//do stuff
}
Then repeat for each possibility.
So... anyone know if you CAN control the actual php script within itself?
Last edited by djr33; 05-17-2006 at 06:54 AM.
Daniel -
Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
-
-
I got this working using the eval() function after someone suggested it to me.
Thanks.
-
-
Ah. Good.
I am still very interested in the concept, though.
It's a worthwhile thing to look into for other uses.
Glad its working
Daniel -
Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
-
-
Yes... the eval() function. The boon and curse of interpreted languages.
It should almost never be necessary to use it.
And you've made the newbie's mistake and confused = with ==.
-
-
Ah, thanks. Fixed the = thing... I'm tired.
Will look into eval.
Daniel -
Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks