Home > Archive > PHP Programming > May 2006 > I need a mini "logic interpreter"
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
I need a mini "logic interpreter"
|
|
| Baron Samedi 2006-05-19, 3:58 am |
| I want to extend the PHP based blog system which I use.
We have static templates for the various blog pages and I would like to
introduce some logic to them.
That is, I want to have some sort of structure, like
[[if <condition>]]
[[else]]
[[endif]]
or [[for 0, 3]] // do 4 time
[[endfor]]
but I am bogged down at the very first hurdle, which is just evaluating
something like '2 == 2.".
The text from the templates will be seen as a string, of course, but
when I try to evaluate it as a bool,
echo 'Evaluated condition: "' . (bool) $text . '"<br>';
it always evaluates to true, even if my condition is '2 == 3'.
Obviously I am missing something very basic about evaluation and maybe
casting.
Can anyone enlighten me, please?
| |
| Alan Little 2006-05-19, 7:58 am |
| Carved in mystic runes upon the very living rock, the last words of Baron
Samedi of comp.lang.php make plain:
> I want to extend the PHP based blog system which I use.
>
> We have static templates for the various blog pages and I would like to
> introduce some logic to them.
>
> That is, I want to have some sort of structure, like
>
> [[if <condition>]]
> [[else]]
> [[endif]]
>
> or [[for 0, 3]] // do 4 time
> [[endfor]]
>
> but I am bogged down at the very first hurdle, which is just evaluating
> something like '2 == 2.".
>
> The text from the templates will be seen as a string, of course, but
> when I try to evaluate it as a bool,
> echo 'Evaluated condition: "' . (bool) $text . '"<br>';
>
> it always evaluates to true, even if my condition is '2 == 3'.
Strings and non-zero numbers evaluate to TRUE. You might consider the
eval() function.
eval('$condition = ($text)? true : false;');
if ($condtion) {
}
The usual caveats about not trusting visitor input apply.
I have a template processor I've been developing, that might work for
you. It's kind of a work-in-progress, but I've used it in production
environments. Email me if you'd like a copy.
--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
| |
| Baron Samedi 2006-05-21, 9:58 pm |
| $text = '2==2';
eval('$condition = ($text)? true : false;');
if ($condtion)
echo '<hr>True<hr>';
else
echo '<hr>False<hr>';
--------------------------------------------------------------------------------
False
--------------------------------------------------------------------------------
| |
| Alan Little 2006-05-22, 7:58 am |
| Carved in mystic runes upon the very living rock, the last words of
Baron Samedi of comp.lang.php make plain:
> $text = '2==2';
>
> eval('$condition = ($text)? true : false;');
>
> if ($condtion)
> echo '<hr>True<hr>';
> else
> echo '<hr>False<hr>';
>
>
> -----------------------------------------------------------------------
> False
> -----------------------------------------------------------------------
1) Variables in single quotes do not get evaluated. That expression
needs to be in double quotes, and then you need to escape the $
for $condition
2) You have a typo. You have $condition in your eval, and $condtion
in your if().
--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
| |
| Baron Samedi 2006-05-23, 3:59 am |
| 1) oops, I didn't know that about the single quotes. Thanks.
What did you mean by "escape the $ for $condition " ?
2) thanks for that.
it is still not working ("2==3" gicves true) , so I guess that I need
to "escape the $ in $condition".
Thanks for your help.
| |
|
| Baron Samedi wrote:
> 1) oops, I didn't know that about the single quotes. Thanks.
>
> What did you mean by "escape the $ for $condition " ?
In:
eval("$condition = ($text)? true : false;");
PHP will try to search for the variable "condition" to put in the eval
statement.
Escaping it like:
eval("\$condition = ($text)? true : false;");
will let PHP know it has to treat this part as text, so it will be evalled
as "$condition" and not the value of the (prehaps existing) variable
$condition. If it tries to replace it with the value of the variables
"condition", there is off course no way $condition gets set in this
statement.
Grtz,
--
Rik Wasmus
| |
| Jerry Stuckle 2006-05-23, 7:59 am |
| Rik wrote:
> Baron Samedi wrote:
>
>
>
> In:
> eval("$condition = ($text)? true : false;");
>
> PHP will try to search for the variable "condition" to put in the eval
> statement.
>
> Escaping it like:
> eval("\$condition = ($text)? true : false;");
>
> will let PHP know it has to treat this part as text, so it will be evalled
> as "$condition" and not the value of the (prehaps existing) variable
> $condition. If it tries to replace it with the value of the variables
> "condition", there is off course no way $condition gets set in this
> statement.
>
> Grtz,
How about :
$condition = (eval ($text)) ? true : false;
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
| |
| Alan Little 2006-05-23, 7:59 am |
| Carved in mystic runes upon the very living rock, the last words of
Jerry Stuckle of comp.lang.php make plain:
> Rik wrote:
>
> How about :
>
> $condition = (eval ($text)) ? true : false;
That gives a parse error in the eval, as 2==3 is not a syntactically
valid PHP statement.
--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
| |
| Baron Samedi 2006-05-24, 3:59 am |
| Isn't it a condition? Do I have to wrap it in brackets? Nope, taht
doesn't work.
Of course "(2==3")" is just an example. The problem is taht I don't
knwo what users will code ther. Could be anything, so long as it is a
condition.
Can you help me out with a code example? I would be very grateful.
| |
| Baron Samedi 2006-05-24, 3:59 am |
| $text = "(2==3)";
eval('\$condition = ($text)? true : false;');
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in
C:\graham\blog\extensions\_snippet_dev\~
scp.php(127) : eval()'d code on
line 1
Sorry, I still don't get it to work.
| |
| Alan Little 2006-05-24, 7:59 am |
| Carved in mystic runes upon the very living rock, the last words of Baron
Samedi of comp.lang.php make plain:
> $text = "(2==3)";
No parentheses.
> eval('\$condition = ($text)? true : false;');
Double qoutes, man. Double quotes.
> Warning: Unexpected character in input: '' (ASCII=92) state=1 in
> C:\graham\blog\extensions\_snippet_dev\~
scp.php(127) : eval()'d code on
> line 1
>
> Sorry, I still don't get it to work.
--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
| |
|
| Baron Samedi wrote:
> $text = "(2==3)";
>
> eval('\$condition = ($text)? true : false;');
>
>
> Warning: Unexpected character in input: '' (ASCII=92) state=1 in
> C:\graham\blog\extensions\_snippet_dev\~
scp.php(127) : eval()'d code
> on line 1
eval("\$condition = ($text)? true : false;"); or
eval('$condition = ('.$text.')? true : false;');
Grtz,
--
Rik Wasmus
| |
|
| Alan Little wrote:
> Carved in mystic runes upon the very living rock, the last words of
> Baron Samedi of comp.lang.php make plain:
>
>
> No parentheses.
>
>
> Double qoutes, man. Double quotes.
Yup,
eval("\$condition = ($text)? true : false;");
or
eval('$condition = ('.$text.')? true : false;');
For quoting-education:
http://nl2.php.net/manual/en/language.types.string.php
Grtz,
--
Rik Wasmus
| |
| the DtTvB 2006-05-28, 7:59 am |
| I made one
Source file: http://dttvb.yi.org/tparser.phps
There are still some problems about nesting. But it works quite well.
-- TESTS --
echo dtsparse('[[if 2==2]]this condition is true[[else]]this condition
is false[[endif]]');
// Outputs this condition is true
echo dtsparse('[[if 3==2]]this condition is true[[else]]this condition
is false[[endif]]');
// Outputs this condition is false
echo dtsparse('[[if 3>2]]this condition is true[[else]]this condition
is false[[endif]]');
// Outputs this condition is true
echo dtsparse('before if[[if 3==3]], this condition is true[[endif]],
after if');
// Outputs before if, this condition is true, after if
echo dtsparse('before if[[if 3==2]], this condition is true[[endif]],
after if');
// Outputs before if, after if
echo dtsparse('[[for 5,10]]aa [[endfor]]');
// Outputs aa aa aa aa aa aa
echo dtsparse('[[for 5 to 10]][[index]],[[endfor]]finish');
// Outputs 5,6,7,8,9,10,finish
Is that what you want? Sorry if it's not.
| |
| Baron Samedi 2006-05-28, 9:58 pm |
| This looks ecxcellent!! Is it GPL? If s, can you please mail me the
code & test script?
Thank you very much for your help!
| |
| the DtTvB 2006-05-29, 7:59 am |
| Maybe, I don't know exactly about GPL, but you don't have to give me
credits.
OK, I will e-mail you a code.
| |
| Baron Samedi 2006-05-29, 9:57 pm |
| Well, all that I mean is that our blog system is GPL. Anyone can take
the source code and change with it.
If I modify your code and release it, it will be under those conditions
- if you are ok with that.
It looks great, by the way. Thank you very much.
| |
| the DtTvB 2006-05-30, 3:58 am |
| Ok, you can du what you want to that code.
|
|
|
|
|