-3

While working on a recent project, I began wondering when somebody may use JavaScript vs. PHP for a generic function. Take this basic function (in JS) as an example, which simply returns whether or not a number falls within a particular range:

function range(num, var1, var2) {
    if ((num >= var1) && (num <= var2)) {
        return true;
    }

    else {
        return false;
    }
}

For something that doesn't query a database, nor is it information that should be — or needs to be — indexed for SEO (I know by default JavaScript will not be indexed), then my inference would be that JavaScript would be sufficient. But at the same time, PHP could be as well.

Basically, if the ONLY point of the application were a simple function like above (not that I can see a reason for that, but I digress...), then which langauge would be better to write this in? JavaScript or PHP?

Would love any insight as to which would be the best method to use and why. I recognize there is no right or wrong answer necessarily, but would like to hear arguments for or against one over the other.

Thanks!

Community
  • 1
  • 1
zrosen
  • 21
  • 1
  • 5
  • 3
    PHP is executed on the server and output to the browser. JS is output from the server to the browser and then executed in the browser. Where do you want it to execute. – AbraCadaver Jul 30 '14 at 20:49
  • Hey AbraCadaver! Correct - but both can do the above function, as an example. Seemingly, doing it either client-side or server-side would have the same effect. – zrosen Jul 30 '14 at 20:51
  • For simple functions like this it's not much of a difference indeed. However, if it's necessary to run the function after the page has loaded, Javascript wins. If it's a more complex function that you rather run server-side than on the client's computer, PHP wins. – Oscar Jul 30 '14 at 20:53
  • 1
    in the above example some people would not want the page to reload, but others would care if people with js off or bots hit the page - so it all DEPENDS –  Jul 30 '14 at 20:53
  • All good thoughts - thanks for your insights! If whoever downvoted has a reason, I would love to hear it as a newer Stackoverflow user... – zrosen Jul 30 '14 at 20:56
  • Zrosen: StackOverflow is intended primarily/exclusively for questions that have definitive, provable answers. Therefore, anything that deals with opinions, is less suitable. Certain other sites can be more helpful, like ones dedicated to software engineering matters, or programming in general : http://programmers.stackexchange.com/ – Joeppie Jul 30 '14 at 21:09

1 Answers1

-1

As you point out, there is no necessarily right or wrong answer. I would say that it depends:

-Is it a problem for people to be able to reverse engineer the code?

-Is it a problem if it does not execute because JavaScript might be disabled?

-Is it preferable to have code execute client-side versus server-side from a performance point of view?

-Does the content generated by the output of this function qualify as something you might want indexed by Search Engines?

Depending on the importance of the above criteria/questions, JavaScript might be disqualified. From the points above, the common thing seems to be that choosing for JavaScript is more likely to lead to potentially undesirable side-effects.

The safest bet, from what I theorize, is therefore the server-side language, PHP.

Joeppie
  • 438
  • 3
  • 16