# PHP - arithmetic operations using strings as operators

Viewed   155 times

I have a string variable, `\$operation`, that can have values like `+` or `-` and two integer variables `\$initial` and `\$unit`.

So to echo the result of the arithmetic operation between them

I have to use something like

`````` if(\$operation == '+') echo (\$initial + \$unit);
if(\$operation == '-') echo (\$initial - \$unit);
``````

Is there a way I can do this without the IF?

1

trickery with math:

``````echo \$initial + ((\$operation == '-') ? -1 : 1) * \$unit;
``````

only using addition, but cheating with multiplying by a negative... :)

Thursday, November 3, 2022

2

This is happening because the concatenation operator has a higher precedence than the addition or subtraction operators, but multiplication and division have a higher precedence then concatenation.

So, what you're really executing is this:

``````echo ("\$a + \$b  = " . \$a) + \$b;
echo ("\$a - \$b  = " . \$a) - \$b;
``````

In the first case, that gets turned into this:

``````"1 + 2 = 1" + \$b
``````

Which PHP tries to convert `"1 + 2 = 1"` into a number (because of type juggling) and gets 1, turning the expression into:

``````1 + 2
``````

Which is why you get 3. The same logic can be applied to the subtraction condition.

Instead, if you put parenthesis around the calculations, you'll get the desired output.

``````echo "\$a + \$b  = " . (\$a + \$b);
echo "\$a - \$b  = " . (\$a - \$b);
``````
Monday, September 5, 2022

2

Dont need to do any conversion first of all. So you can get rid of `\$code = (string)\$code . '';` and all the strval() calls. You \$message can be set as thus:

`\$message = "Winner information follows:rnEmail: ".\$email."rnConfirmation Code: ".\$code;`

Also, it doesnt look like you are including the code in your second email. So the whole thing changed would look like this:

``````<?php
\$myFile = "winners.txt";
\$fh = fopen(\$myFile, 'a') or die("can't open file");
\$email = \$_POST['email'];
\$code = \$_POST['code'];
\$message = "Winner information follows:rnEmail: ".\$email."rnConfirmation Code: ".\$code;
fwrite(\$fh, \$email);
fwrite(\$fh, \$code);
mail("loganhsnow@gmail.com", "Winning Notice ST", \$message);
mail(\$email, "Winning Notice CT", 'Congrats, you won a free amazon gift card at logansnow.tk. If the following confirmation code matches the one in our records you will receive your reward. The code follows: '.\$code);
fclose(\$fh);
?>
``````

One other thing to note is that you might want to put \$email through some sanitizaiton checks to verify it is a valid email address.

Friday, December 23, 2022

4

`=` is parsed as right-associative, but order of evaluation is left-to-right.

So: The statement is parsed as `a[i] = (i = 9)`. However, the expression `i` in `a[i]` is evaluated before the right hand side (`i = 9`), when `i` is still `0`.

It's the equivalent of something like:

``````int[] #0 = a;
int #1 = i;
int #2 = 9;
i = #2;
#0[#1] = #2;
``````
Thursday, August 18, 2022

5

Try sketching an automata that detects expressions. After that the simplest way to implement an automata would be a switch..case with nested if..else. I think it would be far easier than parsing the string the way you are right now.

Edit--

This is a very simple example, only for the sake of demonstration. suppose I want to detect expressions in the form of var1 + var2, the automata would look like this: Image

Implementaion looks like this:

``````done = false;
state = start;
while(!done)
{
switch(state)
{
case start:
if(expression[i] > 'a' && expression[i] < 'z')
state = start;
else if(expression[i] == '+')
{
// seen first operand and waitng for second
// so we switch state
}
break;
if(expression[i] > 'a' && expression[i] < 'z')
else
done = true;
break;
}
}
``````

Like I said this is very simple, your automata would be more complex with many more states and transitions. I've also not included actions here, but you could do actual addition after second operand is read which is after done = true;

Thursday, September 15, 2022