-4

I have a function i.e

function cdec($num) {
for ($n = 0 ; $n < strlen($num) ; $n++) {
$temp = $num[$n];
$dec = $dec + $temp*pow(2 , strlen($num) – $n – 1);
}
return $dec;
}

When I am going to run my code then it is showing me this error

Parse error: syntax error, unexpected '–' (T_STRING)

I don't understand where is my fault. It is showing error in this line

$dec = $dec + $temp*pow(2 , strlen($num) – $n – 1);

Indresh Tayal
  • 276
  • 1
  • 11
  • 2
    What language? Please tag your question with it – Ayush Jan 12 '17 at 07:35
  • 2
    minus (`-`) and hyphen (`–`) are different characters – P_95 Jan 12 '17 at 07:37
  • I have make it like $dec = $dec + $temp*pow(2 , strlen($num) ( - ) $n ( – ) 1); still showing me another errror Parse error: syntax error, unexpected '(' in – Indresh Tayal Jan 12 '17 at 07:40
  • @IndreshTayal Yeah, but you need to change that back to a `-` character. – MC Emperor Jan 12 '17 at 07:41
  • I am working in php language – Indresh Tayal Jan 12 '17 at 07:41
  • 1
    @IndreshTayal Please use the **edit** link below your question and add the [tag:php] tag. – MC Emperor Jan 12 '17 at 07:42
  • Hi Indresh In regards to your previous comment, Arithmetic Operators do not work inside Parenthesis (Brackets) alone, they need data to operate on. What I mean by this is: 1 ( + ) 1; will not work. (1 + 1 ); will work. 1 + 1; will work; The issue, as pointed out by Naveed Ramzan, is that you are not using minus signs (-) but hyphens instead, which are for use in literacy. While I do not come with the answer myself, it was important to correct your use of brackets around Arithmetic Operators. – Michael Thompson Jan 12 '17 at 08:01

2 Answers2

0

Try this

function cdec($num) {
    $dec = '';
    for ($n = 0 ; $n < strlen($num) ; $n++) {
        $temp = $num[$n];
        $dec = $dec + $temp*pow(2 , strlen($num) - $n - 1);
    }
    return $dec;
}

Both Minus signs were in fact Hyphen signs.

Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
-1

Use parenthesis to separate math operations:

function cdec($num) {
    $dec = 0;
    for ($n = 0 ; $n < strlen($num) ; $n++) {
        $temp = $num[$n];
        $dec = $dec + ($temp*(pow(2 , ((strlen($num) - $n) - 1))));
    }
    return $dec;
}
oliver
  • 51
  • 1
  • 6