|
Documents
|
Perl/Php Translation
Created by
Robert Kline
(last updated: Feb 18, 2005)
— Perl arrays —
|
— Php arrays —
|
@a = ();
@a = ( 'xx', 11, 33.5, );
@a = 12..33;
$a[2] = 'something';
$len = scalar(@a);
# or
$len = @a;
@a3 = ('xx', @a1, @a2);
($x, $y) = @a;
$a[@a] = 'new'; # push
push
pop
shift
unshift
splice
foreach $i (@a) { .. }
|
$a = array();
$a = array( 'xx', 11, 33.5, );
$a = range(12,33);
$a[2] = 'something';
$len = count($a);
$a3 = array_merge('xx', $a1, $a2);
list($x, $y) = $a;
$a[] = 'new'; # push
array_push
array_pop
array_shift
array_unshift
array_splice
foreach ($a as $i) { .. }
|
— Perl hashes —
|
— Php hashes —
|
%h = ();
%h = ( 'x' => 'y',
'z' => 'w',
);
$h{'x'} = 7;
while (($key,$value) = each(%h))
{ .. }
$a = keys(%h);
$b = values(%h);
delete $h{'x'};
|
$h = array();
$h = array( 'x' => 'y',
'z' => 'w',
);
$h['x'] = 7;
foreach ($h as $key => $value)
{ .. }
$a = array_keys($h);
$b = array_values($h);
unset( $h['x'] );
|
— Perl data structures —
|
— Php data structures —
|
%h = ('a'=>13, 'b'=>25);
@x = ('hi', 'there', 'all',);
@mix = ( \%h, \@x,
[33..39],
{ x=>15, yy=>23, },
);
$mix[0]->{'b'} # == 25
$mix[0]{'b'} # == 25
$mix[2]->[2] # == 35
$mix[2][2] # == 35
|
$h = array('a'=>13, 'b'=>25);
$x = array('hi', 'there', 'all',);
$mix = array($h, $x,
range(33,39),
array('x'=>15, 'yy'=>23),
);
$mix[0]['b'] # == 25
$mix[2][2] # == 35
|
— Perl array split/join —
|
— Php array split/join —
|
@a = split( '\|', $s );
@a = split( '\s+', $s );
$s = join( '|', @a );
|
$a = preg_split( '/\|/', $s,
-1, PREG_SPLIT_NO_EMPTY );
$a = preg_split( '/\s+/', $s,
-1, PREG_SPLIT_NO_EMPTY );
$s = join( '|', $a );
|
— Perl case conversion —
|
— Php case conversion —
|
$s = lc($s);
$s = uc($s);
$s =~ tr/a-z/A-Z/;
|
$s = strtolower($s);
$s = strtoupper($s);
|
— Perl string comparisons —
|
— Php string comparisons —
|
$s1 eq $s2
$s1 lt $s2
|
strcmp($s1,$s2) == 0
# or
$s1 === $s2
strcmp($s1,$s2) < 0
|
— Perl functions —
|
— Php functions —
|
sub foo {
my @args = @_;
}
sub foo {
$x = 5;
}
foo2( \@a, \%h );
|
function foo() {
$args = func_get_args();
}
function foo() {
global $x;
$x = 5;
}
function foo2($x, $y) {
}
foo2( $a, $h );
|
— Perl string matching operations —
|
— Php string matching operations —
|
$s =~ m/(\w+)/;
$substr = $1;
@all = ($s =~ m/(\w+)/g);
$s =~ s/\s+/X/;
$s =~ s/\s+/X/g;
$s =~ s/^\s+|\s+$//g;
|
preg_match( "/(\w+)/", $s, $match );
$substr = $match[1];
preg_match_all( "/(\w+)/", $s, $match );
$all = $match[0];
$s = preg_replace( "/\s+/", 'X', $s, 1 );
$s = preg_replace( "/\s+/", 'X', $s );
$s = trim($s);
|
— Perl basename/dirname —
|
— Php basename/dirname —
|
use File::Basename;
$b = basename($path);
$d = dirname($path);
|
$b = basename($path);
$d = dirname($path);
|
— Perl environment variables —
|
— Php environment variables —
|
%ENV
$ENV{REQUEST_METHOD}
$ARGV[$i]
$0
|
$_SERVER
$_SERVER[REQUEST_METHOD]
$argv[$i+1]
$argv[0] # Php/CGI only
|
— Perl POST/GET parameters —
|
— Php POST/GET parameters —
|
#form/hyperlink parameters:
# s : single-valued
# m : multi-valued
use CGI (:standard);
$s = param('s');
@m = param('m');
@param_names = param();
$num_params = param();
|
#form/hyperlink parameters:
# s : single-valued
# m[] : multi-valued
# (such as multi-selections
# and checkbox groups)
$PARAM
= array_merge($_GET, $_POST);
$s = $PARAM['s']; # a scalar
$m = $PARAM['m']; # an array
$param_names = array_keys($PARAM);
$num_params = count($PARAM);
|
ToDo: fix this part which is commented out
— Perl URL encode —
|
— Php URL encode —
|
use URI::Escape;
uri_escape($val)
uri_unescape($val)
|
urlencode($val)
urldecode($val)
|
— Perl MySQL database access —
|
— Php MySQL database access —
|
use DBI;
$dbh = DBI->connect(
'DBI:mysql:test:localhost',
$usr,$pwd
);
$dbh->do( $sql_op )
$query = $dbh->prepare( $sql_op );
$query->execute();
while(
@record = $query->fetchrow() )
{ .. }
$dbh->quote($val)
|
$dbh = mysql_connect(
'localhost', $usr, $pwd
);
mysql_query('USE test')
mysql_query( $sql_op );
$results = mysql_query( $sql_op );
while($record =
mysql_fetch_row($results))
{ .. }
"'" . addslashes($val) . "'"
|
Website by Joachim Michaelis
|
|