¥È¥Ã¥×   ÊÔ½¸ Åà·ë º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS

PHP/ʸˡ

Last-modified: 2018-11-14 (¿å) 15:23:58 (2153d)
Top / PHP / ʸˡ

PHP

´ðËܾðÊó

³ÈÄ¥»Ò.php

¥³¥á¥ó¥Èµ­½ÒÊýË¡

  • // ¥³¥á¥ó¥È
  • # ¥³¥á¥ó¥È
  • /* ¥³¥á¥ó¥È */

Ãí°ÕÅÀ: //, #¤Ï¡¢¹ÔËö¤Þ¤¿¤Ï?>¤Þ¤Ç¤¬¥³¥á¥ó¥È¤Ë¤Ê¤ê¤Þ¤¹¡£

print, echo

  • echo, print¶¦¤Ë¸À¸ì¹½Â¤¤Ê¤Î¤Ç()¤¬Ìµ¤¯¤Æ¤âµ­½Ò¤Ç¤­¤Þ¤¹¡£
  • echo¤Ï¥«¥ó¥Þ¤Ç¶èÀڤäÆÏ¢·ë½ÐÎϤǤ­¤Þ¤¹¡£
  • £²½Å°úÍÑÉä¤Ç°Ï¤à¤ÈÊÑ¿ôŸ³«¤ä¥¨¥¹¥±¡¼¥×¥·¡¼¥±¥ó¥¹¤òµ­½Ò¤Ç¤­¤Þ¤¹¡£${}¤È¤«¤®³ç¸Ì¤Ç°Ï¤à¤ÈÊÑ¿ô¤Ç¤¢¤ë¤³¤È¤¬ÌÀ³Î¤Ë¤Ê¤ê¤Þ¤¹¡£
echo $a, $b;
print "Hello ${name}\n"

·¿

·¿Îã
integer100, 0xff
booleanTRUE, FALSE
float3.14, 1.2e3
string'yes', "Hello!\n"
arrayarray('red', 'blue', 'green')
  • gettype($ÊÑ¿ô̾)¤Ç·¿Ì¾Ê¸»úÎ󤬼èÆÀ¤Ç¤­¤Þ¤¹¡£gettype(3.14)¤Çdouble¤ÈÊÖ¤ê¤Þ¤¹¤¬¡¢java¸À¸ì¤Ç¤¤¤¦¤È¤³¤í¤Îfloat¤ÈƱÍͤΰÕÌ£¤Ç¤¹¡£
  • ·¿¤ò¥Á¥§¥Ã¥¯¤¹¤ë¤È¤­¤Ïis_int($var)¤äis_string($var)¤Ê¤É¤òÍѤ¤¤Þ¤¹¡£
  • (·¿Ì¾)¤Ç¥­¥ã¥¹¥È¤Ç¤­¤Þ¤¹¡£
  • ÊÑ¿ô¤ä´Ø¿ôÌá¤êÃͤη¿Àë¸À¤Ï¤¢¤ê¤Þ¤»¤ó¡£

Äê¿ô

Äê¿ôÄêµÁÊýË¡

define("NAME", value);

ÄêµÁºÑ¤ßÄê¿ô

̾Á°°ÕÌ£
NULL̤ÄêµÁ¤òɽ¤¹ÃÍ¡£¾®Ê¸»ú¤Înull¤â»È¤¨¤Þ¤¹¡£
FALSEµ¶
TRUE¿¿
__FILE__¼Â¹Ô¤·¤Æ¤¤¤ë¥¹¥¯¥ê¥×¥È¥Õ¥¡¥¤¥ë̾
__LINE__¼Â¹Ô¤·¤Æ¤¤¤ë¹ÔÈÖ¹æ

Ãí°Õ»ö¹à¡§unset(ÊÑ¿ô̾);¤È¤¹¤ë¤ÈÊÑ¿ô¤½¤Î¤â¤Î¤¬Ìµ¤¯¤Ê¤ê¤Þ¤¹¡£NULL¤òÂåÆþ¤·¤¿ÊÑ¿ô¤Ï¤Ê¤¯¤Ê¤é¤º¤Ë̤ÄêµÁ¤È¤¤¤¦¾õÂ֤Ǹºß¤·¤Þ¤¹¡£

ÊÑ¿ô

PHP¸À¸ì¤ÎÄêµÁºÑ¤ßÊÑ¿ô

̾Á°ÆâÍÆ
$_SERVER['argv']¥¹¥¯¥ê¥×¥È¤ËÅϤµ¤ì¤ë°ú¿ô¤ÎÇÛÎó
$_SERVER['argc']¥¹¥¯¥ê¥×¥È¤ËÅϤµ¤ì¤ë°ú¿ô¤Î¿ô
$_SERVER['PHP_SELF']¥É¥­¥å¥á¥ó¥È¥ë¡¼¥È¤ò´ð½à¤È¤·¤¿¥¹¥¯¥ê¥×¥È¤ÎÁêÂХѥ¹

ApacheͳÍè¤ÎÄêµÁºÑ¤ßÊÑ¿ô

̾Á°ÆâÍÆ
$_SERVER['DOCUMENT_ROOT']¥É¥­¥å¥á¥ó¥È¥ë¡¼¥È¥Ç¥£¥ì¥¯¥È¥ê
$_SERVER['HTTP_USER_AGENT']¥æ¡¼¥¶¥¨¡¼¥¸¥§¥ó¥È
$_SERVER['REMOTE_ADDR']¥¯¥é¥¤¥¢¥ó¥È¤ÎIP¥¢¥É¥ì¥¹
$_SERVER['REMOTE_PORT']¥¯¥é¥¤¥¢¥ó¥È¤Î¥Ý¡¼¥ÈÈÖ¹æ
$_SERVER['SCRIPT_FILENAME']¥¹¥¯¥ê¥×¥È¤ÎÀäÂХѥ¹
$_SERVER['SCRIPT_NAME']¥¹¥¯¥ê¥×¥È¤ÎÁêÂХѥ¹
$_SERVER['SERVER_NAME']¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾
$_SERVER['SERVER_PORT']¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹æ
$_SERVER['SERVER_PROTOCOL']¥×¥í¥È¥³¥ë̾¤È¥Ð¡¼¥¸¥ç¥ó

²ÄÊÑÊÑ¿ô

$foo = 7;
$varX = "foo";
print($$varX); // 7

ÇÛÎ󡦥ϥå·¥å

ÇÛÎó¤ÎÄêµÁÊýË¡

$arColor = array(
    'red',
    'green',
    'blue');
$arColor = array();
$arColor[] = 'red';
$arColor[] = 'green';
$arColor[] = 'blue';
$arColor = array(
    0=>'red',
    1=>'green',
    2=>'blue');
$arColor = array();
$arColor[0] = 'red';
$arColor[1] = 'green';
$arColor[2] = 'blue';
  • print_r($arColor);¤Ç³Îǧ¤Ç¤­¤Þ¤¹¡£
    Array ( [0] => red [1] => green [2] => blue )

ÇÛÎó¸Ä¿ô¤ò»ØÄꤷ¤Æ½é´ü²½¤¹¤ëÊýË¡

$arColor = array();
$arColor = array_pad($arData, 3, 'white'); // ÇÛÎó¡¢³ÈÄ¥¸å¤ÎÇÛÎó¸Ä¿ô¡¢ÀßÄêÃÍ

ÇÛÎó½é´ü²½»þ¤ÎÃí°Õ»ö¹à

¡ß$sum_data[] = array();
¡û$sum_data = array();

¡ß$sum_data[] = array(0, 0, 0, 0, 0, 0, 0);
¡û$sum_data = array(0, 0, 0, 0, 0, 0, 0);

¼¡¤Îɽµ­¤Ï¡¢¤¹¤Ç¤Ë¸ºß¤¹¤ë$sum_dataÇÛÎó¤Ø¤Îpush¤È¤Ê¤ê¤Þ¤¹¡£
$sum_data[] = array(0, 0, 0, 0, 0, 0, 0);

¥Ï¥Ã¥·¥å¤ÎÄêµÁÊýË¡

$arColor = array(
    'red'=>'#ff0000',
    'green'=>'#00ff00',
    'blue'=>'#0000ff');
$arColor['red'] = '#ff0000';
$arColor['green'] = '#00ff00';
$arColor['blue'] = '#0000ff';
  • print_r($arColor);¤Ç³Îǧ¤Ç¤­¤Þ¤¹¡£
    Array ( [red] => #ff0000 [green] => #00ff00 [blue] => #0000ff )

ÇÛÎóÍ×ÁǤκï½ü¤ª¤è¤ÓÇÛÎ󥤥ó¥Ç¥Ã¥¯¥¹¿¶¤êľ¤·ÊýË¡

$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
$b = array_values($a); // b¤Ï¡¢array(0 => 'one', 1 =>'three')¤È¤Ê¤ê¤Þ¤¹

¼°

¼¡¤ÎÃͤÏFALSE¤È¤Ê¤ê¤Þ¤¹¡£

  • integer¤Î0
  • double¤Î0.0
  • ¶õ¤Îʸ»úÎó¡¢ ¤ª¤è¤Óʸ»úÎó¤Î"0"
  • Í×ÁǤοô¤¬¥¼¥í¤Ç¤¢¤ëÇÛÎó
  • ¥¼¥í¤òÍ×ÁǤȤ¹¤ë¥ª¥Ö¥¸¥§¥¯¥È(PHP 4¤Î¤ß)
  • ÆÃÊ̤ÊÃÍ NULL(Ãͤ¬¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤Ê¤¤ÊÑ¿ô¤ò´Þ¤à)
  • ¶õ¤Î¥¿¥°¤«¤éºîÀ®¤µ¤ì¤¿ SimpleXML ¥ª¥Ö¥¸¥§¥¯¥È

±é»»»Ò

³ä¤ê»»¤ÎÃí°ÕÅÀ

À°¿ô¤òÀ°¿ô¤Ç³ä¤Ã¤¿¾ì¹ç¡¢¾ï¤ËfloatÃͤȤʤê¤Þ¤¹¡£

ʸ»úÎó±é»»»Ò

¥Ô¥ê¥ª¥É¤Çʸ»úÎó¤Î·ë¹ç¤¬¹Ô¤¨¤Þ¤¹¡£

print("foo"."99");
$strTmp .= "123";

¼Â¹Ô±é»»»Ò

¥Ð¥Ã¥¯¥¯¥©¡¼¥È¤Ç¥³¥Þ¥ó¥É¤ò°Ï¤¦¤È¡¢¤½¤Î¼Â¹Ô·ë²Ì¤òʸ»úÎó¤È¤·¤Æ¼èÆÀ¤Ç¤­¤Þ¤¹¡£

<?php
    $strTmp = `pwd`;
    print("<pre>".$strTmp."<pre>");
?>

¥¨¥é¡¼À©¸æ±é»»»Ò

@¤ò´Ø¿ô¤ÎÀèƬ¤ËÉÕ¤±¤ë¤È¡¢¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤¬½ÐÎϤµ¤ì¤Ê¤¯¤Ê¤ê¤Þ¤¹¡£

$fin = @fopen("foo.txt", "r") or die("¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤·¤Þ¤»¤ó");

ʸ»úÎó

¥¨¥¹¥±¡¼¥×¥·¡¼¥±¥ó¥¹°ìÍ÷

µ­¹æÀâÌÀ
\xnn16¿Ê¿ôɽµ­¤Î£±Ê¸»ú
\r¥­¥ã¥ê¥Ã¥¸¥ê¥¿¡¼¥ó, CR, 0x0d
\n²þ¹Ô, LF, 0x0a
\t¿åÊ¿¥¿¥Ö
\$¥É¥ëµ­¹æ, PHP¤Ç¤ÏÆó½Å°úÍÑÉäʸ»úÎó¤ÎÃæ¤ÇÊÑ¿ô¤òŸ³«¤Ç¤­¤Þ¤¹¡£
\"Æó½Å°úÍÑÉä
\\\¼«¿È, °ì½Å°úÍÑÉäʸ»úÎó¤ÎÃæ¤Ç¤â»ÈÍѲÄǽ
\'°ì½Å°úÍÑÉä, °ì½Å°úÍÑÉäʸ»úÎó¤ÎÃæ¤Ç»ÈÍÑ

¥Ò¥¢¥É¥­¥å¥á¥ó¥È

$str = <<< HERE
¤Ò¤È¤è¤Ò¤È¤è¤Ë
¤Ò¤È¤ß¤´¤í
HERE;
print("<pre>".$str."<pre>");

¥Ò¥¢¥É¥­¥å¥á¥ó¥È¤ÎÃæ¤Ï¥Ñ¡¼¥¹¤µ¤ì¤Þ¤¹¡£

À©¸æ¹½Â¤

if, switch, while, do while, forʸ¤Ï¡¢java¤ÈƱ¤¸¤Ç¤¹¡£

$arColor = array(
    'red'=>'#ff0000',
    'green'=>'#00ff00',
    'blue'=>'#0000ff');
foreach ($arColor as $value) {
    print($value."<br />");
}
foreach ($arColor as $key => $value) {
    print($key."<br />");
}

´Ø¿ô

$g_charForPrint = '*';
function printChars($iCount) {
    global $g_charForPrint;
    for ($i = 0; $i < $iCount; $i++) {
        print($g_charForPrint);
    }
}
printChars(10);

¥é¥¤¥Ö¥é¥ê¡¦¥¤¥ó¥¯¥ë¡¼¥É¥Õ¥¡¥¤¥ë

¥¯¥é¥¹