Everybody has their own work brest practise : in my case I rely on arrays for lots of algorithmic trick, an dI often get confused when I use theme in both PHP and Javascript
if like me you work on full stack development on a mix javascript frontend and PHP backend, then you switch from one to the other every minute. Quick cheat sheet of syntax variation
php | javascript | |
---|---|---|
ARRAYS | ||
Array init | $arr=[] $arr=array() | let arr=Array(2) arr=[] arr=Array("2", "3") Array initialization is tricky in javascript , confusion can occur because if you give a number, Array will specify array length, otherwise, specify array content |
Array add element | $arr[]="element" | arr.push("element") |
array loop | for ($k=0; $k<count($arr); $k++) {echo $arr[$i]} foreach ($arr as $k=>$v){echo $arr[$i]} | for (let i = 0; i < arr.length ; i++) { {console.log(arr[i]);} arr.forEach((element, index) => console.log(element)); for (const element of array1) {console.log(element);} |
String concat | use dot , example $str = $str1.$str2 | use + , example $str=$str1+str2 |
NUMBERS | ||
round decimal numbers (2 decimals) | round($number, 2) | number.toFixed(2); |