This is something very unique comparison which will gives you a quick different how same concept is set in both language.
JavaScript | PHP |
A client side language | A server side language |
Both languages are loosely typed language | Both languages are loosely typed language |
Data types | |
1. Number 2. String 3. Boolean 4. Undefined 5. Null 6. Symbol (ES 2015) 7. BigInt (ES 2020) 8. Object (array & dates) | 1. Integer 2. Float 3. String 4. Boolean 5. Null 6. Array 7. Object 8. Resources |
Check data type | |
typeof() | gettype() |
When we declare variable without value | |
If you declare a variable and don’t give it a value, the variable is automatically given a undefined value. | If you declare a variable and don’t give it a value, the variable is automatically given a null value. |
Variable Scope | |
Block scope – Variables declared inside a { } block cannot be accessed from outside the block for either let, var & const Function scope – Variables defined inside a function are not accessible (visible) from outside the function Global scope – A variable declared outside a function, becomes global | Local – Variables declared inside a { } block cannot be accessed from outside the block same as JS Global – A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. To use those inside the function we use global keyword. Static – Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable. |
Defining Variable | |
using let, var & const | We just need to add $ before variable name or for constant we can use const keyword |
Arrays | |
JavaScript has supports only array with number indexes. const cars = [ “Saab”, “Volvo”, “BMW” ]; The name indexes array is more likely object in JS | Indexed arrays – Arrays with a numeric index $cars = array(“Volvo”, “BMW”, “Toyota”); Associative arrays – Arrays with named keys $age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”); Multidimensional arrays – Arrays containing one or more arrays $cars = array ( array(“Volvo”,22,18), array(“BMW”,15,13), array(“Saab”,5,2), array(“Land Rover”,17,15) ); |
Object | |
const person = { firstName: “John”, lastName: “Doe”, age: 50, eyeColor: “blue” }; | in PHP, Object is an instance of either a built-in or user defined class. class firstClass { public $x; public $y; public function __construct() { $this->x = 10; $this->y = 20; } } $obj1 = new firstClass(); |