2015-10-18 07:00:23 +03:00
|
|
|
---
|
|
|
|
language: PHP
|
|
|
|
contributors:
|
|
|
|
- ["Malcolm Fell", "http://emarref.net/"]
|
|
|
|
- ["Trismegiste", "https://github.com/Trismegiste"]
|
|
|
|
translators:
|
|
|
|
- ["Kazushige Tominaga", "https://github.com/kazu9su"]
|
|
|
|
filename: learnphp.php
|
|
|
|
---
|
|
|
|
|
|
|
|
このドキュメントでは、 PHP 5+ について説明します。
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php // PHPのコードは、<?php タグで囲む必要があります。
|
|
|
|
|
2015-10-18 07:27:55 +03:00
|
|
|
// もしあなたのphpファイルがPHPのみで構成される場合、
|
2015-10-18 07:00:23 +03:00
|
|
|
// 意図しない出力を防ぐために、phpの閉じタグは省略するのがベストプラクティスです。
|
2015-10-18 07:27:55 +03:00
|
|
|
// 一行のコメントを書く場合、2つのスラッシュで始めます。
|
|
|
|
# ハッシュ(ポンド記号として知られる)を使いたいでしょうが、//のほうが一般的です
|
2015-10-18 07:00:23 +03:00
|
|
|
/*
|
2015-10-18 07:27:55 +03:00
|
|
|
スラッシュとアスタリスク、アスタリスクとスラッシュで囲むと、
|
|
|
|
複数行のコメントを書けます。
|
2015-10-18 07:00:23 +03:00
|
|
|
*/
|
|
|
|
|
2015-10-18 07:27:55 +03:00
|
|
|
// 出力をプリントしたい場合は、"echo" か "print" を使います。
|
|
|
|
print('Hello '); // これは "Hello " という改行なしの文字列をプリントします。
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2015-10-18 07:27:55 +03:00
|
|
|
// カッコ()はprintとecho関数に置いては省略できます。
|
|
|
|
echo "World\n"; // これは改行ありの"World"という文字列をプリントします。
|
|
|
|
// (全ての命令文では、セミコロンを最後に付けることが必須です。)
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2015-10-18 07:27:55 +03:00
|
|
|
// <?php タグの外側にあるものは、自動的にプリントされます。
|
2015-10-18 07:00:23 +03:00
|
|
|
?>
|
|
|
|
Hello World Again!
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/************************************
|
2015-10-18 07:27:55 +03:00
|
|
|
* 型と変数について
|
2015-10-18 07:00:23 +03:00
|
|
|
*/
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 変数は"$"マークで始まります
|
|
|
|
// 有効な変数名にするには、文字またはアンダースコア(_)で始めて,
|
|
|
|
// その後はどんな数字でも、文字でも、アンダースコアで続けても構いません
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
//ブーリアン値は大文字、小文字問いません
|
2015-10-18 07:00:23 +03:00
|
|
|
$boolean = true; // or TRUE or True
|
|
|
|
$boolean = false; // or FALSE or False
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 数値
|
2015-10-18 07:00:23 +03:00
|
|
|
$int1 = 12; // => 12
|
|
|
|
$int2 = -12; // => -12
|
2016-01-13 18:01:47 +03:00
|
|
|
$int3 = 012; // => 10 (先頭の0は8進法を示す)
|
|
|
|
$int4 = 0x0F; // => 15 (先頭の0xは16進法を示す)
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// floats(浮動小数) (別名double)
|
2015-10-18 07:00:23 +03:00
|
|
|
$float = 1.234;
|
|
|
|
$float = 1.2e3;
|
|
|
|
$float = 7E-10;
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 変数の削除
|
2015-10-18 07:00:23 +03:00
|
|
|
unset($int1);
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 計算式
|
2015-10-18 07:00:23 +03:00
|
|
|
$sum = 1 + 1; // 2
|
|
|
|
$difference = 2 - 1; // 1
|
|
|
|
$product = 2 * 2; // 4
|
|
|
|
$quotient = 2 / 1; // 2
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 式の省略
|
2015-10-18 07:00:23 +03:00
|
|
|
$number = 0;
|
2016-01-13 18:01:47 +03:00
|
|
|
$number += 1; // $numberに1加算Increment $number by 1
|
|
|
|
echo $number++; // 1 がプリントされる(式の評価の後に加算される)
|
|
|
|
echo ++$number; // 3 がプリントされる(式の評価の前に加算される)
|
|
|
|
$number /= $float; // 割り算した結果の商を$numberに割り当てる
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 文字列はシングルクォートで囲むのが望ましいです
|
2015-10-18 07:00:23 +03:00
|
|
|
$sgl_quotes = '$String'; // => '$String'
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 文字列中に、他の変数を埋め込みたい場合以外は、ダブルクォートを使用するのはやめましょう
|
2015-10-18 07:00:23 +03:00
|
|
|
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'
|
|
|
|
|
|
|
|
// Special characters are only escaped in double quotes
|
2016-01-13 18:01:47 +03:00
|
|
|
// 特殊文字はダブルクォートによってのみ、エスケープされます
|
2015-10-18 07:00:23 +03:00
|
|
|
$escaped = "This contains a \t tab character.";
|
|
|
|
$unescaped = 'This just contains a slash and a t: \t';
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// 必要があれば、変数を波括弧で囲みます
|
2015-10-18 07:00:23 +03:00
|
|
|
$money = "I have $${number} in the bank.";
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// PHP 5.3から、nowdocs形式が変数の挿入をしない複数行の文字列の定義に使用できます
|
2015-10-18 07:00:23 +03:00
|
|
|
$nowdoc = <<<'END'
|
|
|
|
Multi line
|
|
|
|
string
|
|
|
|
END;
|
|
|
|
|
2016-01-13 18:01:47 +03:00
|
|
|
// ヒアドキュメント形式なら、文字列中に変数の挿入を行えます。
|
2015-10-18 07:00:23 +03:00
|
|
|
$heredoc = <<<END
|
|
|
|
Multi line
|
|
|
|
$sgl_quotes
|
|
|
|
END;
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 文字列の連結は . で行います
|
2015-10-18 07:00:23 +03:00
|
|
|
echo 'This string ' . 'is concatenated';
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 別々のパラメータとしてechoに渡すこともできます
|
2015-10-18 07:00:23 +03:00
|
|
|
echo 'Multiple', 'Parameters', 'Valid';
|
|
|
|
|
|
|
|
/********************************
|
2016-01-14 15:15:25 +03:00
|
|
|
* 定数
|
2015-10-18 07:00:23 +03:00
|
|
|
*/
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 定数は define() を使って定義します
|
|
|
|
// また、実行中は変更することができないので注意が必要です!
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 有効は定数は文字かアンダースコアで始めます
|
|
|
|
// それ移行のは、どんな数値でも文字列でもアンダースコアでも構いません
|
2015-10-18 07:00:23 +03:00
|
|
|
define("FOO", "something");
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 定義した名前をそのまま($はつけずに)使用することで、定数にアクセスできます
|
2015-10-18 07:00:23 +03:00
|
|
|
// access to a constant is possible by direct using the choosen name
|
|
|
|
echo 'This outputs '.FOO;
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
2016-01-14 15:15:25 +03:00
|
|
|
* 配列
|
2015-10-18 07:00:23 +03:00
|
|
|
*/
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// PHPの配列はすべて連想配列です
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 連想配列は、他の言語ではハッシュ(ハッシュマップ)として知られています
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// すべてのバージョンのPHPで動作します
|
2015-10-18 07:00:23 +03:00
|
|
|
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// PHP 5.4 から、新しいシンタックスが導入されました
|
2015-10-18 07:00:23 +03:00
|
|
|
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
echo $associative['One']; // 1とプリントされます
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// キーを指定しないシンプルな配列にも、自動的に数値キーが振られます
|
2015-10-18 07:00:23 +03:00
|
|
|
$array = ['One', 'Two', 'Three'];
|
|
|
|
echo $array[0]; // => "One"
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 配列の最後に要素を追加する
|
2015-10-18 07:00:23 +03:00
|
|
|
$array[] = 'Four';
|
2016-01-14 15:15:25 +03:00
|
|
|
// または、次のようにも書けます
|
2015-10-18 07:00:23 +03:00
|
|
|
array_push($array, 'Five');
|
|
|
|
|
2016-01-14 15:15:25 +03:00
|
|
|
// 配列から要素を削除
|
2015-10-18 07:00:23 +03:00
|
|
|
unset($array[3]);
|
|
|
|
|
|
|
|
/********************************
|
2016-01-15 18:07:29 +03:00
|
|
|
* 出力
|
2015-10-18 07:00:23 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
echo('Hello World!');
|
2016-01-15 18:07:29 +03:00
|
|
|
// 標準出力にHello World! とプリントします
|
|
|
|
// 標準出力はブラウザーで実行していればWebページに出力されます
|
2015-10-18 07:00:23 +03:00
|
|
|
// Stdout is the web page if running in a browser.
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
print('Hello World!'); // echoの結果と同じです
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// echo は言語自体の構成要素であり、括弧なしで呼び出せます
|
2015-10-18 07:00:23 +03:00
|
|
|
// echo is actually a language construct, so you can drop the parentheses.
|
|
|
|
echo 'Hello World!';
|
2016-01-15 18:07:29 +03:00
|
|
|
print 'Hello World!'; // printも同様です
|
2015-10-18 07:00:23 +03:00
|
|
|
|
|
|
|
$paragraph = 'paragraph';
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
echo 100; // スカラー数値を直接出力します
|
|
|
|
echo $paragraph; // 変数も使用できます
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// PHPタグの短縮型が設定されているか、使用しているPHPのバージョンが
|
|
|
|
// 5.4.0 以上であれば、短縮echoシンタックスを使用できます
|
2015-10-18 07:00:23 +03:00
|
|
|
?>
|
|
|
|
<p><?= $paragraph ?></p>
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$x = 1;
|
|
|
|
$y = 2;
|
2016-01-15 18:07:29 +03:00
|
|
|
$x = $y; // $xに$yの値を代入します
|
2015-10-18 07:00:23 +03:00
|
|
|
$z = &$y;
|
2016-01-15 18:07:29 +03:00
|
|
|
// $zは$yへの参照です。
|
|
|
|
// $zの値を変更すると$yの値も変更されるでしょう。逆も同様です。
|
|
|
|
// $xは$yの最初の値を変わらず保持しています
|
2015-10-18 07:00:23 +03:00
|
|
|
|
|
|
|
echo $x; // => 2
|
|
|
|
echo $z; // => 2
|
|
|
|
$y = 0;
|
|
|
|
echo $x; // => 2
|
|
|
|
echo $z; // => 0
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// 変数の型と値を標準出力へダンプします
|
|
|
|
var_dump($z); // int(0) と出力されます
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// 人間が読めるフォーマットで変数を標準出力にプリントします
|
2015-10-18 07:00:23 +03:00
|
|
|
print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three )
|
|
|
|
|
|
|
|
/********************************
|
2016-01-15 18:07:29 +03:00
|
|
|
* ロジック
|
2015-10-18 07:00:23 +03:00
|
|
|
*/
|
|
|
|
$a = 0;
|
|
|
|
$b = '0';
|
|
|
|
$c = '1';
|
|
|
|
$d = '1';
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// assertは引数がfalseの場合、Exceptionを投げます
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
//これらの比較は型が違ったとしても、常に真です。
|
2015-10-18 07:00:23 +03:00
|
|
|
assert($a == $b); // equality
|
|
|
|
assert($c != $a); // inequality
|
|
|
|
assert($c <> $a); // alternative inequality
|
|
|
|
assert($a < $c);
|
|
|
|
assert($c > $b);
|
|
|
|
assert($a <= $b);
|
|
|
|
assert($c >= $d);
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// 次の比較は値が等しく、かつ同じ型である場合のみ真です
|
2015-10-18 07:00:23 +03:00
|
|
|
assert($c === $d);
|
|
|
|
assert($a !== $d);
|
|
|
|
assert(1 === '1');
|
|
|
|
assert(1 !== '1');
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// spaceship演算子はPHP7から使用可能です
|
2015-10-18 07:00:23 +03:00
|
|
|
$a = 100;
|
|
|
|
$b = 1000;
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
echo $a <=> $a; // 等しいので0になります
|
|
|
|
echo $a <=> $b; // $a < $b なので -1 です
|
|
|
|
echo $b <=> $a; // $b > $a なので 1 です
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// 変数は使用するコンテキストによって、変換されます
|
2015-10-18 07:00:23 +03:00
|
|
|
|
|
|
|
$integer = 1;
|
|
|
|
echo $integer + $integer; // => 2
|
|
|
|
|
|
|
|
$string = '1';
|
2016-01-15 18:07:29 +03:00
|
|
|
echo $string + $string; // => 2 (文字列は強制的に数値として処理されます)
|
2015-10-18 07:00:23 +03:00
|
|
|
|
|
|
|
$string = 'one';
|
|
|
|
echo $string + $string; // => 0
|
2016-01-15 18:07:29 +03:00
|
|
|
// '+'演算子は文字列'one'を数値にキャストできないので、0と出力されます
|
2015-10-18 07:00:23 +03:00
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// 型のキャスティングによって、変数を指定したもう一つの型として扱うことができます
|
2015-10-18 07:00:23 +03:00
|
|
|
// Type casting can be used to treat a variable as another type
|
|
|
|
|
|
|
|
$boolean = (boolean) 1; // => true
|
|
|
|
|
|
|
|
$zero = 0;
|
|
|
|
$boolean = (boolean) $zero; // => false
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// 型をキャストするため専用の関数も存在します
|
2015-10-18 07:00:23 +03:00
|
|
|
$integer = 5;
|
|
|
|
$string = strval($integer);
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
$var = null; // Null値
|
2015-10-18 07:00:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
2016-01-15 18:07:29 +03:00
|
|
|
* 制御構造
|
2015-10-18 07:00:23 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
print 'I get printed';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
print 'I don\'t';
|
|
|
|
} else {
|
|
|
|
print 'I get printed';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
print 'Does not get printed';
|
|
|
|
} elseif(true) {
|
|
|
|
print 'Does';
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// 参考演算子
|
2015-10-18 07:00:23 +03:00
|
|
|
print (false ? 'Does not get printed' : 'Does');
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// PHP 5.3から、三項演算子の短縮形が使用できます
|
|
|
|
// $x ? $x : 'Does'と同義です
|
2015-10-18 07:00:23 +03:00
|
|
|
$x = false;
|
|
|
|
print($x ?: 'Does');
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// null合体演算子はPHP 7から使用できます
|
2015-10-18 07:00:23 +03:00
|
|
|
$a = null;
|
|
|
|
$b = 'Does print';
|
|
|
|
echo $a ?? 'a is not set'; // prints 'a is not set'
|
|
|
|
echo $b ?? 'b is not set'; // prints 'Does print'
|
|
|
|
|
|
|
|
|
|
|
|
$x = 0;
|
|
|
|
if ($x === '0') {
|
|
|
|
print 'Does not print';
|
|
|
|
} elseif($x == '1') {
|
|
|
|
print 'Does not print';
|
|
|
|
} else {
|
|
|
|
print 'Does print';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// :を用いる別の構文はテンプレートで有用です
|
2015-10-18 07:00:23 +03:00
|
|
|
?>
|
|
|
|
|
|
|
|
<?php if ($x): ?>
|
2016-01-15 18:07:29 +03:00
|
|
|
この部分はifが真のとき表示されます
|
2015-10-18 07:00:23 +03:00
|
|
|
<?php else: ?>
|
2016-01-15 18:07:29 +03:00
|
|
|
それ以外の場合は、この部分が表示されます
|
2015-10-18 07:00:23 +03:00
|
|
|
<?php endif; ?>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
2016-01-15 18:07:29 +03:00
|
|
|
// いくつかのロジックを保存するにはswitchを使用します
|
2015-10-18 07:00:23 +03:00
|
|
|
switch ($x) {
|
|
|
|
case '0':
|
|
|
|
print 'Switch does type coercion';
|
2016-01-15 18:07:29 +03:00
|
|
|
break; // breakを書く必要があります。
|
|
|
|
// でなければ、次の'two', 'three'のcase文を続けて実行することになります。
|
2015-10-18 07:00:23 +03:00
|
|
|
case 'two':
|
|
|
|
case 'three':
|
2016-01-15 18:07:29 +03:00
|
|
|
// 変数が'two'または'three'の場合、何かを実行します
|
2015-10-18 07:00:23 +03:00
|
|
|
break;
|
|
|
|
default:
|
2016-01-15 18:07:29 +03:00
|
|
|
//デフォルトで何かを実行します
|
2015-10-18 07:00:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// While, do...while and for loops are probably familiar
|
|
|
|
$i = 0;
|
|
|
|
while ($i < 5) {
|
|
|
|
echo $i++;
|
|
|
|
}; // Prints "01234"
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
do {
|
|
|
|
echo $i++;
|
|
|
|
} while ($i < 5); // Prints "01234"
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
for ($x = 0; $x < 10; $x++) {
|
|
|
|
echo $x;
|
|
|
|
} // Prints "0123456789"
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
$wheels = ['bicycle' => 2, 'car' => 4];
|
|
|
|
|
|
|
|
// Foreach loops can iterate over arrays
|
|
|
|
foreach ($wheels as $wheel_count) {
|
|
|
|
echo $wheel_count;
|
|
|
|
} // Prints "24"
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
// You can iterate over the keys as well as the values
|
|
|
|
foreach ($wheels as $vehicle => $wheel_count) {
|
|
|
|
echo "A $vehicle has $wheel_count wheels";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
while ($i < 5) {
|
|
|
|
if ($i === 3) {
|
|
|
|
break; // Exit out of the while loop
|
|
|
|
}
|
|
|
|
echo $i++;
|
|
|
|
} // Prints "012"
|
|
|
|
|
|
|
|
for ($i = 0; $i < 5; $i++) {
|
|
|
|
if ($i === 3) {
|
|
|
|
continue; // Skip this iteration of the loop
|
|
|
|
}
|
|
|
|
echo $i;
|
|
|
|
} // Prints "0124"
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* Functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Define a function with "function":
|
|
|
|
function my_function () {
|
|
|
|
return 'Hello';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo my_function(); // => "Hello"
|
|
|
|
|
|
|
|
// A valid function name starts with a letter or underscore, followed by any
|
|
|
|
// number of letters, numbers, or underscores.
|
|
|
|
|
|
|
|
function add ($x, $y = 1) { // $y is optional and defaults to 1
|
|
|
|
$result = $x + $y;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo add(4); // => 5
|
|
|
|
echo add(4, 2); // => 6
|
|
|
|
|
|
|
|
// $result is not accessible outside the function
|
|
|
|
// print $result; // Gives a warning.
|
|
|
|
|
|
|
|
// Since PHP 5.3 you can declare anonymous functions;
|
|
|
|
$inc = function ($x) {
|
|
|
|
return $x + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
echo $inc(2); // => 3
|
|
|
|
|
|
|
|
function foo ($x, $y, $z) {
|
|
|
|
echo "$x - $y - $z";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions can return functions
|
|
|
|
function bar ($x, $y) {
|
|
|
|
// Use 'use' to bring in outside variables
|
|
|
|
return function ($z) use ($x, $y) {
|
|
|
|
foo($x, $y, $z);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
$bar = bar('A', 'B');
|
|
|
|
$bar('C'); // Prints "A - B - C"
|
|
|
|
|
|
|
|
// You can call named functions using strings
|
|
|
|
$function_name = 'add';
|
|
|
|
echo $function_name(1, 2); // => 3
|
|
|
|
// Useful for programatically determining which function to run.
|
|
|
|
// Or, use call_user_func(callable $callback [, $parameter [, ... ]]);
|
|
|
|
|
|
|
|
|
|
|
|
// You can get the all the parameters passed to a function
|
|
|
|
function parameters() {
|
|
|
|
$numargs = func_num_args();
|
|
|
|
if ($numargs > 0) {
|
|
|
|
echo func_get_arg(0) . ' | ';
|
|
|
|
}
|
|
|
|
$args_array = func_get_args();
|
|
|
|
foreach ($args_array as $key => $arg) {
|
|
|
|
echo $key . ' - ' . $arg . ' | ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* Includes
|
|
|
|
*/
|
|
|
|
|
|
|
|
<?php
|
|
|
|
// PHP within included files must also begin with a PHP open tag.
|
|
|
|
|
|
|
|
include 'my-file.php';
|
|
|
|
// The code in my-file.php is now available in the current scope.
|
|
|
|
// If the file cannot be included (e.g. file not found), a warning is emitted.
|
|
|
|
|
|
|
|
include_once 'my-file.php';
|
|
|
|
// If the code in my-file.php has been included elsewhere, it will
|
|
|
|
// not be included again. This prevents multiple class declaration errors
|
|
|
|
|
|
|
|
require 'my-file.php';
|
|
|
|
require_once 'my-file.php';
|
|
|
|
// Same as include(), except require() will cause a fatal error if the
|
|
|
|
// file cannot be included.
|
|
|
|
|
|
|
|
// Contents of my-include.php:
|
|
|
|
<?php
|
|
|
|
|
|
|
|
return 'Anything you like.';
|
|
|
|
// End file
|
|
|
|
|
|
|
|
// Includes and requires may also return a value.
|
|
|
|
$value = include 'my-include.php';
|
|
|
|
|
|
|
|
// Files are included based on the file path given or, if none is given,
|
|
|
|
// the include_path configuration directive. If the file isn't found in
|
|
|
|
// the include_path, include will finally check in the calling script's
|
|
|
|
// own directory and the current working directory before failing.
|
|
|
|
/* */
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Classes are defined with the class keyword
|
|
|
|
|
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
const MY_CONST = 'value'; // A constant
|
|
|
|
|
|
|
|
static $staticVar = 'static';
|
|
|
|
|
|
|
|
// Static variables and their visibility
|
|
|
|
public static $publicStaticVar = 'publicStatic';
|
|
|
|
// Accessible within the class only
|
|
|
|
private static $privateStaticVar = 'privateStatic';
|
|
|
|
// Accessible from the class and subclasses
|
|
|
|
protected static $protectedStaticVar = 'protectedStatic';
|
|
|
|
|
|
|
|
// Properties must declare their visibility
|
|
|
|
public $property = 'public';
|
|
|
|
public $instanceProp;
|
|
|
|
protected $prot = 'protected'; // Accessible from the class and subclasses
|
|
|
|
private $priv = 'private'; // Accessible within the class only
|
|
|
|
|
|
|
|
// Create a constructor with __construct
|
|
|
|
public function __construct($instanceProp) {
|
|
|
|
// Access instance variables with $this
|
|
|
|
$this->instanceProp = $instanceProp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Methods are declared as functions inside a class
|
|
|
|
public function myMethod()
|
|
|
|
{
|
|
|
|
print 'MyClass';
|
|
|
|
}
|
|
|
|
|
|
|
|
//final keyword would make a function unoverridable
|
|
|
|
final function youCannotOverrideMe()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Declaring class properties or methods as static makes them accessible without
|
|
|
|
* needing an instantiation of the class. A property declared as static can not
|
|
|
|
* be accessed with an instantiated class object (though a static method can).
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static function myStaticMethod()
|
|
|
|
{
|
|
|
|
print 'I am static';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Class constants can always be accessed statically
|
|
|
|
echo MyClass::MY_CONST; // Outputs 'value';
|
|
|
|
|
|
|
|
echo MyClass::$staticVar; // Outputs 'static';
|
|
|
|
MyClass::myStaticMethod(); // Outputs 'I am static';
|
|
|
|
|
|
|
|
// Instantiate classes using new
|
|
|
|
$my_class = new MyClass('An instance property');
|
|
|
|
// The parentheses are optional if not passing in an argument.
|
|
|
|
|
|
|
|
// Access class members using ->
|
|
|
|
echo $my_class->property; // => "public"
|
|
|
|
echo $my_class->instanceProp; // => "An instance property"
|
|
|
|
$my_class->myMethod(); // => "MyClass"
|
|
|
|
|
|
|
|
|
|
|
|
// Extend classes using "extends"
|
|
|
|
class MyOtherClass extends MyClass
|
|
|
|
{
|
|
|
|
function printProtectedProperty()
|
|
|
|
{
|
|
|
|
echo $this->prot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override a method
|
|
|
|
function myMethod()
|
|
|
|
{
|
|
|
|
parent::myMethod();
|
|
|
|
print ' > MyOtherClass';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$my_other_class = new MyOtherClass('Instance prop');
|
|
|
|
$my_other_class->printProtectedProperty(); // => Prints "protected"
|
|
|
|
$my_other_class->myMethod(); // Prints "MyClass > MyOtherClass"
|
|
|
|
|
|
|
|
final class YouCannotExtendMe
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can use "magic methods" to create getters and setters
|
|
|
|
class MyMapClass
|
|
|
|
{
|
|
|
|
private $property;
|
|
|
|
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
return $this->$key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __set($key, $value)
|
|
|
|
{
|
|
|
|
$this->$key = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$x = new MyMapClass();
|
|
|
|
echo $x->property; // Will use the __get() method
|
|
|
|
$x->property = 'Something'; // Will use the __set() method
|
|
|
|
|
|
|
|
// Classes can be abstract (using the abstract keyword) or
|
|
|
|
// implement interfaces (using the implements keyword).
|
|
|
|
// An interface is declared with the interface keyword.
|
|
|
|
|
|
|
|
interface InterfaceOne
|
|
|
|
{
|
|
|
|
public function doSomething();
|
|
|
|
}
|
|
|
|
|
|
|
|
interface InterfaceTwo
|
|
|
|
{
|
|
|
|
public function doSomethingElse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// interfaces can be extended
|
|
|
|
interface InterfaceThree extends InterfaceTwo
|
|
|
|
{
|
|
|
|
public function doAnotherContract();
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class MyAbstractClass implements InterfaceOne
|
|
|
|
{
|
|
|
|
public $x = 'doSomething';
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
|
|
|
|
{
|
|
|
|
public function doSomething()
|
|
|
|
{
|
|
|
|
echo $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doSomethingElse()
|
|
|
|
{
|
|
|
|
echo 'doSomethingElse';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Classes can implement more than one interface
|
|
|
|
class SomeOtherClass implements InterfaceOne, InterfaceTwo
|
|
|
|
{
|
|
|
|
public function doSomething()
|
|
|
|
{
|
|
|
|
echo 'doSomething';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doSomethingElse()
|
|
|
|
{
|
|
|
|
echo 'doSomethingElse';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* Traits
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Traits are available from PHP 5.4.0 and are declared using "trait"
|
|
|
|
|
|
|
|
trait MyTrait
|
|
|
|
{
|
|
|
|
public function myTraitMethod()
|
|
|
|
{
|
|
|
|
print 'I have MyTrait';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyTraitfulClass
|
|
|
|
{
|
|
|
|
use MyTrait;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cls = new MyTraitfulClass();
|
|
|
|
$cls->myTraitMethod(); // Prints "I have MyTrait"
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* Namespaces
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This section is separate, because a namespace declaration
|
|
|
|
// must be the first statement in a file. Let's pretend that is not the case
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
// By default, classes exist in the global namespace, and can
|
|
|
|
// be explicitly called with a backslash.
|
|
|
|
|
|
|
|
$cls = new \MyClass();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the namespace for a file
|
|
|
|
namespace My\Namespace;
|
|
|
|
|
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// (from another file)
|
|
|
|
$cls = new My\Namespace\MyClass;
|
|
|
|
|
|
|
|
//Or from within another namespace.
|
|
|
|
namespace My\Other\Namespace;
|
|
|
|
|
|
|
|
use My\Namespace\MyClass;
|
|
|
|
|
|
|
|
$cls = new MyClass();
|
|
|
|
|
|
|
|
// Or you can alias the namespace;
|
|
|
|
|
|
|
|
namespace My\Other\Namespace;
|
|
|
|
|
|
|
|
use My\Namespace as SomeOtherNamespace;
|
|
|
|
|
|
|
|
$cls = new SomeOtherNamespace\MyClass();
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* Error Handling
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Simple error handling can be done with try catch block
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Do something
|
|
|
|
} catch ( Exception $e) {
|
|
|
|
// Handle exception
|
|
|
|
}
|
|
|
|
|
|
|
|
// When using try catch blocks in a namespaced enviroment use the following
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Do something
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Handle exception
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom exceptions
|
|
|
|
|
|
|
|
class MyException extends Exception {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$condition = true;
|
|
|
|
|
|
|
|
if ($condition) {
|
|
|
|
throw new MyException('Something just happend');
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (MyException $e) {
|
|
|
|
// Handle my exception
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## More Information
|
|
|
|
|
|
|
|
Visit the [official PHP documentation](http://www.php.net/manual/) for reference
|
|
|
|
and community input.
|
|
|
|
|
|
|
|
If you're interested in up-to-date best practices, visit
|
|
|
|
[PHP The Right Way](http://www.phptherightway.com/).
|
|
|
|
|
|
|
|
If you're coming from a language with good package management, check out
|
|
|
|
[Composer](http://getcomposer.org/).
|
|
|
|
|
|
|
|
For common standards, visit the PHP Framework Interoperability Group's
|
|
|
|
[PSR standards](https://github.com/php-fig/fig-standards).
|