/    Sign up×
Community /Pin to ProfileBookmark

Using a veriable as an operator

Hi,

I am using an if statement but the operator is a variable, but php doesn’t seam to like is and i get this error msg: –

Parse error: syntax error, unexpected T_VARIABLE in xxx on line 616

The code is this: –

[code=php]
//Form: –
<select name=”whereOperator2″>
<option value=”==”>Equal to</option>
<option value=”>”>Greater than</option>
<option value=”<“>Less than</option>
</select>

//Code Line 616
if ($data[$whereColumn] $whereOperator $whereClause)
[/code]

Any gd ideas to get around it other than creating another if statement of the 3 variables in the select i.e

[code=php]
if ($whereOperator == ‘==’) {
if ($data[$whereColumn] == $whereClause) {
//code
}
} else if ($whereOperator == ‘>’) {
if ($data[$whereColumn] > $whereClause) {
// and so on
[/code]

Thanks
Adam

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@aznchong91Aug 19.2005 — I don't THINK so... I think you have to do it the manual way. ?
Copy linkTweet thisAlerts:
@NogDogAug 19.2005 — [code=php]
exec("$result = ($data[$whereColumn] $whereOperator $whereClause);");
if($result)
{
# comparison returned TRUE
}
else
{
# comparison returned FALSE
}
[/code]
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — thanks for all ya help,

may i ask nog dog how long have you been doing php, you always seem to impress me, just wondered how long it took to get to your level.

Regards

Adam
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — ok i have tested it and a small problem lol, i get the dos screen flash up the amount of time the line of code is activated as its in a while function :s

Any other ideas lol.

Thanks

Adam
Copy linkTweet thisAlerts:
@artoAug 19.2005 — I think NogDog meant [B]eval[/B], not [B]exec[/B].

Arto
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — Ok i have come up with this now, although there is a small problem.

It only seems to recognise the first if, so if i selct the $whereOperator to == or > for some reason it jumps straight to $clause=true; the < sign works fine and does its job perfect.

Any ideas or have a better way to do it

[code=php]
if ($whereOperator == '>') {
$Clause = $data[$whereColumn] > $whereClause;
} else if ($whereOperator == '==') {
$Clause = $data[$whereColumn] == $whereClause;
} else if ($whereOperator == '<') {
$Clause = $data[$whereColumn] < $whereClause;
} else {
$Clause = true;
}

if ($Clause) {
//code
[/code]


Thanks

Adam
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — thanks arto, and it again works fine on > but on == or < still doesn;t work properly??

Thanks

adam
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — haha no problem job done, stupid me only put the = in the select as one equal and not two ?

Well it ended up that eval worked ? thanks for all ya help

Adam
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — sorry to be a pain lol but....

I am trying to now get it to do this query but i get an error

[code=php]
eval("$eval_result = (($data[$whereColumn] $whereOperator $whereClause) $whereMulti ($data[$whereColumn2] $whereOperator2 $whereClause2));");

//whereMulti is : -
<input type="radio" name="whereMulti" value="&&" checked>And<input type="radio" name="sqlOperation" value="||" >OR
[/code]


Sorry to be a pain :s

The error is : - Parse error: syntax error, unexpected T_VARIABLE in location/file.php(619) : eval()'d code on line 1

Many Thanks

Adam
Copy linkTweet thisAlerts:
@NogDogAug 19.2005 — I think NogDog meant [B]eval[/B], not [B]exec[/B].

Arto[/QUOTE]

Oops! Yes, eval() it is.
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — any idea's on the new line :s

Thanks

Adam
Copy linkTweet thisAlerts:
@NogDogAug 19.2005 — sorry to be a pain lol but....

I am trying to now get it to do this query but i get an error

[code=php]
eval("$eval_result = (($data[$whereColumn] $whereOperator $whereClause) $whereMulti ($data[$whereColumn2] $whereOperator2 $whereClause2));");

//whereMulti is : -
<input type="radio" name="whereMulti" value="&&" checked>And<input type="radio" name="sqlOperation" value="||" >OR
[/code]


Sorry to be a pain :s

The error is : - Parse error: syntax error, unexpected T_VARIABLE in location/file.php(619) : eval()'d code on line 1

Many Thanks

Adam[/QUOTE]


Remove the backslash before $wheremulti - you want it to be interpolated into the desired logical operator before the eval() expression is executed.
Copy linkTweet thisAlerts:
@NogDogAug 19.2005 — thanks for all ya help,

may i ask nog dog how long have you been doing php, you always seem to impress me, just wondered how long it took to get to your level.

Regards

Adam[/QUOTE]

I've done stuff off-and-on with PHP for the last 2-3 years, really started learning it in the last year or less. But I've been in the IT business for over 15 years now and have dabbled in a lot of UNIX shell scripting and a bit of Perl before ever touching PHP. As PHP has a lot of similarities to them, I guess I picked it up relatively easily as compared to coming into it with no such background.
Copy linkTweet thisAlerts:
@k0r54authorAug 19.2005 — thank for all your help nogdog, its been rly useful.

I have been doing php for around a year or so now, but before i started i barly new css and only basic html i have progressed steadily but im getting it with with alot of help from webdeveloper.com ?.

Thanks all and i hope this post will come in useful for some1 else.

Adam
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...