PHP and JavaScript
I’ve been using JavaScript a lot for the past month and I just finished another course in codeacademy which is PHP and I think it’s a good idea to compare the two languages.
Most PHP commands and functions are nearly the same with JavaScript like the if/else statement switch statement, creating classes and functions, etc. But there are things that you can do in PHP that you can’t do in JavaScritp and there are things that you can do in JS that you can’t do in PHP. PHP is often used to access data in the database. It is used as a backend language while JS is often used in the frontend side.
Code comparison
1 2 |
|
This is how you declared a variable in PHP, while in JS you can do it like this:
1 2 |
|
”$” in PHP is the same as “var” in JS. It tells the interpreter that it’s a variable
Switch statement
Switch stament in PHP is done exactly the same way as you do it in JS, the only thing here is in PHP, there are two ways two write a switch statement.
this is the conventional way to write switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
and the other way is using ‘endswitch’ or alternative systax.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Classes
You can also create Classes and Objects in PHP. The major difference here between PHP and JS is the way how you access properties. You can access properties of Classes in JS by means of dot notation while in PHP you can do it like this:
1 2 3 4 |
|