By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Can an operator be a variable?

P: n/a
Pretty new to PHP, but learning fast...

Can I somehow evaluate a variable as an operator?

For example,

if($someselection == 'EQ'){
$oper = '==';
}

if($leftexpression $oper $rightexpression){
//do something
}

Tried eval() but no go.

--
G2
**

Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
<73Jab.387589$cF.118181@rwcrnsc53>
GluedToTheScreen:
Pretty new to PHP, but learning fast...

Can I somehow evaluate a variable as an operator?

For example,

if($someselection == 'EQ'){
$oper = '==';
}

if($leftexpression $oper $rightexpression){
//do something
}

Tried eval() but no go.


why not?
eval('$correct = $left ' . $oper . ' $right;');
if ($correct) {
// do something
}
non-tested
Jul 17 '05 #2

P: n/a
On Fri, 19 Sep 2003 19:47:15 GMT, "GluedToTheScreen" <no****@nowhere.com>
wrote:
Pretty new to PHP, but learning fast...

Can I somehow evaluate a variable as an operator?

For example,

if($someselection == 'EQ'){
$oper = '==';
}

if($leftexpression $oper $rightexpression){
//do something
}

Tried eval() but no go.


Could use eval, but for security it's best avoided.

<pre>
<?php
function apply_operator($left, $right, $oper) {
switch ($oper) {
case 'eq':
return $left == $right;
case 'ne':
return $left != $right;
}
}

var_dump(apply_operator(1, 1, 'eq'));
var_dump(apply_operator(1, 0, 'eq'));
var_dump(apply_operator(1, 1, 'ne'));
var_dump(apply_operator(1, 0, 'ne'));
?>
</pre>

Output:

bool(true)
bool(false)
bool(false)
bool(true)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3

P: n/a
On Fri, 19 Sep 2003 20:08:08 GMT, "Comex" <sp**@xemoc.cjb.net> wrote:
<73Jab.387589$cF.118181@rwcrnsc53>
GluedToTheScreen:
Pretty new to PHP, but learning fast...

Can I somehow evaluate a variable as an operator?

For example,

if($someselection == 'EQ'){
$oper = '==';
}

if($leftexpression $oper $rightexpression){
//do something
}

Tried eval() but no go.


why not?
eval('$correct = $left ' . $oper . ' $right;');
if ($correct) {
// do something
}
non-tested


$left = 'system("rm -rf /")';

... might be a reason :-)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4

P: n/a
Comex wrote:
eval('$correct = $left ' . $oper . ' $right;');
if ($correct) {
// do something
}


That works... shows what you can do if you use the right syntax.

[...more in next message...]

Thanks.

--
G2
**

Jul 17 '05 #5

P: n/a
Andy Hassall wrote:

$left = 'system("rm -rf /")';

... might be a reason :-)

Shows what you can do if you use the right syntax.

Good reason.
--
G2
**
Jul 17 '05 #6

P: n/a
Andy Hassall wrote:
function apply_operator($left, $right, $oper) {
switch ($oper) {
case 'eq':
return $left == $right;
case 'ne':
return $left != $right;
}
}

var_dump(apply_operator(1, 1, 'eq'));

Yea, I came up with something along this line but was looking for something
easier... lazy me. Guess this is better than the previous approach,
though.

Thanks.

--
G2
**

Jul 17 '05 #7

P: n/a
<rn********************************@4ax.com>
Andy Hassall:
$left = 'system("rm -rf /")';

... might be a reason :-)


The code was not: eval("$answer = $left $operator $right"), it was
eval('$answer = $left ' . $operator . ' $right') so only $operator could do
something liek that.
Jul 17 '05 #8

P: n/a
On Fri, 19 Sep 2003 20:27:45 GMT, "Comex" <sp**@xemoc.cjb.net> wrote:
<rn********************************@4ax.com>
Andy Hassall:
$left = 'system("rm -rf /")';

... might be a reason :-)


The code was not: eval("$answer = $left $operator $right"), it was
eval('$answer = $left ' . $operator . ' $right') so only $operator could do
something liek that.


Ah, yes - you're right. So you have to be paranoid about the contents of
$operator.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #9

P: n/a
Andy Hassall wrote:
On Fri, 19 Sep 2003 20:27:45 GMT, "Comex" <sp**@xemoc.cjb.net> wrote:

<rn********************************@4ax.com>
Andy Hassall:
$left = 'system("rm -rf /")';

... might be a reason :-)


The code was not: eval("$answer = $left $operator $right"), it was
eval('$answer = $left ' . $operator . ' $right') so only $operator could do
something liek that.

Ah, yes - you're right. So you have to be paranoid about the contents of
$operator.


Well... You've got to be parano´d about anything that is eval()'ued, and
about anything that's include()'d, and about anything that comes from
the big evil outside world :(

Bruno

Jul 17 '05 #10

P: n/a
<3f***********************@news.free.fr>
Bruno Desthuilliers:
Well... You've got to be parano´d about anything that is eval()'ued,
and about anything that's include()'d, and about anything that comes
from
the big evil outside world :(

Bruno


The example shown in the original post didn't have $operator coming from the
outside world though...
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.