2 Hint at operators as functions
source | link

$test = $number1 $operator1 $number2 $operator2 $number3;

Most languages implementations have a step where a parser analyses your code and builds a tree from it. So for example the expression 5 + 5 * 8 would get parsed as

  +
 / \
5   *
   / \
  8   8

thanks to the compiler's knowledge about precedences. If you fed it variables in place of operators, it would not know the proper order of operations before running the code. For most implementations that would be a serious problem, so most languages don't allow that.

You could of course conceive a language where the parser just parses the above as a sequence of expressions and operators, to be sorted and evaluated at runtime. Presumably there just isn't much application for this.

Many scripting languages allow the evaluation of arbitrary expressions (or at least arbitrary arithmetic expressions as in the case of expr) at runtime. There you could just combine your numbers and operators into a single expression and let the language evaluate that. In PHP (and many others) that function is called eval.

$test = eval("$number1 $operator1 $number2 $operator2 $number3");

There are also languages which allow for code generation at compile time. The mixin expression in D comes to my mind, where I believe you could write something like

test = mixin("number1 " + operator1 + " number2 " + operator2 + "number3");

Here operator1 and operator2 would have to be string constants which are known at compile time, e.g. template parameters. number1, number2 and number3 were left as normal runtime variables.

Other answers already discussed the various ways how an operator and a function are more or less the same thing, depending on the language. But usually there is a syntactic difference between a builtin infix operator symbol like + and a named callable like operator1. I'll leave the details to those other answers.

$test = $number1 $operator1 $number2 $operator2 $number3;

Most languages implementations have a step where a parser analyses your code and builds a tree from it. So for example the expression 5 + 5 * 8 would get parsed as

  +
 / \
5   *
   / \
  8   8

thanks to the compiler's knowledge about precedences. If you fed it variables in place of operators, it would not know the proper order of operations before running the code. For most implementations that would be a serious problem, so most languages don't allow that.

You could of course conceive a language where the parser just parses the above as a sequence of expressions and operators, to be sorted and evaluated at runtime. Presumably there just isn't much application for this.

Many scripting languages allow the evaluation of arbitrary expressions (or at least arbitrary arithmetic expressions as in the case of expr) at runtime. There you could just combine your numbers and operators into a single expression and let the language evaluate that. In PHP (and many others) that function is called eval.

$test = eval("$number1 $operator1 $number2 $operator2 $number3");

There are also languages which allow for code generation at compile time. The mixin expression in D comes to my mind, where I believe you could write something like

test = mixin("number1 " + operator1 + " number2 " + operator2 + "number3");

Here operator1 and operator2 would have to be string constants which are known at compile time, e.g. template parameters. number1, number2 and number3 were left as normal runtime variables.

$test = $number1 $operator1 $number2 $operator2 $number3;

Most languages implementations have a step where a parser analyses your code and builds a tree from it. So for example the expression 5 + 5 * 8 would get parsed as

  +
 / \
5   *
   / \
  8   8

thanks to the compiler's knowledge about precedences. If you fed it variables in place of operators, it would not know the proper order of operations before running the code. For most implementations that would be a serious problem, so most languages don't allow that.

You could of course conceive a language where the parser just parses the above as a sequence of expressions and operators, to be sorted and evaluated at runtime. Presumably there just isn't much application for this.

Many scripting languages allow the evaluation of arbitrary expressions (or at least arbitrary arithmetic expressions as in the case of expr) at runtime. There you could just combine your numbers and operators into a single expression and let the language evaluate that. In PHP (and many others) that function is called eval.

$test = eval("$number1 $operator1 $number2 $operator2 $number3");

There are also languages which allow for code generation at compile time. The mixin expression in D comes to my mind, where I believe you could write something like

test = mixin("number1 " + operator1 + " number2 " + operator2 + "number3");

Here operator1 and operator2 would have to be string constants which are known at compile time, e.g. template parameters. number1, number2 and number3 were left as normal runtime variables.

Other answers already discussed the various ways how an operator and a function are more or less the same thing, depending on the language. But usually there is a syntactic difference between a builtin infix operator symbol like + and a named callable like operator1. I'll leave the details to those other answers.

1
source | link

$test = $number1 $operator1 $number2 $operator2 $number3;

Most languages implementations have a step where a parser analyses your code and builds a tree from it. So for example the expression 5 + 5 * 8 would get parsed as

  +
 / \
5   *
   / \
  8   8

thanks to the compiler's knowledge about precedences. If you fed it variables in place of operators, it would not know the proper order of operations before running the code. For most implementations that would be a serious problem, so most languages don't allow that.

You could of course conceive a language where the parser just parses the above as a sequence of expressions and operators, to be sorted and evaluated at runtime. Presumably there just isn't much application for this.

Many scripting languages allow the evaluation of arbitrary expressions (or at least arbitrary arithmetic expressions as in the case of expr) at runtime. There you could just combine your numbers and operators into a single expression and let the language evaluate that. In PHP (and many others) that function is called eval.

$test = eval("$number1 $operator1 $number2 $operator2 $number3");

There are also languages which allow for code generation at compile time. The mixin expression in D comes to my mind, where I believe you could write something like

test = mixin("number1 " + operator1 + " number2 " + operator2 + "number3");

Here operator1 and operator2 would have to be string constants which are known at compile time, e.g. template parameters. number1, number2 and number3 were left as normal runtime variables.