Perlerã®ããã®PHP1
ãã¶ãPHPã使ããªããã°ãªããªãããã«ãªãã¨æãã®ã§ãä»ã®ãã¡ããåå¼·ã
ããCatalystã§ãã£ã¨éã³ããã
ã¨ãããããããè²·ã£ãã
- ä½è : David Sklar,æ¡æ潤,廣å·é¡
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã¸ã£ãã³
- çºå£²æ¥: 2005/10/27
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 6人 ã¯ãªãã¯: 54å
- ãã®ååãå«ãããã° (41件) ãè¦ã
èªãã§ããã©å¿ããªããã¡ã«ä»¥ä¸ã¡ã¢ã
éå§ã¨çµäº
PHP
<?php CODE ?>
ã<ã?phpãã¨ã?>ãã§å²ãã ã¨ãããPHPã³ã¼ãã¨ãã¦è§£éãããâ¦â¦ãã¨æã£ããã©ãã©ã¤ãã©ãªçã«æ¸ãã¨ãã¯ã?>ããã¤ããªããããã
åèï¼ãPHP基礎文法最速マスターの補足 - モノノフ日記ã
Perlã ã¨ãã®ã1;ããã¤ããªãã¨ãããªãããããã«è©²å½ããã¨æãã
大æåå°æå
Perl
print "ok\n"; #=> åã PRinT "ok\n"; #=> åããªã sub test { print "ok\n"; } test(); #=> åã TeSt(); #=> åããªã
PHP
<?php print "ok\n"; #=> åã PRinT "ok\n"; #=> åã function test() { print "ok\n"; } test(); #=> åã TeSt(); #=> åã ?>
ãªãã¨ï¼
Perlããè¦ãã人éï¼ã¾ãåã ãã©ï¼ã«ãã¦ã¿ãã°ãããéãã ã¨æã£ãï¼
é¢æ°ã«å¤§æåå°æåã®éãããªãã¨ã¯ã
â¦â¦ã¾ããããå©ç¨ãã¦ä½ãããæ°ã«ã¯ãªããªãããã©ã
ã³ã¡ã³ã
Perl
# 1è¡ã³ã¡ã³ã # 1è¡ã³ã¡ã³ã # 1è¡ã³ã¡ã³ã
è¤æ°è¡ã¯ã=podãã=cutãã§ã§ãããã©ãªãã¨ãªãéªéãªã¤ã¡ã¼ã¸ã
PHP
<?php // 1è¡ã³ã¡ã³ã # 1è¡ã³ã¡ã³ã /* * è¤æ°è¡ã³ã¡ã³ã */ ?>
3種é¡ã
æè¿ã®ã¹ã¿ã¤ã«ã§ã¯#ããã//ã好ã¾ãã¾ãã
P14
ã¸ããã¸ããã¸ããã»ãã¨ããªã
æåå
ãã¢ããã¥ã¡ã³ã
Perl
$id = 'abcde111'; print << END_OF_SQL; SELECT COUNT(*) FROM users WHERE id = '$id' END_OF_SQL
PHP
<?php $id = 'abcde111'; print <<< END_OF_SQL SELECT COUNT(*) FROM users WHERE id = '$id' END_OF_SQL; ?>
PHPã®å ´åãå°ãªãã<ãã1åå¤ãã¦3åã
Perlã¨åãã裸ã®åºåãåèªã§ä¸ã®å¤æ°ã¯å±éãããã
ãã ããçµããã®åºåãæåã«ã»ãã³ãã³ãã¤ãããã¨ãã§ããã
ããã¤ããªãã¦ãããã®ã§Perlã«æ
£ãã¦ããåã¯ãã£ã¨ã¤ããªãã
trueã¨false
å¤ãã
ifelse
Perl
if (EXPR) { } elsif (EXPR) { } else { }
é å
Perl
@array = ( 'Safari', 'FireFox', 'Chrome', 'IE', 'Opera', ); $count = @array; print $count; #=> 5
PHP
<?php $array = array( 'Safari', 'FireFox', 'Chrome', 'IE', 'Opera', ); $count = count($array); print $count; #=> 5 ?>
é
åãã$ãã«å
¥ããããè¦ãç®ãæ
£ããªããªâ¦ã
é
åã®æå¾ã«ã«ã³ãå
¥ãã¦ãåé¡ãªãã®ã¯åããããã£ãã
ããã·ã¥
Perl
%hash = ( red => '#f00', green => '#0f0', blue => '#00f', ); print $hash{red}; #=> #f00
PHP
<?php $hash = array( red => '#f00', green => '#0f0', blue => '#00f', ); print $hash[red]; #=> #f00 ?>
ããã·ã¥ãPerlã®ã%ãã®ä»£ããã«PHPã§ã¯ã$ãã«å
¥ããã
å
¥ããã¨ãã¯ã=>ããPerlã§ã¯ãã ã®ã,ãã®ä»£ããã ãã©ãPHPã§ã¯å¿
é ã¿ãããã¾ããã
åãåºãã¯[]ã
ãããããã
push()
Perl
push(@array, 'iCab');
foreach
é
åã®ã¾ããæ¹ã
ã¾ãPerlã¯forãforeachãåããã®ã§åã¯Cçãªforã¯ä½¿ããªãã®ã§ããã®ãã¼ã¸ã§ã³ã
Perl
foreach my $browser (@array) { print "$browser\n"; }
PHP
<?php foreach ($array as $browser) { print "$browser\n"; } ?>
ãããã宣è¨éãã¼ã
ããã·ã¥ã®å ´åã
Perl
foreach my $col (keys(%hash)) { print "$col, $hash{$col}\n"; }
PHP
<?php foreach ($hash as $col => $rgb) { print "$col, $hash[$col]\n"; } ?>
ããã·ã¥ã®ãã¼åå¨æç¡
Perl
if (exists($hash{red})) { }
PHP
<?php if (array_key_exists('red', $hash)) { } ?>
ããã·ã¥ã®å¤ã§åå¨æç¡
Perl
if (grep('#f00' eq $_, values(%hash))) { }
PHP
<?php if (in_array('#f00', $hash)) { } ?>
Perlã ã¨ããããããã©ãPHPã ã¨å°ç¨ã®ããããããã
ããã«ãã®ãã¼ãåå¾ã ã¨ã
Perl
if (my ($col) = grep('#f00' eq $hash{$_}, keys(%hash))) { }
é åãjoin()
Perl
print join(',', @array); #=> Safari,FireFox,Chrome,IE,Opera,iCab
PHP
<?php print implode(',', $array); #=> Safari,FireFox,Chrome,IE,Opera,iCab ?>
é åãsplit()
Perl
$browser = 'Safari,FireFox,Chrome,IE,Opera,iCab'; @array = split(/,/, $browser); print $array[2]; #=> Chrome
é åãsort()
Perl
@array = sort(@array); print "@array\n"; #=> Chrome FireFox IE Opera Safari iCab
é åãéé ã«ãã
Perl
@array = reverse(@array);
PHP
<?php $array = array_reverse($array); ?>
é åãéé ã«ã½ã¼ã
Perl
@array = reverse(sort(@array));
ããã·ã¥ãã½ã¼ã
ããããPHPãå ã«ã
PHP
<?php sort($hash); foreach ($hash as $col => $rgb) { print "$col, $rgb\n"; } #=> 0, #00f #=> 1, #0f0 #=> 2, #f00 ?>
ãããã
ãã¼ã®ã¨ãããé
åã®æ·»åã«å¤ãããã¦ãã¾ã£ããã
Perlãªãã
Perl
@array = (); push @array, $_ for sort(values(%hash));
ãããªæãã®æå³ã
ãã¼ã®å¤ãå£ããã«å¤ã®æ¹ã§ã½ã¼ãããã«ã¯asort()ã
PHP
<?php asort($hash); foreach ($hash as $col => $rgb) { print "$col, $rgb\n"; } #=> blue, #00f #=> green, #0f0 #=> red, #f00 ?>
Perlãªããããã·ã¥ã«é çªããªãã®ã§ã
Perl
@array = (); for my $rgb (sort(values(%hash))) { my ($col) = grep($rgb eq $hash{$_}, keys(%hash)); push @array, ($col, $rgb); } print "@array\n"; #=> blue #00f green #0f0 red #f00
è¤éã
ãã¼ã§ã½ã¼ãã
Perl
@array = (); for my $col (sort(keys(%hash))) { push @array, ($col, $hash{$col}); } print "@array\n"; #=> blue #00f green #0f0 red #f00
PHPã®sort()ãasort(), ksort()ã¯ããããéé ããã£ã¦ã
æé | éé |
---|---|
sort() | rsort() |
asort() | arsort() |
ksort() | krsort() |
ã¨ã®ãã¨ã
ãªããâ¦â¦ãreverseã§ããããã¨æã£ããæããªãã£ããã
ã¨ãããããã®1ã§ã