468,315 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

I need a mini "logic interpreter"

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?

May 19 '06 #1
17 1484
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/
May 19 '06 #2
$text = '2==2';

eval('$condition = ($text)? true : false;');

if ($condtion)
echo '<hr>True<hr>';
else
echo '<hr>False<hr>';
--------------------------------------------------------------------------------
False
--------------------------------------------------------------------------------

May 22 '06 #3
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/
May 22 '06 #4
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.

May 23 '06 #5
Rik
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
May 23 '06 #6
Rik wrote:
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,


How about:

$condition = (eval ($text)) ? true : false;

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 23 '06 #7
Carved in mystic runes upon the very living rock, the last words of
Jerry Stuckle of comp.lang.php make plain:
Rik wrote:
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


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/
May 23 '06 #8
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.

May 24 '06 #9
$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(12 7) : eval()'d code on
line 1

Sorry, I still don't get it to work.

May 24 '06 #10
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(12 7) : 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/
May 24 '06 #11
Rik
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(12 7) : eval()'d code
on line 1


eval("\$condition = ($text)? true : false;"); or
eval('$condition = ('.$text.')? true : false;');

Grtz,
--
Rik Wasmus
May 25 '06 #12
Rik
Alan Little wrote:
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.


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
May 25 '06 #13
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.

May 28 '06 #14
This looks ecxcellent!! Is it GPL? If s, can you please mail me the
code & test script?

Thank you very much for your help!

May 29 '06 #15
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.

May 29 '06 #16
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.

May 30 '06 #17
Ok, you can du what you want to that code.

May 30 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.