472,805 Members | 1,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 1596
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: William Morris | last post by:
Apologies in advance for the cross-post. Not sure if this is better handled in ASP code or TransactSQL. Windows2000 Server SQL 2000 tblPeople contactid int firstname varchar(25) lastname...
2
by: jvvhie | last post by:
Hello, I am writing a pure-Python game engine that interprets the code of game objects within the same process with the exec statement. My main goal is to make as much power available as possible...
0
by: tomerdr | last post by:
Hi, I implemented the "Interpreter" pattern for a simple calculator whicu support +\*- I want to ask what is a good way to construct the expression tree? Thanks in advance.
16
by: lawrence k | last post by:
I've never before written a PHP script to run on my home Ubuntu machine (though I've written dozens of PHP cron jobs for my web server), so I thought I'd start off with a very simple test: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.