PHPの設定ファイル(php.ini)を編集する

PHP に関する設定は php.ini ファイルを使って設定します。ここでは現在 php.ini ファイルで設定されている内容を確認し、修正を行う方法について解説します。

(Last modified: )

php.iniを編集する

XAMPP をインストールした場合、 PHP に関する設定ファイルである php.ini は XAMPP をインストールしたディレクトリの下にある php\php.ini にあります。

php.iniを編集する(1)

php.ini ファイルはテキストファイルですので、テキストエディタを使って表示したり編集したりすることができます。ここでは php.ini で設定されている内容の一部を確認し、編集を行います。

default_charset

最初に default_charset です。 default_charset は PHP で使用するデフォルトの文字エンコーディングを設定します。

現在は次のように設定されています。

; PHP's default character set is set to UTF-8.
; http://php.net/default-charset
default_charset="UTF-8"

デフォルトの文字エンコーディングは UTF-8 に設定されています。

include_path

次に include_path です。 include_path に設定されたディレクトリの中に設置したファイルは、 PHP のプログラムの中で require や include を実行するときに ファイル名だけでアクセスすることができます。

現在は次のように設定されています。

; UNIX: "/path1:/path2"
include_path=D:\xampp\php\PEAR
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
;
; PHP's default setting for include_path is ".;/path/to/php/pear"
; http://php.net/include-path

デフォルトでは XAMPP をインストールしたディレクトリの下の php\PEAR\ ディレクトリが設定されています。

インクルードパスには複数のパスを記述でき、セミコロン(;)で区切って続けて記述できます。デフォルトの設定では「C:\xampp\php\PEAR」が設定されています。

include_path(1)

実際にこのディレクトリを見てみます。

include_path(2)

デフォルトで多くのファイルが既に格納されていることが確認できます。

extension_dir & extension

次に extension_dir と extension です。 extension_dir に設定されたディレクトリの中に拡張モジュールを設置します。

現在は次のように設定されています。

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
;extension_dir = "./"
; On windows:
extension_dir="D:\xampp\php\ext"

デフォルトでは XAMPP をインストールしたディレクトリの下の php\ext\ ディレクトリが設定されています。

extension_dir(1)

実際にこのディレクトリを見てみます。

extension_dir(2)

多くのモジュールが配置されていることが確認できます。

そして実際にどのモジュールを PHP に読み込むのかについて extension で指定します。下記は php.ini ファイルに記述されている extension の一部です。

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

extension=bz2
extension=curl
;extension=ffi
;extension=ftp
extension=fileinfo
;extension=gd
extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
extension=mbstring

読み込むモジュールを「extension=モジュール名」と記述します。行の先頭に「;」が付いている行はコメントとして扱われますので「;」が付いているものは現在有効になっていないものです。今後必要になったモジュールを読み込むには先頭の「;」を削除することでモジュールが読み込まれます。

mbstring

日本語などのマルチバイト文字を扱うには mbstring モジュールを有効にしておく必要があります。デフォルトでは次のように拡張モジュールの設定がされており mbstring モジュールは有効となっています。

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

extension=mbstring

もし先頭に「;」が付いていた場合は「;」を削除して下さい。

続いて mbstring モジュールの設定を行います。

[mbstring]
; language for internal character representation.
; This affects mb_send_mail() and mbstring.detect_order.
; http://php.net/mbstring.language
;mbstring.language = Japanese

; Use of this INI entry is deprecated, use global internal_encoding instead.
; internal/script encoding.
; Some encoding cannot work as internal encoding. (e.g. SJIS, BIG5, ISO-2022-*)
; If empty, default_charset or internal_encoding or iconv.internal_encoding is used.
; The precedence is: default_charset < internal_encoding < iconv.internal_encoding
;mbstring.internal_encoding =

; Use of this INI entry is deprecated, use global input_encoding instead.
; http input encoding.
; mbstring.encoding_translation = On is needed to use this setting.
; If empty, default_charset or input_encoding or mbstring.input is used.
; The precedence is: default_charset < input_encoding < mbstring.http_input
; http://php.net/mbstring.http-input
;mbstring.http_input =

; Use of this INI entry is deprecated, use global output_encoding instead.
; http output encoding.
; mb_output_handler must be registered as output buffer to function.
; If empty, default_charset or output_encoding or mbstring.http_output is used.
; The precedence is: default_charset < output_encoding < mbstring.http_output
; To use an output encoding conversion, mbstring's output handler must be set
; otherwise output encoding conversion cannot be performed.
; http://php.net/mbstring.http-output
;mbstring.http_output =

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
; http://php.net/mbstring.encoding-translation
;mbstring.encoding_translation = Off

; automatic encoding detection order.
; "auto" detect order is changed according to mbstring.language
; http://php.net/mbstring.detect-order
;mbstring.detect_order = auto

; substitute_character used when character cannot be converted
; one from another
; http://php.net/mbstring.substitute-character
;mbstring.substitute_character = none

; Enable strict encoding detection.
;mbstring.strict_detection = Off

; This directive specifies the regex pattern of content types for which mb_output_handler()
; is activated.
; Default: mbstring.http_output_conv_mimetype=^(text/|application/xhtml\+xml)
;mbstring.http_output_conv_mimetype=

; This directive specifies maximum stack depth for mbstring regular expressions. It is similar
; to the pcre.recursion_limit for PCRE.
;mbstring.regex_stack_limit=100000

; This directive specifies maximum retry count for mbstring regular expressions. It is similar
; to the pcre.backtrack_limit for PCRE.
;mbstring.regex_retry_limit=1000000

各項目の先頭の「;」を削除した上で順に設定を変更していきます。設定方法はご利用になっている環境によって異なりますので何が正解というものはありません。下記では私が設定している例をご紹介します。

mbstring.language は mbstring で使用される言語設定のデフォルト値です。通常は Japanese を指定します。

; language for internal character representation.
; This affects mb_send_mail() and mbstring.detect_order.
; http://php.net/mbstring.language
mbstring.language = Japanese

mbstring.internal_encoding は内部文字エンコーディングのデフォルト値です。この設定は PHP 5.6.0 で非推奨となりました。コメントのままで構いません。

mbstring.http_input は HTTP 入力文字エンコーディングのデフォルト値です。この設定は PHP 5.6.0 で非推奨となりました。コメントのままで構いません。

mbstring.http_output は HTTP HTTP 出力文字エンコーディングのデフォルト値です。この設定は PHP 5.6.0 で非推奨となりました。コメントのままで構いません。

mbstring.encoding_translation は入力される HTTP クエリに関して、 文字エンコーディング検出および内部文字エンコーディングへの変換を自動で行うかどうかを設定します。 Off を指定しました。

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
; http://php.net/mbstring.encoding-translation
mbstring.encoding_translation = Off

mbstring.detect_order は文字コードの自動判別を行う時にどの文字コードから順に確認していくのかを指定します。今回は auto ではなく明示的に指定しています。

; automatic encoding detection order.
; "auto" detect order is changed according to mbstring.language
; http://php.net/mbstring.detect-order
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII

mbstring.substitute_character は無効な文字があった場合に代わりに表示する文字を指定します。今回はデフォルトの値のまま無効な文字は何も表示しない設定の none を設定しました。

; substitute_character used when character cannot be converted
; one from another
; http://php.net/mbstring.substitute-character
mbstring.substitute_character = none

mbstring.strict_detection は文字コードの自動判別を厳密に行うかどうかの設定です。今回は Off を設定しました。

; Enable strict encoding detection.
mbstring.strict_detection = Off

mbstring.http_output_conv_mimetype 、 mbstring.regex_stack_limit 、 mbstring.regex_retry_limit についてはよく分かっていないので、今回はコメントのままとしておきます。

設定項目だけを抜き出すと次のようになりました。

mbstring.language = Japanese
mbstring.encoding_translation = Off
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII
mbstring.substitute_character = none
mbstring.strict_detection = Off

php.iniの変更内容を反映する

php.ini ファイルの必要最小限の設定変更は以上となります。 PHP から MySQL(MariaDB) などのデータベースを使用する場合は MySQL に関するモジュールの読み込みや設定などが必要となりますが、それは別のページで解説します。

変更した php.ini を有効とするには Apache を再起動する必要があります。再起動すると最新の php.ini を読み込みます。

-- --

php.ini ファイルで設定されている内容を確認し、修正を行う方法について解説しました。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。