Perl / PHP Übersetzung


Perl/Php Translation
Copyright © By Robert Kline
For original English text, go to: http://www.cs.wcupa.edu/~rkline/perl2php/

Erstellt von Robert Kline (Stand: 18. Februar 2005)

– Perl Arrays
– Php Arrays
 @ a = ();

@ A = ('xx', 11, 33,5,);

@ A = 12 .. 33;

$ A [2] = 'etwas';

$ Len = scalar (@ a);
# Oder
$ Len = @ a;

@ A3 = ("xx", @ A1 @ A2);

($ X, $ y) = @ a;

$ A [@ a] = 'new', # drücken

schieben
Pop
verschieben
unshift
Spleiß

foreach $ i (@ a) {.. }

 $ a = array ();

$ A = array ('xx', 11, 33,5,);

$ A = range (12,33);

$ A [2] = 'etwas';

$ Len = count ($ a);

$ A3 = array_merge ("xx", $ A1, $ A2);

list ($ x, $ y) = $ a;

$ A [] = 'new', # drücken

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 = Tasten (% h);
$ B = Werte (% h);

löschen $ 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 Datenstrukturen -
- Php Datenstrukturen -
% h = ('a' => 13, 'b' => 25);
@ X = ('Hallo', 'dort', 'alle',);

@ Mix = (\% h, \ @ x,
[33 .. 39],
{X => 15, JJ => 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 ('hallo', 'dort', 'alle',);

$ Mix = array ($ h, $ x,
Bereich (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 Fall Konvertierung -
- Php Fall Konvertierung -
 $ s = lc ($ s);
$ S = uc ($ s);

$ S = ~ tr / A-Z / A-Z /;

 $ s = strtolower ($ s);
$ S = strtoupper ($ s);

- Perl Zeichenfolgenvergleiche -
- Php Zeichenfolgenvergleiche -
 $ s1 eq $ s2

$ S1 lt $ s2

 strcmp ($ s1, $ s2) == 0
# Oder
$ S1 === $ s2

strcmp ($ s1, $ s2) <0

- Perl Funktionen -
- Php Funktionen -
 sub foo {
my @ args = @ _;
}

sub foo {
$ X = 5;
}

foo2 (\ @ a, \% h);

 Funktion foo () {
$ Args = func_get_args ();
}

Funktion foo () {
global $ x;
$ X = 5;
}

Funktion foo2 ($ x, $ y) {
}

foo2 ($ a, $ h);

- Perl String-Matching-Operationen -
- Php String-Matching-Operationen -
 $ 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 Umgebungsvariablen -
- Php Umgebungsvariablen -
% ENV

$ ENV {REQUEST_METHOD}

$ ARGV [$ i]

$ 0

 $ _SERVER

$ _SERVER [REQUEST_METHOD]

$ Argv [$ i +1]

$ Argv [0] # PHP / CGI nur

- Perl POST / GET-Parameter -
- Php POST / GET-Parameter -
 Form # / Hyperlink-Parameter:
# S: einwertig
# M: mehrwertig

use CGI (: standard);

$ S = param ('s');
@ M = param ('m');

@ Param_names = param ();
Num_params $ = param ();

 Form # / Hyperlink-Parameter:
# S: einwertig
# M []: mehrwertig
# (Wie Multi-Auswahlen
# Und Checkbox-Gruppen)

$ Param
= Array_merge ($ _GET, $ _POST);

$ S = $ param ['s']; # ein Skalar
$ M = $ param ['m']; # ein Array

$ Param_names array_keys = ($ param);
$ Num_params = count ($ param);

- Perl HTML-Elemente -
- Php HTML-Elemente -
 use CGI (: standard);

$ Ref = "x.cgi";
a ({href => $ ref}, "yy")

TextField ({name => "yy", size => 5})

Passwort ({name => "yy", size => 5})

textarea ({name => "yy",
cols => 5, Zeilen => 2})

submit ({value => "yy"})

Taste ({name => "xx",
Wert => "yy",
onclick => "submit ()",
}
)

Etiketten% = (0 => 'a', 1 => 'Q', 2 => 'x');
popup_menu ({name => "xx",
Werte => [0 .. 2],
Etiketten => \% Etiketten,
size => 4,
}
)

@ A = ('xx', 'yy', 'zz');
radio_group ({name => 'nn',
Werte => \ @ a,
Standard => '_',
linebreak => 1,
}
)

Etiketten% = ('xx' => 'L1', 'yy' => 'L2');
@ A = Tasten (Etiketten%);
checkbox_group ({name => 'nn',
Werte => \ @ a,
Etiketten => \% Etiketten,
}
)

Tabelle (
Tr (
[
td (['a', 'b']),
td (['x', 'y']),
]
)
)

 # Die Perl / CGI-Funktionen haben die
# Zusätzliche Eigenschaft der "Stabilität"
# Wenn in reentrant Formen verwendet.
# Die Werte der HTML-Elemente sind
# Entsprechend des eingehenden gesetzt
# Parameter-Werte für diese Elemente.
# Die Versionen unten sind nicht stabil.

$ Ref = "x.php";
<A href = ""> Jj

'a', 1 => 'Q', 2 => 'x');
foreach (range (0,2) als $ _)
echo "",
$ Labels [$ _];
?>

$ A = array ('xx', 'yy', 'zz');
foreach ($ a as $ _)
echo " $ _
";

$ Labels = array ('xx' => 'L1', 'yy' => 'L2');
foreach (array_keys ($ labels) als $ _)
echo " ",
$ Labels [$ _];


a · b
x · y

- Perl URL kodieren -
- Php URL kodieren -
 Verwendung URI :: Escape;

uri_escape ($ val)
uri_unescape ($ val)

urlencode ($ val)
urldecode ($ val)

- Perl MySQL-Datenbank-Zugriff -
- Php MySQL-Datenbank-Zugriff -
 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). "'"

Comments are closed.