Contents
What variables mean (change title)
php | perl | perlr | python
....
k = key
v = value
i = numeric iterator
b = boolean
s = string
n = number
arr = arrayBooleans
php
true, false
Evaluates to false: 0,'',null,false,array() (empty array)
Everything else evaluates to trueperl
0, '', undefined variables => all evaulate to false
1, everything else => all evaluate to truepython
True, False
Evaluates to false: False, None, '', 0, [], {}, ()
Everything else evaluates to trueNull + other
php
nullperl
nullpython
None (null)Division
python
10/3 #int/int is floored
10/3.0 #int/dbl is standard
10//30 #int//dbl is floored:Numeric power
php
$n = pow(2,3);perl
$n = 2**3;python
n = 2**3python
n = pow(2,3)Mod
php
$n = 4.5%3;
$n = 4.5%3;
#res = $n == 1.5;python
n = 4.5%3For loop over an array
php
foreach($a as $v)php
foreach($a as $i => $v)php
for($i=0;$i<count($arr);$i++)perl
for $v (@array)perl
for ($i=0;$i<=$#a;$i++) {
  $v = $a[$i];
}python
for i in a:
  v = a[i]For loop over a hash
php
foreach ($arr as $k => $v)php
foreach ($arr as $v)perl
for $k (keys %hash) {
  $v = $hash[$k];
}perl
for $s (%hash)Backwards if
perl
print 'backwards' if true;Scalar construction
php
$s = 'abc';perl
$s = 'abc';python
s = 'abc'Scalar referencing
perl
see commentsScalar cloning
php
$v2 = $v;perl
$v2 = $v;python
v2 = vArray construction
php
$arr = array(1,2,3);python
arr = [1,2,3]perl
@arr = (1,2,3);perlr
$arr_ref = [1,2,3];perl
@arr = qw(cat dog fish);Array cloning
php
$a2 = $a;perl
@a2 = @a;python
a2 = a[:]python
import copy
a2 = copy.deepcopy(a)Array referencing
php
$a2 = $a;python
a2 = aHash construction with values
php
$h = array('k'=>'v','k2'=>'v2');perl
%h = ('k','v','k2','v2');perlr
$h_ref = {k => 'v', k2 => 'v2'};python
h = {'k':'v','k2':'v2'}Hash add key / value
php
$arr[$k] = $v;perl
$hash{$k} = $v;perlr
$hash->{$key};python
h[k] = vHash cloning
php
$h2 = $h;perl
%h2 = %h;python
h2 = h.copy()Hash referencing
php
$h2 = &$h;perl
1python
h2 = hCreate a range
php
$a = range(0,5);perl
@a = 1..5;Array push
php
$arr[] = $v;php
array_push($arr,$v);python
arr.append(v)perl
push $a, $v;Array pop
php
$v = array_pop($arr);perl
$v = pop @a;python
v = a.pop()Array access last value
php
$v = $a[count($a)-1];perl
$v = $a[-1];python
v = a[-1]Array access single value
php
$v = $a[1];perl
$v = $arr[5];perlr
$v = $arr->[5];python
v = a[1]Array access multiple values
perl
@a2 = @a[1,3];python
a[1::2]Array modify value
php
$arr[5] = 10;python
arr[5] = 10;perl /* using $arr[5] because $v is a scaler, you don't use @arr[5] as you'd expect
$arr[5] = 10;perlr
$arr->[5] = 10;Value exists in array
php
$b = in_array($v,$a);perl
$v = grep $_ eq $v, @arrperl
$v = grep $v, @a;python
b = v in aKey exists in hash
php
$b = isset($arr[$k])perl
$b = defined $arr[$k];python
b = a.has_key(k)Hash keys
php /* i think */
$a = array_keys($h);perl
$a = keys %h;python
a = h.keysHash values
php /* i think */
$a = array_values($h);perl
a = values %h;String concatination
php
$s = 'a'.'b';perl
'a'.'b';python
'a'+'b';Sprintf
php
$s = sprintf('The %s brown %s','quick','fox');perl
$s = sprintf('The %s brown %s','quick','fox');python /* can use a scaler instead of an tuple if only a single value */
s = 'The %s brown %s' % ('quick','brown')String to array - split
php
$arr = preg_split("/$rx_delimeter/",$s);php
$arr = split($delimeter,$s);php
$arr = explode($delimeter,$s);Array to string - join
php
$s = implode($glue,$a);Regular expression match
php
$b = preg_match("/$rx/",$s);php
preg_match_all("/$rx/",$s,$matches);perl
$b = $s =~ /$rx/;Regular expression replace
php
$s = preg_replace("/$rx/",$replacement,$s);perl
$s =~ s/$rx/$replacement/g;python
import re
$s = re.sub(rx,replacment,s)Array slice
php /* unknown */
$a = array_slice()perl
@a2 = @a[1..3];python
a2 = a[1:3]Array reverse
php
$a2 = array_reverse($a);perl
@a2 = reverse @a;Iterate line by line over a text file
php
$fh = fopen('data.txt');
while ($line = fgets($fh)) {
  $line = preg_replace('/[\r\n]/','',$line);
}
fclose($fh);php
$s = file_get_contents('data.txt');
$arr = preg_split('/\n/',$s);
foreach ($arr as $line) ...perl
open 'data.txt', FH;
for $line (<FH>) {
  chomp $line;
}
close FH;python
import codecs
fh = codecs.open(filename,'r','latin-1')
for line in fh:python
fh = open(filename,'r')
for line in fh:Write to a file
php
fopen(...),fputs(...),fclose(...);Read contents from an external website
php /* download something that implements cURL for anything more advanced using HTTP */
$s = file_get_contents($url);Call a system command, get output
php
ob_start();
system('yada');
$s = ob_get_clean;
print $s;Get arguments after calling from command line
php
$argv;perl
@ARGV;python
sys.argv;Cgi - get
php
$h = $_GET;Cgi - post
php
$h = $_POST;While loop
php /* don't think that it uses the word 'do' anywhere */
while ($b) {}perl
while ($b) {}Do loop
perl
do {} while ($b);Switch statement
php
switch ($var) {
  case 'one': // is in $var == 'one'
  default:
}Else if
php
else if
elseifpython
elifperl
elsifDeleting varaiables
php
unset($var);Print_r, var_dump
php
print_r($arr);
var_dump($var);perl
use Data::Dumper;
print Dumper \$var;Mysql datetime to timestamp - 2008-10-14 12:00:04
php
$val = explode(" ",$datetime);
$date = explode("-",$val[0]);
$time = explode(":",$val[1]);
$timestamp = mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);Open a mysql database connection
php
$db = mysql_connect($host,$username,$password);
mysql_select_db($database);
 Mysql query return assoc
php
$result = mysql_query($sql);
while ($row = mysql_assoc($result))Die / exit
php
die($s);php /* i think */
exit($s);perl
die($s);perl
exit();python
import sys
sys.exit()Class
php
class MyClass extends MyParent {
  var $my_var = 15;
  function MyClass($arg) { // constructor - __construct for php5 ??
  $this->MyParent($arg); // super
  }
}Construct object
php
$my_obj = new MyClass($arg);python
my_obj = MyClass(arg)Dynamic variable
php
${$var}Dynamic function
php
call_user_func($fn,$args);Import
php
require()
require_once()
include()
include_onceperl
use Data::Dumper;Define a constant
php
define('MY_CONST',64);Escape html to display in a browser
php
$s = htmlentities($s);Escape string for database use
php /* i think */
$s = mysql_real_escape_string($s);Encode a string in windows-1251 (though is this ever actually used even if charset is specified in html?)
Encode a string in iso-8859-1 (latin-1)
Encode a string in utf