Why doesn’t that code work?

Asked

Viewed 119 times

-5

I have that code:

function multiply(a, b) {
    return a * b
}

I wonder why it doesn’t work?

Error returned:

PHP Parse error: syntax error, Unexpected ',', expecting variable (T_VARIABLE) in /home/codewarrior/run.php on line 5

  • 1

    Dude, is this code actually javascript or php? are you sure it’s php? because it seems to me to be javascript by way of it, because variable PHP must contain $ at the beginning

  • @Paulo Roberto The error must be php

  • 1

    "Problems or real questions you found" because this is on the tour so if there can be no error?

2 answers

6

I believe you took the code from here.

What happens is that some basic things are missing like the use of $ to indicate variables passed as parameters:

Variables in PHP are represented by a dollar sign ($) followed by the variable name.1

And it’s also missing ; (semicolon) at the end of the second line of your program:

PHP requires instructions to be completed with a semicolon at the end of each command.1

Finally, you can use your code this way:

function multiply($a, $b) {
  return $a * $b;
}
  • Uzu, the correction of the code would close the excellent answer!

  • 1

    @Bsalvo, I made an edition as suggested.

1

'Cause you forgot to put ; at the end of the line.

function multiply($a, $b) {
  return $a * $b;
}

Initially I believed that the problem was in Javascript because its variables were without the $ necessary for PHP to recognize as a variable, but in fact its function is in PHP as follows the tag, so I believe you got confused and were programming Javascript in PHP. (I’ve been there before)

So keep in mind that to declare and use variables in PHP you have to use $ at the beginning of each.

  • Missed use $ too, is required in PHP.

  • @Uzumakiartanis boy I SWORE it was in javascript, I believe that the OP also thought, by mistake.

  • It’s not PHP anyway, the syntax is very similar, also note the error PHP Parser error

  • Yes I saw here, only maybe he got confused and was programming js in php, as I did a few times

Browser other questions tagged

You are not signed in. Login or sign up in order to post.