Skip to content

Instantly share code, notes, and snippets.

@gabrielbarros
Created August 6, 2025 00:09
Show Gist options
  • Save gabrielbarros/043335875a84ffcf884781b546f73cc9 to your computer and use it in GitHub Desktop.
Save gabrielbarros/043335875a84ffcf884781b546f73cc9 to your computer and use it in GitHub Desktop.
Python vs. PHP - Which is easier?

Python vs. PHP - Which is easier?

Python date and time

# timestamp now
import time
now = int(time.time())
print(now)

# timestamp to date and time
from datetime import datetime, timezone

timestamp = 946684800
print(datetime.fromtimestamp(timestamp, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S'))

# date and time to timestamp
from datetime import datetime, timezone

year = 2000
month = 1
day = 1
hour = 0
minute = 0
second = 0

dt = datetime(year, month, day, hour, minute, second, tzinfo=timezone.utc)
print(int(dt.timestamp()))

PHP date and time

// timestamp now
$now = time();
echo $now, "\n";

// timestamp to date and time
$timestamp = 946684800;
echo gmdate('Y-m-d H:i:s', $timestamp), "\n";

// date and time to timestamp
$year = 2000;
$month = 1;
$day = 1;
$hour = 0;
$minute = 0;
$second = 0;

echo gmmktime(
    hour: $hour,
    minute: $minute,
    second: $second,
    month: $month,
    day: $day,
    year: $year
), "\n";

Python HTTP request

# GET request
import requests

url = 'https://example.com'

headers = {
    'User-Agent': 'My Python script'
}

response = requests.get(url, headers=headers)

print(response.text)

# POST request
import requests

url = 'https://example.com'

headers = {
    'User-Agent': 'My Python script'
}

data = {
    'name': 'John Doe',
    'age': '40'
}

response = requests.post(url, headers=headers, data=data)

print(response.text)

PHP HTTP request

// GET request
$url = 'https://example.com';

$options = [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERAGENT => 'My PHP script',
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_CAINFO => '/etc/ssl/certs/ca-certificates.crt'
];

$curl = curl_init();
curl_setopt_array($curl, $options);

$output = curl_exec($curl);

echo $output;

// POST request
$url = 'https://example.com';

$form = [
    'name' => 'John Doe',
    'age' => '40'
];

$body = http_build_query($form);

$options = [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERAGENT => 'My PHP script',
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_CAINFO => '/etc/ssl/certs/ca-certificates.crt',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $body
];

$curl = curl_init();
curl_setopt_array($curl, $options);

$output = curl_exec($curl);

echo $output;

Python JSON

# encode json
import json

data = {
    'name': 'John Doe',
    'age': 25,
    'is_admin': False
}

print(json.dumps(data))

# decode json
import json

json_str = '{"name": "John Doe", "age": 25, "is_admin": false}'

print(json.loads(json_str))

PHP JSON

// encode JSON
$data = [
    'name' => 'John Doe',
    'age' => 25,
    'is_admin' => false
];

echo json_encode($data);

// decode JSON
$json = '{"name": "John Doe", "age": 25, "is_admin": false}';

print_r(json_decode($json));

Python math

import math

w = -1
x = 90
y = 7
z = 1.54

sum_ = x + y
sub = x - y
times = a * y
div = a / y
power = x ** y
int_div = x // y
remainder = x % y
absolute = math.fabs(w)

sqrt = math.sqrt(x)
pi = math.pi
euler = math.e
sin = math.sin(y)
cos = math.cos(y)
tan = math.tan(y)
log10 = math.log10(y)
log_e = math.log(y)
factorial = math.factorial(y)

floor = math.floor(z)
ceil = math.ceil(z)
round_ = round(z)

infinity = math.inf
not_a_number = math.nan

PHP math

$w = -1;
$x = 90;
$y = 7;
$z = 1.54;

$sum = $x + $y;
$sub = $x - $y;
$times = $a * $y;
$div = $a / $y;
$power = $x ** $y;
$intDiv = intdiv($x, $y);
$remainder = $x % $y;
$absolute = abs($w);

$sqrt = sqrt($x);
$pi = M_PI; // or pi()
$euler = M_E;
$sin = sin($y);
$cos = cos($y);
$tan = tan($y);
$log10 = log10($y);
$logE = log($y);
$factorial = (int) gmp_fact($y); // require ext-gmp

$floor = floor($z);
$ceil = ceil($z);
$round = round($z);

$infinity = INF;
$notANumber = NAN;

Python hashing

# md5
import hashlib

text = 'Hello world'
md5 = hashlib.md5(text.encode('utf-8')).hexdigest()

# sha256
import hashlib

text = 'Hello world'
sha256 = hashlib.sha256(text.encode('utf-8')).hexdigest()

PHP hashing

// md5
$text = 'Hello world';
$md5 = md5($text);

// sha256
$str = 'Hello world';
$sha256 = hash('sha256', $str);

Python password

# password to hash
import bcrypt

password = '123456'
rounds = 10

hashed_password = bcrypt.hashpw(
    password.encode('utf-8'),
    bcrypt.gensalt(rounds=rounds)
).decode('utf-8')

# verify password
import bcrypt

hashed_password = '$2b$10$UbwkPT1djnxvS6G98ZfWS.sh2NVx4dPUdplMovgWvOwGCmjcycDnW'
password = '123456'

if bcrypt.checkpw(password.encode('utf-8'), hashed_password.encode('utf-8')):
    print('OK')
else:
    print('FAIL')

PHP password

# password to hash
$password = '123456';
$rounds = 10;

$hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);

# verify password
$hash = '$2y$10$iEXQsbuN6AghJx9b4GnDz.owB26Rj7kO3EjqLJgY/QMMfA7xGnaPO';
$password = '123456';

if (password_verify($password, $hash)) {
    echo "OK\n";
} else {
    echo "FAIL\n";
}

Python CLI

# input
name = input('What\'s your name? ')
print('Your name is ' + name)

# arguments
import sys

print(len(sys.argv)) # number of arguments
print(sys.argv[1]) # 1st argument
print(sys.argv[2]) # 2nd argument

# STDIN
import sys

stdin = sys.stdin.read()

# STDOUT
import sys

sys.stdout.write("Your message here (1)\n")
print("Your message here (2)")

# STDERR
import sys

sys.stderr.write("An error has ocurred\n")

# exit code
import sys

sys.exit(1)

# execute program
import os

os.system('df -h')

# environment variable
path = os.environ['PATH']

PHP CLI

<?php
// input
$name = readline('What\'s your name? ');
echo "Your name is $name\n";

// arguments
echo $argc, "\n"; // number of arguments
echo $argv[1], "\n"; // 1st argument
echo $argv[2], "\n"; // 2nd argument

// STDIN
$stdin = file_get_contents('php://stdin');

// STDOUT
fprintf(STDOUT, "Your message here (1)\n");
echo "Your message here (2)\n";

// STDERR
fprintf(STDERR, "An error has ocurred\n");

// exit code
exit(1);

// execute program
echo shell_exec('df -h');

// environment variable
$path = getenv('PATH'); // or $_SERVER['PATH']

Python binary data

# byte sequence
bytes = b'\x63\x61\x66\xC3\xA9'

# random bytes
import os

bytes = os.urandom(10)

# quick hexdump
hexdump = 'café'.encode('utf-8').hex()

PHP binary data

// byte sequence
$bytes = "\x63\x61\x66\xC3\xA9";

// random bytes
$bytes = random_bytes(10);

// quick hexdump
$hexdump = bin2hex('café');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment