Python vs. PHP - Which is easier?
# 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 ()))
// 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" ;
# 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 )
// 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 ;
# 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 ))
// 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 ));
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
$ 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 ;
# 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 ()
// md5
$ text = 'Hello world ' ;
$ md5 = md5 ($ text );
// sha256
$ str = 'Hello world ' ;
$ sha256 = hash ('sha256 ' , $ str );
# 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' )
# 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" ;
}
# 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
// 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']
# 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 ()
// byte sequence
$ bytes = "\x63\x61\x66\xC3\xA9" ;
// random bytes
$ bytes = random_bytes (10 );
// quick hexdump
$ hexdump = bin2hex ('café ' );