0

I want to store a plus sign (or any other sign) in a PHP variable and make arithmetic operation. I tried to store sign plus in variable $c. But PHP it's not accepting it.

$a = 3; 
$b = 5;
$с = "+"; 

But when I echo it,

echo $a. $c. $b;

it gives me the result 35.

I tried enothoer one way, but it doesn't work:

if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}

The result is -2. How get result $a + $b?

AndreyKlym
  • 21
  • 3
  • Have you tried to check your code for problems? If I execute it, I receive an error, see https://3v4l.org/8SrV1 – Nico Haase Jun 28 '21 at 11:37
  • 4
    ....because you've used an cyrillic `c` in the third line, and a latin `c` in the comparison – Nico Haase Jun 28 '21 at 11:38
  • 1
    Was just about to say, the two `c` you have are different ASCII codes, hence they are different variables altogether. The one on line 3 is 1089 and the one in your `if` condition is 99 (which is correct). – Qirel Jun 28 '21 at 11:39
  • @NicoHaase is correct. See working one here: http://sandbox.onlinephpfunctions.com/code/6432701be253715baee3b390a8020e60352355a1 – drodil Jun 28 '21 at 11:40
  • After seeing the edit: what else did you assume on concatenating three strings? – Nico Haase Jun 28 '21 at 12:02

1 Answers1

2

You're using a combination of the Cyrillic and latin letter c in your code.

If I paste your code on this site to show each unicode char, the first c is an \u0441 (Cyrillic), how ever, the c inside the if shows 0x63 (latin), so PHP is unable to match those and trows:

PHP Notice: Undefined variable: c

Fixed:

<?php

$a = 3; 
$b = 5;
$c = "+"; 

if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}

Try it online!

0stone0
  • 34,288
  • 4
  • 39
  • 64