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? 17 1617
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/
$text = '2==2';
eval('$condition = ($text)? true : false;');
if ($condtion)
echo '<hr>True<hr>';
else
echo '<hr>False<hr>';
--------------------------------------------------------------------------------
False
--------------------------------------------------------------------------------
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/
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
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
==================
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/
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.
$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.
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/
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
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
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.
This looks ecxcellent!! Is it GPL? If s, can you please mail me the
code & test script?
Thank you very much for your help!
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.
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.
Ok, you can du what you want to that code. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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.
|
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:
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |