Warning: Undefined array key 0 in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/functions.php on line 406

Warning: Attempt to read property "parent" on null in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/functions.php on line 407

Warning: Attempt to read property "term_id" on null in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/functions.php on line 413

Warning: Attempt to read property "cat_name" on null in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/functions.php on line 413

定数の宣言と命名規約


Warning: Undefined variable $pre in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/functions.php on line 517
PHP

定数とは一度定義すると内容が変わらない値のことです。 PHPでの定数はスコープがグローバルであるため、どこからでも呼び出せるのも特徴です。

定数は滅多に変更するされない値、例えば「税率」や「サーバ名」などに使用します。 変数を定数のように扱うよりも、定数として定義する方がより利便性が高くなるでしょう。

なお定数は変数のように頭に”$”が不要であるため、定数だと分かりにくい問題があります。 例えば変数「server」なら”$server”と一目で変数だと分かりますが、定数「server」だと”server”で何だこりゃとなったりします。なので頭文字を大文字にする、全て大文字にするなど、命名ルールを付けておくと分かりやすいです。

定数の定義

定数はdefine()関数を使って定義します。 なお定数は変数のように頭に”$”は描きませんです。

定数の定義

efine("定数名","定数の値")

定数の使用例

efine("site_url","http://amaruweb.net");
echo "サイトURLは" . site_url . "です。";

実行結果

イトURLはhttp://amaruweb.netです。

定数の再定義はできない

定数は最初に定義した内容が保持されます。 再度difine関数で定義しても最初のdefine関数で定義した内容を変えることはできません。

定数の再定義

efine("site_url","http://amaruweb.net");
define("site_url","http://saiteigi.net");
echo "サイトURLは" . site_url . "です。";

再定義した定数の内容

イトURLはhttp://amaruweb.netです。

定数の命名規約

定数の命名規約は変数とほぼ同じです。

定数の命名規約

先頭は アンダーバー or アルファベット
・先頭以外は アンダーバー or 数字 or アルファベット

大文字と小文字も区別されるので、打ち間違いのないように気を付けましょう。

定義済み定数

PHPには定義済みの定数があり、その定数名で定義することはできません。 内容は環境によって変わるので、get_defined_constants()関数を使って調べてみることをお勧めします。


Warning: Undefined variable $category in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/content.php on line 79

Warning: Attempt to read property "name" on null in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/content.php on line 79

の記事

eyecatch

インターネット入門‐そもそもインターネットって何?

eyecatch

改行・空白・タブ

eyecatch

SEO(検索エンジン最適化)よりUO(ユーザー最適化)を心がける

eyecatch

JavaScriptの繰り返し処理

eyecatch

リセットCSSでブラウザ間差異をなくそう

eyecatch

フォントの基礎


Warning: Undefined variable $category in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/content.php on line 84

Warning: Attempt to read property "count" on null in /home/yatsuba/yatsuba.com/public_html/wp-content/themes/amaru/content.php on line 84
HOME