Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable as an operator

Tags:

php

So I have something like the following:

$a = 3; $b = 4; $c = 5; $d = 6; 

and I run a comparison like

if($a>$b || $c>$d) { echo 'yes'; }; 

That all works just fine. Is it possible to use a variable in place of the operator? Something like:

$e = ||; 

Which I could then use as

if($a>$b $e $c>$d) { echo 'yes'; }; 
like image 299
Jason Avatar asked Feb 14 '10 23:02

Jason


People also ask

How are operators used with variables?

Assignment Operators are used to assign a value to a property or variable. Assignment Operators can be numeric, date, system, time, or text. Comparison Operators are used to perform comparisons. Concatenation Operators are used to combine strings.

Can I assign operator to variable JavaScript?

JavaScript Assignment OperatorsAssignment operators assign values to JavaScript variables. The addition assignment operator ( += ) adds a value to a variable.

What is the difference between variable and operator?

This chapter describes how to write statements using variables, which store values like numbers and words, and operators, which are symbols that perform a computation.

When the operator is used before the variable is called?

The increment (++) and decrement (--) operators are unusual in that you can place them either before or after the variable. Placing the operator symbol before the variable is called the prefix form of the operator, and using the operator symbol after the variable is called the postfix form.


1 Answers

No, that syntax isn't available. The best you could do would be an eval(), which would not be recommended, especially if the $e came from user input (ie, a form), or a switch statement with each operator as a case

switch($e) {     case "||":         if($a>$b || $c>$d)             echo 'yes';     break; } 
like image 135
Kristopher Avatar answered Oct 12 '22 12:10

Kristopher