PHP ããã°ã©ãã "@" ã使ãã¹ãã§ãªã 5 ã¤ã®çç±
ï¼é£ãã£ã½ãã¿ã¤ãã«ã§ãã大ã¾ããã§ã
ã§ã @ ï¼ã¨ã©ã¼å¶å¾¡æ¼ç®åã¨ããã¾ãï¼ï¼ã¯ãã¼ãçãªè©±ããã¾ããããèè ã®æ¹ãããã@ã«å¯¾ãã¦æ¹å¤çã«ãªãçç±ãè¨è¼ããã¦ããªããã¨ã®ã¡ã¼ã«ãããã ãã¾ããã確ãã«ãã®éãã§ãããå®ã¯çç±ãä¸æ¸ãã®ã¨ãã«ã¯æ¸ããã®ã§ãããé·ããªã£ã¦ãã¾ã£ãã®ã§åã£ã¦ãã¹ãããã®ã§ãããããã£ã¦ãããã¥ãããªã£ã¦ãã¾ãã¾ãããï¼ãã¿ã¾ããã
ã¨ãããã¨ã§ãPHPããã°ã©ãããã¨ã©ã¼å¶å¾¡æ¼ç®åã@ã使ãã¹ãã§ãªã 5 ã¤ã®çç±ãè¿°ã¹ã¾ãï¼
å§ããåã«ãæ¬è³ªçãªã¨ãã
è²ã çç±ã¯ã¤ãããã¨ããã£ã±ãååè¿°ã¹ãã
çµçã«$qã«å ¥ããã®ãåãã§ãããã¨ã¨ãã³ã¼ãã¨ãã¦åãæå³ã§ãããã¯ãå¥ãããªãã§ããããã
ãä¸çªæ¬è³ªçãªè©±ã§ããã以ä¸ã®è©±ã§ã¯ããã¾ããã
ã¤ã¾ããçºçããå¯è½æ§ãããã¨ããã£ã¦ããã¨ã©ã¼ã表示ãããªããã¨ã¨ãã¨ã©ã¼ããããã©ããããã§ãã¯ãã¦é©åã«å¦çãããã®ãã¯ãæå³ãå
¨ç¶éãã¨ããæå³ã§ãã
1. ã¨ã©ã¼ãçºçããçç±ãããããªã
ããã¨ã¨ã©ã¼ã表示ãããªãããã«ãã¦ããã®ã§å½ããåã§ãããã¨ã©ã¼ã表示ããã¾ããã
ä¾ãã°ã以ä¸ã®ä¾ãè¦ã¾ãããã
<?php $_POST['hoge'] = 1; $a = $_POST['hoge']; $b = $_POST['fuga']; var_dump($a); var_dump($b);
ãããå®è¡ããã¨ã
Notice: Undefined index: fuga in /Users/sotarok/tmp/hoge.php on line 4 int(1) NULL
ã¨ãªãã¾ããã
ããã¯ã$_POSTã«ãhogeã¨ããã¤ã³ããã¯ã¹ã¯ãããã©ãfugaã¨ããã¤ã³ããã¯ã¹ã¯ãªãã®ã§ã$aã$bã¯ããããã1ï¼$_POSTã®hogeï¼ãnullï¼fugaã¯ã»ããããã¦ããªãã®ã§ï¼ã¨ãªãã¾ãã
ãfuga ã¨ããé
åã®ã¤ã³ããã¯ã¹ã¯ç¡ãããã¨ããNoticeãåºãã¦ããã¾ãã
POSTã«ãhogeãfugaãå ¥ã£ã¦ãããå ¥ã£ã¦ããªããããããªãã®ã§ãNoticeã表示ããã¦ã¯é¬±é¶ããã®ã§ã@ãçãã¦ãã¾ãã¾ãããã
<?php $_POST['hoge'] = 1; $a = @$_POST['hoge']; $b = @$_POST['fuga']; var_dump($a); var_dump($b);
int(1) NULL
ä¸è¦ããã£ãããã¾ãããããããããã¨ãå®ç¾ã§ãã¾ããã
ã§ã¯ã以ä¸ã®ä¾ã§ã¯ã©ãã§ãããï¼
<?php $_POST['hoge'] = 1; $a = @$_POTS['hoge']; $b = @$_POTS['fuga']; var_dump($a); var_dump($b);
NULL NULL
ãããï¼hogeã«ã¯1ãã»ãããã¦ããã®ã«ãNULLã«ãªã£ã¦ãã¾ã£ã¦ãã¾ãï¼ï¼
â@âãå¤ãã¦ã¿ã¾ããããããã¨ãå®è¡çµæã¯ã以ä¸ã®ããã«ãªãã¾ãã
Notice: Undefined variable: _POTS in /Users/sotarok/tmp/hoge.php on line 3 Notice: Undefined variable: _POTS in /Users/sotarok/tmp/hoge.php on line 4 NULL NULL
ããããªãã ã POST ãééã£ã¦ POTS ã¨æã£ã¦ãã¾ã£ã¦ããã®ããã¨ã¿ã¤ããã¹ã«æ°ã¥ããã¨ã«ãªãã¾ãã
ãããªå°ããªããã°ã©ã ã§ãããã ããè¦ãã¨ãããããªãã¹ã¯ããªãããæ°ã¥ãã ãããªã©ã¨æã£ã¦ãã¾ãããããã¾ããããããã¯é常ã«å±éºãªãã¨ã§ãã
@ãã¤ãããã®æã®ãã©ãã§ãªãã¨ã©ã¼ãçºçãã¦ããã®ããä¸åããããªãããã§ãã
ãã®ããã«ãæ¬æ¥æ°ã¥ããã¯ãã ã£ããã ããªããã¹ããã¨ã©ã¼ãåºãªããããã«ã¿ã¤ããããã1æ¥ãæ½°ãããã¨ã«ãªãã¾ãã
2. @ãã¤ããã¯ã»ãã¤ãã¨é©åã«å¤ã®ãã§ãã¯ãããæ¹æ³ã身ã«ä»ããªã
極端ãªè¨ãæ¹ããããã¾ãããã
ããªããã¨ã©ã¼ãåºã¦ããªããã¾ãã§ãåããã@ ã¤ãã¨ãããããã
ã¨ããæèãçã¾ããã®ãããªã«ãããå±éºã ã¨æãã¾ãã
$_POST 㧠null or å¤ ãã¨ãã®ã« @ ãã¤ããã¨ç°¡åã ãã¨è¦ãã¦ãã¾ã£ãããã°ã©ãã¯ããåããã©ãªããã¨ã©ã¼ãã§ããªããã¨ããç¶æ³ã§æ¬¡ã«ä½ããã¦ãã¾ãã®ããæ³åããã ãã§é³¥èããã¡ã¾ãï¼
ã¨ããããã«å訳風ã®ã»ãªãã¯ã¨ããããã¾ããã£ã±ããããä¸çªæããªãã¨æãã¾ãããªã«ãã¨ã©ã¼ãèµ·ããã¨ãã«ããã®ã¨ã©ã¼ã®æå³ãç¥ãããªããããèµ·ããã®ããã§ã¯ã©ãããã°èµ·ããªãã£ãã®ããããããã£ããã¨å¦ã¶ã¹ããªã®ã§ã¯ãªãã§ããããã
ã¨ãã«ã件ã®æ¸ç±ã«é¢ãã¦ã¯ããåå¿è
ããã¿ã¼ã²ããã¨ãªã£ã¦ããã®ã§ããããã£ãããã§ãããå¾ãªãã·ãã¥ã¨ã¼ã·ã§ã³ã§ @ ãã¤ãã¦ãããªã«ãã¨ãããã¨ã§ã¯ãªããæåããã ãnull or å¤ãã¨ãã«ã¯ãå¤å°é
ããã©ãã£ã¡ã®æ¹ãç°¡åã ããã@ã¤ãã¡ããããã¨ãã風ã«PHPã身ã«ã¤ãã¦ãã¾ãã¨ãå½ç¶ãä»ã®é¨åã§ãããã¯å®è·µããã¦ãã¾ãã¨æãã¾ãã
3. ã¨ã©ã¼ã®ãã³ããªã³ã°ã«ãã£ã¦ã¯æå³ããªããªã
PHPã«ã¯ãPHPãåãã¨ã©ã¼ãæã¾ãã¦ç¬èªã®ã¨ã©ã¼å¦çããããã¨ãã§ãã¾ãã*1
ã§ã¯ã以ä¸ã®ä¾ãè¦ã¦ã¿ã¾ãããã
<?php set_error_handler("my_error"); $_POST['hoge'] = 1; $a = $_POST['hoge']; $b = $_POST['fuga']; var_dump($a); var_dump($b); function my_error($errno, $errstr, $errfile, $errline) { // æ¬å½ã¯ããã§ã¯ã¨ã©ã¼ã®ç¨®é¡ã«ãã£ã¦å¦çãããããããã¡ã¤ã«ã«æ¸ãåºããããªã© echo "My PHP Error: $errno, $errstr, in $errfile, on $errline\n"; }
å®è¡ããã¨ã
My PHP Error: 8, Undefined index: fuga, in /Users/sotarok/tmp/hoge.php, on 5 int(1) NULL
ãã®ããã«ãã«ã¹ã¿ã ãããã¨ã©ã¼ãåºåããã¾ãã
ã§ã¯ãã¾ã鬱é¶ããã®ã§ã @ ãã¤ãã¾ãããã
<?php set_error_handler("my_error"); $_POST['hoge'] = 1; $a = @$_POST['hoge']; $b = @$_POST['fuga']; var_dump($a); var_dump($b); function my_error($errno, $errstr, $errfile, $errline) { // æ¬å½ã¯ããã§ã¯ã¨ã©ã¼ã®ç¨®é¡ã«ãã£ã¦å¦çãããããããã¡ã¤ã«ã«æ¸ãåºããããªã© echo "My PHP Error: $errno, $errstr, in $errfile, on $errline\n"; }
My PHP Error: 8, Undefined index: fuga, in /Users/sotarok/tmp/hoge.php, on 5 int(1) NULL
ã¾ãã¨ã©ã¼ãåºã¦ãã¾ãã¾ããã@ ãçãã¦ããã¯ããªã®ã«ã©ããã¦ã§ããããã
PHPã®ããã¥ã¢ã«ã«ã¯ãã¡ãã¨ãã®ç¹ã触ãããã¦ãã¾ãã
PHP ã®æ¨æºã®ã¨ã©ã¼ãã³ãã©ã¯å®å ¨ã«ãã¤ãã¹ããããã¨ã«æ³¨æãã¦ãã ããã error_reporting() ã®è¨å®ã«ããããããã©ã®ãããªå ´åã§ã ã¦ã¼ã¶ãè¨å®ããã¨ã©ã¼ãã³ãã©ãã³ã¼ã«ããã¾ãããã ãããã®å ´åã§ã ãã³ãã©ã§ error_reporting() ã®ã«ã¬ã³ãã®å¤ãèªã¿ã ããã«ãããã¦é©åã«åä½ããããã¨ã¯å¯è½ã§ããã¨ã©ã¼ãçºçããå½ä»¤ã®åã« @ ã¨ã©ã¼å¶å¾¡æ¼ç®å ãä»å ããã¦ããå ´åããã®å¤ã¯ 0 ã¨ãªããã¨ã«ã¯æ³¨æãã¾ããã
PHP: set_error_handler - Manual
ãããerror_repoting ã @ ã®æç¡ã«é¢ããããã¨ã©ã¼ãã³ãã©ã¯å¼ã°ããã®ã§ãã
ãã¡ããããerror_reporting() ã®å¤ããã§ãã¯ãã¦é©åã«å¦çããã¨ã©ã¼ãã³ãã©ãå®ç¾©ããã°ããã ãããã¨ãã主張ã¯ããã¨ã¯æãã¾ãããâ@âãã¤ããã³ã¼ãã¯ããã³ãã©ã®éãã«ãã£ã¦ã¨ã©ã¼ã®å¶å¾¡ãé©åã«ã§ãããã§ããªããããããªãã³ã¼ãã¨ãªã£ã¦ãã¾ãã®ã§ãã
4. é ã
ãããªã®ã¯ãä¸è¨3ã¤ããè¦ããæ£ç´ãã¾ãç¨åº¦ã§ãããé度ã¯é ãã§ããä¸ææãã®è©±é¡ãæµè¡ã£ããã¨ãããã¾ãã®ã§ãã§ãã¯ãã¦ã¿ãã¨è¯ãã®ã§ã¯ãªãã§ããããããã®ç¹ã¯ãæ¸ç±ã«ãæ¸ãã¦ããã¾ããã*2
詳ãããã¨ã¯ãã½ã¼ã¹èªãã£ã¦ãã¨ã§ãåãå°ãèªã¿ã¾ããï¼æ°ã«ãªãæ¹ã¯ã
ããããè¦ãã¨ã T_ISSET 㨠@ ã®éãããããã®ã§ã¯ãªãã§ããããï¼
以ä¸ã®è¨äºãè¯ãè¨äºã§ãããé常ã«ããããããæ¸ãã¦ããã¾ããã
5. ããã¡ããããã
ã ã£ã¦ã¨ã©ã¼ãæ¬æ¥åºãã¯ããªã®ã«ã¨ã©ã¼ãåºããªããããã§ãã
ãããªã®ããã¡ããããããªãã§ããï¼ï¼*3
ï¼ããããªããã
ï¼ï¼ã¤ã£ã¦ã¿ã¤ãã«ãèªåãããã£ãããï¼ã¤ãããããããã£ããã©ããç ãã§ãã
ãã®ä»ãæ§ã ãªäººã®æè¦
èªåã®èªèãããããã¦ééã£ã¦ãããããªãããã¨æã£ã¦ãå°ãå¨ãã®äººã«ãæè¦ãããã¦ã¿ã¾ãããã©ãããä¸è¨1-4(5)ã«å½ã¦ã¯ã¾ããããªæè¦ã§ã¯ãªãã§ããããï¼
é ãããã¨æ¬æ¥è¦ã¤ããããã¹ãã¨ã©ã¼ãè¦ã¤ããããªããªããã
-- ãµããã¨æ°
ããããNet_UserAgent_MobileãWarningãåããã @ new ãªã©ã¨ãã¦ãããã
ã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«ããã¦ããªããµã¼ãã«è¨ç½®ããã¨ãã«1æ¥ã¯ã¾ã£ã
-- åã ä¸çªæ°
@ ã¨ã使ããªã
ããããPHPçã¨ããã°PHPçã w
-- riafæ°
çµè«
- ã¨ã©ã¼ã¯ãã¡ãã¨ãã§ãã¯ãã
- å¤ãå ¥ã£ã¦ããã®ãããªãã®ããå ¥ã£ã¦ããå¤ãããããã®ãæ£å¸¸ãªã®ã
- é åã«å¤ãã»ããããã¦ãããå¤æ°ãã»ããããã¦ãããã¯ãissetã§
- @ã使ãã®ã¯ãããå¾ãªãå ´é¢ããä»æ¹ããªãå ´é¢ï¼ãã®PHPé¢æ°ãé¢æ°å é¨ã§ããããWarningã ãã¡ãããã ãï¼çãªï¼ãªã©ãæä½éã«éãã¹ã
- ï¼ã¤ã¾ã¨ã¾ããªãã¦ããããªãããã§ãã@ è¯ããªããªããã¨ããçç±ã¯ã ãããè¨ã£ã
ããã @ 使ã£ãæ¹ãè¯ããã¨ããçç±ããæã¡ã®æ¹ãããããããã¯ããã§æãã¦ããã ããããªãã¨æãã¾ãã
ï¼ãããããªãã¨ãªããPHP 5.3 ã® $a = $hoge?: null; ã«ã¯ @ ãçããããªã£ã¡ããã¾ããããã¼ããªããªãã§ãããããã®ä»æ§ããï¼è¶
ä¸éå端ã§ãããï¼
PHP éå¼ãã¬ã·ã (PROGRAMMER'S RECiPE)
- ä½è : é´æ¨æ²æ²»,å®è¤å»ºä¸,å±±ç°ç´æ,å «æ¨ç §æ,å±±æ¬ç¾©ä¹,æ²³åå彦
- åºç社/ã¡ã¼ã«ã¼: ç¿æ³³ç¤¾
- çºå£²æ¥: 2009/06/30
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- è³¼å ¥: 42人 ã¯ãªãã¯: 291å
- ãã®ååãå«ãããã° (30件) ãè¦ã
*1:å¤ãã®ãã¬ã¼ã ã¯ã¼ã¯ã§ã¯ãset_error_handlerãç¬èªã«ã¨ã©ã¼ãã³ããªã³ã°ããããã®çµæããã¡ã¤ã«ã«åãåºããããã¡ã¼ã«ã§ã¢ã©ã¼ããé£ã°ããããªã©ããæ©è½ãæã£ã¦ãã¾ããããã§ã¯å¤ãã¯è§¦ãã¾ããã
*2:ãªãé ããã¯æ¸ãã¦ããã¾ããã
*3:PHPãããã¡ããããã§ãããããã§ãã
*4:special thanks: ãã®è¨äºãæ¸ãã«ããã£ã¦ãanatoo, riaf, ãããã¡ããããµããã¨ãããªã©ã«ãæè¦ã伺ããã¦ããã ãã¾ããããããã¨ããããã¾ãã