PHP vs Python Syntax Comparison
| PHP: Variable Declaration |
|---|
$name = "Alice"; |
$age = 30; |
| Python: Variable Declaration |
|---|
name = "Alice" |
age = 30 |
| PHP: Output |
|---|
echo "Hello"; |
| Python: Output |
|---|
print("Hello") |
| PHP: Return |
|---|
function getValue() { |
| Python: Return |
|---|
def get_value(): |
| PHP: Assignment in If |
|---|
if ($x = $y) { ... } |
| Python: Assignment in If |
|---|
if (x := y): ... |
| PHP: Not Empty |
|---|
if (!empty($x)) { ... } |
| Python: Not Empty |
|---|
if x: |
| PHP: Concatenate |
|---|
$greeting = "Hi " . $name; |
| Python: Concatenate |
|---|
greeting = "Hi " + name |
| PHP: foreach |
|---|
foreach ($arr as $x) { |
| Python: for loop |
|---|
for x in arr: |
| PHP: while loop |
|---|
$i = 0; |
| Python: while loop |
|---|
i = 0 |
| PHP: Multiply |
|---|
$total = $x * $y; |
| Python: Multiply |
|---|
total = x * y |
| PHP: Indexed Array |
|---|
$colors = ["red", "green"]; |
echo $colors[1]; |
| Python: List |
|---|
colors = ["red", "green"] |
print(colors[1]) |
| PHP: Echo Object List |
|---|
|
| Python: Print Object List |
|---|
|
| PHP: Assoc Array |
|---|
$user = [ |
echo $user["name"]; |
| Python: Dictionary |
|---|
user = { |
print(user["name"]) |
| PHP: Value in Array |
|---|
|
| Python: Value in List |
|---|
|
| PHP: Count |
|---|
count($colors) |
| Python: Length |
|---|
len(colors) |