473,378 Members | 1,314 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,378 software developers and data experts.

Can an operator be a variable?

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

Similar topics

5
by: ··········· sylvain | last post by:
hello, what does the operator |= mean? I saw it in a script : $output = "png"; $style = BCS_ALIGN_CENTER; $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
4
by: user | last post by:
Hi, Is it possible to override assignment, the way that '+' can be overridden for example? Thanks, Toby
5
by: Avinash | last post by:
Hi, Why Overloaded operator cannot be a friend ? Thanking You. Avinash
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
14
by: Joost Ronkes Agerbeek | last post by:
Should I remove const from a private member just for the sake of the assignment operator? I have a class that looks something like this. class Foo { public: Foo(const std::string&...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.