473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

possible to forward if-expression in variable?

Hi Folks,
I would like to forward an expression to an if-statement via a variable.
Like:
$a=10;
$b=20;

$expr="$a > $b";#this should be my if-expression

if ($expr) do something;#test should be if($a > $b)

Of course this does not work, as $expr is simply determined as being true.
Is there any chance to get this working?
Thanks in advance,
Chris
Jul 17 '05 #1
6 1914
Chris wrote:
I would like to forward an expression to an if-statement via a variable.
Like:
$a=10;
$b=20;

$expr="$a > $b";#this should be my if-expression


Try this:

<?php
$a = 5;
$b = 10;

$expr1 = ($a > $b);
$expr2 = ($a < $b);

if ($expr1) echo 'expr1 is true';
else echo 'expr1 is false';

echo "<pre>\n\n\n</pre>";

if ($expr2) echo 'expr2 is true';
else echo 'expr2 is false';
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Hi Chris,

In addition to Pedro's posting:
I find it usefull to use the following construct in such situations:

$total = (($num<100) ? 12 : 50) * 1.5;

The first expression is evaluated, if true it returns 12, if not 50.
The result is multiplied by 1.5

You can nest them as many times as needed.

Regards,
Erwin Moller
Jul 17 '05 #3
Hi Pedro, Hi Erwin,
thanks for the proposals. But I might have not been clear enough.
You hand over the expression as a TRUE/FALSE result. Of course this
works (with one exception - if expression is FALSE you won't be able
to have an else statement - at least in my hands it cancels the else
fork).

What I wanted is a little different (and more complicated). My
if-expressions should be stored as a string, because I fetch them from
a database. Now I want to use them in a PHP-context. [Reason for this
is that I check plausabilities in forms, depending on the result of
selected formfields - the according plausibilities are defined in the
database]

So, is there any chance to convert a string
"$a > $b"
into an expression/result
TRUE/FALSE.
??

Sorry for having been inprecise in my forst posting...
Maybe there is an answer.
Cheers,
Chris
In addition to Pedro's posting:
I find it usefull to use the following construct in such situations:

$total = (($num<100) ? 12 : 50) * 1.5;

Try this:

<Pedro snip>
<?php
$a = 5;
$b = 10;

$expr1 = ($a > $b);
$expr2 = ($a < $b);

if ($expr1) echo 'expr1 is true';
else echo 'expr1 is false';

echo "<pre>\n\n\n</pre>";

if ($expr2) echo 'expr2 is true';
else echo 'expr2 is false';
?>
</Pedro snip>
Jul 17 '05 #4
lookup eval() from www.php.net

greetz,

Michel

"Chris" <ch*******@1click2art.de> wrote in message
news:72**************************@posting.google.c om...
Hi Pedro, Hi Erwin,
thanks for the proposals. But I might have not been clear enough.
You hand over the expression as a TRUE/FALSE result. Of course this
works (with one exception - if expression is FALSE you won't be able
to have an else statement - at least in my hands it cancels the else
fork).

What I wanted is a little different (and more complicated). My
if-expressions should be stored as a string, because I fetch them from
a database. Now I want to use them in a PHP-context. [Reason for this
is that I check plausabilities in forms, depending on the result of
selected formfields - the according plausibilities are defined in the
database]

So, is there any chance to convert a string
"$a > $b"
into an expression/result
TRUE/FALSE.
??

Sorry for having been inprecise in my forst posting...
Maybe there is an answer.
Cheers,
Chris
In addition to Pedro's posting:
I find it usefull to use the following construct in such situations:

$total = (($num<100) ? 12 : 50) * 1.5;

Try this:

<Pedro snip>
<?php
$a = 5;
$b = 10;

$expr1 = ($a > $b);
$expr2 = ($a < $b);

if ($expr1) echo 'expr1 is true';
else echo 'expr1 is false';

echo "<pre>\n\n\n</pre>";

if ($expr2) echo 'expr2 is true';
else echo 'expr2 is false';
?>
</Pedro snip>

Jul 17 '05 #5
Chris,

?Can't you use:

if ( eval("return $expression;") )
do something

Greetings,

Henk.

Chris wrote:
Hi Pedro, Hi Erwin,
thanks for the proposals. But I might have not been clear enough.
You hand over the expression as a TRUE/FALSE result. Of course this
works (with one exception - if expression is FALSE you won't be able
to have an else statement - at least in my hands it cancels the else
fork).

What I wanted is a little different (and more complicated). My
if-expressions should be stored as a string, because I fetch them from
a database. Now I want to use them in a PHP-context. [Reason for this
is that I check plausabilities in forms, depending on the result of
selected formfields - the according plausibilities are defined in the
database]


Jul 17 '05 #6
Thanks Michel and Henk!

I hadn't though about eval() - it's that easy.

Cheers,
Chris
Henk Verhoeven <ne**@metaclassREMOVE-THIS.nl> wrote in message news:<c1*********@news4.tilbu1.nb.home.nl>...
Chris,

?Can't you use:

if ( eval("return $expression;") )
do something

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
1
by: qazmlp | last post by:
Is it a good style to re-forward declare the types, which were already forward declared in base header file, in derived class header file? // base.h class addrClass; class base { public:...
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
2
by: Marcin Kalicinski | last post by:
Hi All, In some headers meant to work with both C and C++ the following is often found: typedef struct tagMyStruct { /*some members*/ } MyStruct; Can I forward-declare MyStruct somehow? ...
11
by: Randy Yates | last post by:
I'm having a problem with forward references. For example, class DATE; class MYCLASS; class MYCLASS { public:
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
8
by: Heiner | last post by:
Hi! The following snippet class B; class A { public:
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
7
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
4
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 I have a DB2 online DB backup that was done w/ the INCLUDE LOGS option. I am interested in restoring that backup, and rolling forward ONLY the logs contained in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.