Programming Syntax Comparison
Concept C++ Python PHP JavaScript
Single-line // text # text // text // text
comment
Block /* ... */ ''' ... ''' or """ ... /* ... */ /* ... */
comment """
Print / std::cout print("Hello") echo "Hello"; console.log("Hello")
Hello << "Hello"; ;
Variable int x = 10; x = 10 $x = 10; let x = 10;
Constant const int X: Final=5 define('X',5); const X=5;
X=5;
String std::string s = "hi" $s = "hi"; let s = "hi";
s="hi";
String s1 + s2 "a" + "b" $a . $b "a" + "b"
concat
Length of s.size() / len(s) strlen($s) s.length
string s.length()
Indexing s[i] s[i] $s[$i] s[i]
Arithmetic +-*/% + - * / % // ** + - * / % ** + - * / % **
ops
If / else if(...){} if: ... elif: ... else: if(...){} elseif(...){} if(...){} else if(...){}
else{} else{} else{}
Switch switch(x){.. match (3.10+) or switch($x){...} switch(x){...}
.} if/elif
For loop for(int for i in range(n): for($i=0;$i<$n;$i+ for(let
(count) i=0;i<n;i++ +){} i=0;i<n;i++){}
) {}
1
While loop while(cond while cond: while(cond){} while(cond){}
){}
For-each for(auto for e in iterable: foreach($arr as for(const e of arr){}
&e: v){} $k=>$v){}
Array/List int [1,2,3] $a=[1,2,3]; let a=[1,2,3];
a[3]={1,2,3
};
Assoc/Dict/ map<string {'k':1} ['k'=>1] let m={k:1};
Map ,int> m;
m["k"]=1;
Function int f(int def f(a): ... function f($a){...} function f(a){...}
a){...}
Lambda [](int lambda x: x*x fn($x)=>$x*$x (x)=>x*x
x){return
x*x;}
Class class C{ class C: class C{ public $x; class C {
public: int } constructor(x){...} }
x; };
Inheritance class D: class D(C): ... class D extends C class D extends C
public C {} {} {}
Try/Catch try{...} try: ... except ... try{...} try{...} catch(e){...}
catch(...) {} else ... finally catch(Exception finally
$e){...}
File read ifstream open('a.txt').read( file_get_contents('a fs.readFileSync('a.tx
f("a.txt"); ) .txt') t','utf8')
File write ofstream open('b.txt','w'). file_put_contents('a fs.writeFileSync('b.t
f("b.txt"); write(s) .txt',$s) xt',s)
f<<s;