From 95af711a03e89d6f95b81e5b9d9b4cb530789a35 Mon Sep 17 00:00:00 2001 From: Tomy Date: Sun, 18 Oct 2015 13:00:23 +0900 Subject: begin translate --- ja-jp/php.html.markdown | 767 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 767 insertions(+) create mode 100644 ja-jp/php.html.markdown (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown new file mode 100644 index 00000000..3831e220 --- /dev/null +++ b/ja-jp/php.html.markdown @@ -0,0 +1,767 @@ +--- +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 + +Hello World Again! + 12 +$int2 = -12; // => -12 +$int3 = 012; // => 10 (a leading 0 denotes an octal number) +$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal) + +// Floats (aka doubles) +$float = 1.234; +$float = 1.2e3; +$float = 7E-10; + +// Delete variable +unset($int1); + +// Arithmetic +$sum = 1 + 1; // 2 +$difference = 2 - 1; // 1 +$product = 2 * 2; // 4 +$quotient = 2 / 1; // 2 + +// Shorthand arithmetic +$number = 0; +$number += 1; // Increment $number by 1 +echo $number++; // Prints 1 (increments after evaluation) +echo ++$number; // Prints 3 (increments before evaluation) +$number /= $float; // Divide and assign the quotient to $number + +// Strings should be enclosed in single quotes; +$sgl_quotes = '$String'; // => '$String' + +// Avoid using double quotes except to embed other variables +$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' + +// Special characters are only escaped in double quotes +$escaped = "This contains a \t tab character."; +$unescaped = 'This just contains a slash and a t: \t'; + +// Enclose a variable in curly braces if needed +$money = "I have $${number} in the bank."; + +// Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners +$nowdoc = <<<'END' +Multi line +string +END; + +// Heredocs will do string interpolation +$heredoc = << 1, 'Two' => 2, 'Three' => 3); + +// PHP 5.4 introduced a new syntax +$associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; + +echo $associative['One']; // prints 1 + +// List literals implicitly assign integer keys +$array = ['One', 'Two', 'Three']; +echo $array[0]; // => "One" + +// Add an element to the end of an array +$array[] = 'Four'; +// or +array_push($array, 'Five'); + +// Remove element from array +unset($array[3]); + +/******************************** + * Output + */ + +echo('Hello World!'); +// Prints Hello World! to stdout. +// Stdout is the web page if running in a browser. + +print('Hello World!'); // The same as echo + +// echo is actually a language construct, so you can drop the parentheses. +echo 'Hello World!'; +print 'Hello World!'; // So is print + +$paragraph = 'paragraph'; + +echo 100; // Echo scalar variables directly +echo $paragraph; // or variables + +// If short open tags are configured, or your PHP version is +// 5.4.0 or greater, you can use the short echo syntax +?> +

+ 2 +echo $z; // => 2 +$y = 0; +echo $x; // => 2 +echo $z; // => 0 + +// Dumps type and value of variable to stdout +var_dump($z); // prints int(0) + +// Prints variable to stdout in human-readable format +print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three ) + +/******************************** + * Logic + */ +$a = 0; +$b = '0'; +$c = '1'; +$d = '1'; + +// assert throws a warning if its argument is not true + +// These comparisons will always be true, even if the types aren't the same. +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); + +// The following will only be true if the values match and are the same type. +assert($c === $d); +assert($a !== $d); +assert(1 === '1'); +assert(1 !== '1'); + +// spaceship operator since PHP 7 +$a = 100; +$b = 1000; + +echo $a <=> $a; // 0 since they are equal +echo $a <=> $b; // -1 since $a < $b +echo $b <=> $a; // 1 since $b > $a + +// Variables can be converted between types, depending on their usage. + +$integer = 1; +echo $integer + $integer; // => 2 + +$string = '1'; +echo $string + $string; // => 2 (strings are coerced to integers) + +$string = 'one'; +echo $string + $string; // => 0 +// Outputs 0 because the + operator cannot cast the string 'one' to a number + +// Type casting can be used to treat a variable as another type + +$boolean = (boolean) 1; // => true + +$zero = 0; +$boolean = (boolean) $zero; // => false + +// There are also dedicated functions for casting most types +$integer = 5; +$string = strval($integer); + +$var = null; // Null value + + +/******************************** + * Control Structures + */ + +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'; +} + +// ternary operator +print (false ? 'Does not get printed' : 'Does'); + +// ternary shortcut operator since PHP 5.3 +// equivalent of "$x ? $x : 'Does'"" +$x = false; +print($x ?: 'Does'); + +// null coalesce operator since php 7 +$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'; +} + + + +// This alternative syntax is useful for templates: +?> + + +This is displayed if the test is truthy. + +This is displayed otherwise. + + + 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 + */ + +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 + + Date: Sun, 18 Oct 2015 13:27:55 +0900 Subject: translate first block --- ja-jp/php.html.markdown | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown index 3831e220..4448a1f5 100644 --- a/ja-jp/php.html.markdown +++ b/ja-jp/php.html.markdown @@ -13,32 +13,30 @@ filename: learnphp.php ```php Hello World Again! Date: Thu, 14 Jan 2016 00:01:47 +0900 Subject: translate until line 100 --- ja-jp/php.html.markdown | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown index 4448a1f5..0cea2041 100644 --- a/ja-jp/php.html.markdown +++ b/ja-jp/php.html.markdown @@ -39,61 +39,62 @@ Hello World Again! * 型と変数について */ -// Variables begin with the $ symbol. -// A valid variable name starts with a letter or underscore, -// followed by any number of letters, numbers, or underscores. +// 変数は"$"マークで始まります +// 有効な変数名にするには、文字またはアンダースコア(_)で始めて, +// その後はどんな数字でも、文字でも、アンダースコアで続けても構いません -// Boolean values are case-insensitive +//ブーリアン値は大文字、小文字問いません $boolean = true; // or TRUE or True $boolean = false; // or FALSE or False -// Integers +// 数値 $int1 = 12; // => 12 $int2 = -12; // => -12 -$int3 = 012; // => 10 (a leading 0 denotes an octal number) -$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal) +$int3 = 012; // => 10 (先頭の0は8進法を示す) +$int4 = 0x0F; // => 15 (先頭の0xは16進法を示す) -// Floats (aka doubles) +// floats(浮動小数) (別名double) $float = 1.234; $float = 1.2e3; $float = 7E-10; -// Delete variable +// 変数の削除 unset($int1); -// Arithmetic +// 計算式 $sum = 1 + 1; // 2 $difference = 2 - 1; // 1 $product = 2 * 2; // 4 $quotient = 2 / 1; // 2 -// Shorthand arithmetic +// 式の省略 $number = 0; -$number += 1; // Increment $number by 1 -echo $number++; // Prints 1 (increments after evaluation) -echo ++$number; // Prints 3 (increments before evaluation) -$number /= $float; // Divide and assign the quotient to $number +$number += 1; // $numberに1加算Increment $number by 1 +echo $number++; // 1 がプリントされる(式の評価の後に加算される) +echo ++$number; // 3 がプリントされる(式の評価の前に加算される) +$number /= $float; // 割り算した結果の商を$numberに割り当てる -// Strings should be enclosed in single quotes; +// 文字列はシングルクォートで囲むのが望ましいです $sgl_quotes = '$String'; // => '$String' -// Avoid using double quotes except to embed other variables +// 文字列中に、他の変数を埋め込みたい場合以外は、ダブルクォートを使用するのはやめましょう $dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' // Special characters are only escaped in double quotes +// 特殊文字はダブルクォートによってのみ、エスケープされます $escaped = "This contains a \t tab character."; $unescaped = 'This just contains a slash and a t: \t'; -// Enclose a variable in curly braces if needed +// 必要があれば、変数を波括弧で囲みます $money = "I have $${number} in the bank."; -// Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners +// PHP 5.3から、nowdocs形式が変数の挿入をしない複数行の文字列の定義に使用できます $nowdoc = <<<'END' Multi line string END; -// Heredocs will do string interpolation +// ヒアドキュメント形式なら、文字列中に変数の挿入を行えます。 $heredoc = << Date: Thu, 14 Jan 2016 21:15:25 +0900 Subject: translate 100 to 150 lines --- ja-jp/php.html.markdown | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown index 0cea2041..c01e2ba0 100644 --- a/ja-jp/php.html.markdown +++ b/ja-jp/php.html.markdown @@ -100,53 +100,54 @@ Multi line $sgl_quotes END; -// String concatenation is done with . +// 文字列の連結は . で行います echo 'This string ' . 'is concatenated'; -// Strings can be passed in as parameters to echo +// 別々のパラメータとしてechoに渡すこともできます echo 'Multiple', 'Parameters', 'Valid'; /******************************** - * Constants + * 定数 */ -// A constant is defined by using define() -// and can never be changed during runtime! +// 定数は define() を使って定義します +// また、実行中は変更することができないので注意が必要です! -// a valid constant name starts with a letter or underscore, -// followed by any number of letters, numbers, or underscores. +// 有効は定数は文字かアンダースコアで始めます +// それ移行のは、どんな数値でも文字列でもアンダースコアでも構いません define("FOO", "something"); +// 定義した名前をそのまま($はつけずに)使用することで、定数にアクセスできます // access to a constant is possible by direct using the choosen name echo 'This outputs '.FOO; /******************************** - * Arrays + * 配列 */ -// All arrays in PHP are associative arrays (hashmaps), +// PHPの配列はすべて連想配列です -// Associative arrays, known as hashmaps in some languages. +// 連想配列は、他の言語ではハッシュ(ハッシュマップ)として知られています -// Works with all PHP versions +// すべてのバージョンのPHPで動作します $associative = array('One' => 1, 'Two' => 2, 'Three' => 3); -// PHP 5.4 introduced a new syntax +// PHP 5.4 から、新しいシンタックスが導入されました $associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; -echo $associative['One']; // prints 1 +echo $associative['One']; // 1とプリントされます -// List literals implicitly assign integer keys +// キーを指定しないシンプルな配列にも、自動的に数値キーが振られます $array = ['One', 'Two', 'Three']; echo $array[0]; // => "One" -// Add an element to the end of an array +// 配列の最後に要素を追加する $array[] = 'Four'; -// or +// または、次のようにも書けます array_push($array, 'Five'); -// Remove element from array +// 配列から要素を削除 unset($array[3]); /******************************** -- cgit v1.2.3 From b5bdff8f9eca5385dcd76c42cd19b17e58307584 Mon Sep 17 00:00:00 2001 From: Tomy Date: Sat, 16 Jan 2016 00:07:29 +0900 Subject: translate to 328 --- ja-jp/php.html.markdown | 85 +++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 41 deletions(-) (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown index c01e2ba0..22c0561c 100644 --- a/ja-jp/php.html.markdown +++ b/ja-jp/php.html.markdown @@ -151,37 +151,39 @@ array_push($array, 'Five'); unset($array[3]); /******************************** - * Output + * 出力 */ echo('Hello World!'); -// Prints Hello World! to stdout. +// 標準出力にHello World! とプリントします +// 標準出力はブラウザーで実行していればWebページに出力されます // Stdout is the web page if running in a browser. -print('Hello World!'); // The same as echo +print('Hello World!'); // echoの結果と同じです +// echo は言語自体の構成要素であり、括弧なしで呼び出せます // echo is actually a language construct, so you can drop the parentheses. echo 'Hello World!'; -print 'Hello World!'; // So is print +print 'Hello World!'; // printも同様です $paragraph = 'paragraph'; -echo 100; // Echo scalar variables directly -echo $paragraph; // or variables +echo 100; // スカラー数値を直接出力します +echo $paragraph; // 変数も使用できます -// If short open tags are configured, or your PHP version is -// 5.4.0 or greater, you can use the short echo syntax +// PHPタグの短縮型が設定されているか、使用しているPHPのバージョンが +// 5.4.0 以上であれば、短縮echoシンタックスを使用できます ?>

2 echo $z; // => 2 @@ -189,23 +191,23 @@ $y = 0; echo $x; // => 2 echo $z; // => 0 -// Dumps type and value of variable to stdout -var_dump($z); // prints int(0) +// 変数の型と値を標準出力へダンプします +var_dump($z); // int(0) と出力されます -// Prints variable to stdout in human-readable format +// 人間が読めるフォーマットで変数を標準出力にプリントします print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three ) /******************************** - * Logic + * ロジック */ $a = 0; $b = '0'; $c = '1'; $d = '1'; -// assert throws a warning if its argument is not true +// assertは引数がfalseの場合、Exceptionを投げます -// These comparisons will always be true, even if the types aren't the same. +//これらの比較は型が違ったとしても、常に真です。 assert($a == $b); // equality assert($c != $a); // inequality assert($c <> $a); // alternative inequality @@ -214,32 +216,33 @@ assert($c > $b); assert($a <= $b); assert($c >= $d); -// The following will only be true if the values match and are the same type. +// 次の比較は値が等しく、かつ同じ型である場合のみ真です assert($c === $d); assert($a !== $d); assert(1 === '1'); assert(1 !== '1'); -// spaceship operator since PHP 7 +// spaceship演算子はPHP7から使用可能です $a = 100; $b = 1000; -echo $a <=> $a; // 0 since they are equal -echo $a <=> $b; // -1 since $a < $b -echo $b <=> $a; // 1 since $b > $a +echo $a <=> $a; // 等しいので0になります +echo $a <=> $b; // $a < $b なので -1 です +echo $b <=> $a; // $b > $a なので 1 です -// Variables can be converted between types, depending on their usage. +// 変数は使用するコンテキストによって、変換されます $integer = 1; echo $integer + $integer; // => 2 $string = '1'; -echo $string + $string; // => 2 (strings are coerced to integers) +echo $string + $string; // => 2 (文字列は強制的に数値として処理されます) $string = 'one'; echo $string + $string; // => 0 -// Outputs 0 because the + operator cannot cast the string 'one' to a number +// '+'演算子は文字列'one'を数値にキャストできないので、0と出力されます +// 型のキャスティングによって、変数を指定したもう一つの型として扱うことができます // Type casting can be used to treat a variable as another type $boolean = (boolean) 1; // => true @@ -247,15 +250,15 @@ $boolean = (boolean) 1; // => true $zero = 0; $boolean = (boolean) $zero; // => false -// There are also dedicated functions for casting most types +// 型をキャストするため専用の関数も存在します $integer = 5; $string = strval($integer); -$var = null; // Null value +$var = null; // Null値 /******************************** - * Control Structures + * 制御構造 */ if (true) { @@ -274,15 +277,15 @@ if (false) { print 'Does'; } -// ternary operator +// 参考演算子 print (false ? 'Does not get printed' : 'Does'); -// ternary shortcut operator since PHP 5.3 -// equivalent of "$x ? $x : 'Does'"" +// PHP 5.3から、三項演算子の短縮形が使用できます +// $x ? $x : 'Does'と同義です $x = false; print($x ?: 'Does'); -// null coalesce operator since php 7 +// null合体演算子はPHP 7から使用できます $a = null; $b = 'Does print'; echo $a ?? 'a is not set'; // prints 'a is not set' @@ -300,29 +303,29 @@ if ($x === '0') { -// This alternative syntax is useful for templates: +// :を用いる別の構文はテンプレートで有用です ?> -This is displayed if the test is truthy. +この部分はifが真のとき表示されます -This is displayed otherwise. +それ以外の場合は、この部分が表示されます Date: Sun, 17 Jan 2016 13:04:25 +0900 Subject: translate to 487 --- ja-jp/php.html.markdown | 64 +++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 31 deletions(-) (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown index 22c0561c..96d45f08 100644 --- a/ja-jp/php.html.markdown +++ b/ja-jp/php.html.markdown @@ -328,7 +328,7 @@ switch ($x) { //デフォルトで何かを実行します } -// While, do...while and for loops are probably familiar +// while, do, forの構文は、おそらく他の言語とも共通なものです $i = 0; while ($i < 5) { echo $i++; @@ -351,14 +351,14 @@ echo "\n"; $wheels = ['bicycle' => 2, 'car' => 4]; -// Foreach loops can iterate over arrays +//Foreachループによって、 配列を反復処理できます foreach ($wheels as $wheel_count) { echo $wheel_count; } // Prints "24" echo "\n"; -// You can iterate over the keys as well as the values +// 値と同じ様に、keyも反復処理できます foreach ($wheels as $vehicle => $wheel_count) { echo "A $vehicle has $wheel_count wheels"; } @@ -382,20 +382,20 @@ for ($i = 0; $i < 5; $i++) { /******************************** - * Functions + * 関数 */ -// Define a function with "function": +// 関数を"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 +function add ($x, $y = 1) { // $yはオプショナルな値であり、デフォルトで 1 です $result = $x + $y; return $result; } @@ -403,10 +403,10 @@ function add ($x, $y = 1) { // $y is optional and defaults to 1 echo add(4); // => 5 echo add(4, 2); // => 6 -// $result is not accessible outside the function -// print $result; // Gives a warning. +// $result には、関数の外からアクセス出来ません +// print $result; // エラーになります -// Since PHP 5.3 you can declare anonymous functions; +// PHP 5.3 から、無名関数が使えます $inc = function ($x) { return $x + 1; }; @@ -417,9 +417,9 @@ function foo ($x, $y, $z) { echo "$x - $y - $z"; } -// Functions can return functions +// 関数は、関数を返すことができます function bar ($x, $y) { - // Use 'use' to bring in outside variables + // 関数外の変数を利用したいときは、'use'を使います return function ($z) use ($x, $y) { foo($x, $y, $z); }; @@ -428,14 +428,15 @@ function bar ($x, $y) { $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 [, ... ]]); +// プログラミング中に、動的に動かす関数を決める場合に便利です。 +// もしくは、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) { @@ -450,38 +451,39 @@ function parameters() { parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World | /******************************** - * Includes + * ファイルの読み込み */ Date: Sun, 17 Jan 2016 16:18:48 +0900 Subject: translate all --- ja-jp/php.html.markdown | 118 +++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 57 deletions(-) (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown index 96d45f08..a5b48120 100644 --- a/ja-jp/php.html.markdown +++ b/ja-jp/php.html.markdown @@ -487,51 +487,52 @@ $value = include 'my-include.php'; /* */ /******************************** - * Classes + * クラス */ -// Classes are defined with the class keyword +// クラスはclassキーワードで定義します class MyClass { - const MY_CONST = 'value'; // A constant + const MY_CONST = 'value'; // クラス定数です 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 + protected $prot = 'protected'; // そのクラスと子クラスで参照可能 + private $priv = 'private'; // クラス内でのみアクセス可能 - // Create a constructor with __construct + // __constructでコンストラクターを生成します public function __construct($instanceProp) { - // Access instance variables with $this + // $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キーワードは関数の上書きを禁止します 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). + * クラスプロパティまたはメソッドをstaticとして作成すれば、 + * クラスをインスタンス化(newすること)しなくてもアクセスできます。 + * プロパティをstaticとして定義すると、 + * インスタンス化されたクラスオブジェクトを通してのアクセスはできなくなります。 */ public static function myStaticMethod() @@ -540,23 +541,23 @@ class MyClass } } -// 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 +// クラスをインスタンス化するには、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" +// extendsを使用してクラスを継承します。 class MyOtherClass extends MyClass { function printProtectedProperty() @@ -564,7 +565,7 @@ class MyOtherClass extends MyClass echo $this->prot; } - // Override a method + // メソッドを上書きします。 function myMethod() { parent::myMethod(); @@ -580,7 +581,7 @@ final class YouCannotExtendMe { } -// You can use "magic methods" to create getters and setters +// 「マジックメソッド」を使ってゲッターとセッターを生成できます。 class MyMapClass { private $property; @@ -597,12 +598,12 @@ class MyMapClass } $x = new MyMapClass(); -echo $x->property; // Will use the __get() method -$x->property = 'Something'; // Will use the __set() method +echo $x->property; // __get() メソッドを使用します +$x->property = 'Something'; // __set() メソッドを使用します -// Classes can be abstract (using the abstract keyword) or -// implement interfaces (using the implements keyword). -// An interface is declared with the interface keyword. +// クラスは抽象クラスにもできます(abstractキーワードを使用します)し、 +// インターフェースを実装することもできます(implementsキーワードを使用します)。 +// インターフェースはinterfaceキーワードで定義します。 interface InterfaceOne { @@ -614,7 +615,7 @@ interface InterfaceTwo public function doSomethingElse(); } -// interfaces can be extended +// インターフェースは継承することができます interface InterfaceThree extends InterfaceTwo { public function doAnotherContract(); @@ -639,7 +640,7 @@ class MyConcreteClass extends MyAbstractClass implements InterfaceTwo } -// Classes can implement more than one interface +// クラスは1つ以上のインターフェースを実装できます。 class SomeOtherClass implements InterfaceOne, InterfaceTwo { public function doSomething() @@ -655,10 +656,10 @@ class SomeOtherClass implements InterfaceOne, InterfaceTwo /******************************** - * Traits + * トレイト */ -// Traits are available from PHP 5.4.0 and are declared using "trait" +// トレイトはPHP 5.4.0 以上で使用可能で、traitキーワードで定義します。 trait MyTrait { @@ -678,39 +679,40 @@ $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 +// このセクションは名前空間の定義はファイルの先頭で宣言される必要があるため、 +// 独立しています。 +// そのケースには当てはまらないふりをして続けましょう。 Date: Sun, 17 Jan 2016 16:21:04 +0900 Subject: fix typo --- ja-jp/php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ja-jp') diff --git a/ja-jp/php.html.markdown b/ja-jp/php.html.markdown index a5b48120..b07dfb2f 100644 --- a/ja-jp/php.html.markdown +++ b/ja-jp/php.html.markdown @@ -277,7 +277,7 @@ if (false) { print 'Does'; } -// 参考演算子 +// 三項演算子 print (false ? 'Does not get printed' : 'Does'); // PHP 5.3から、三項演算子の短縮形が使用できます -- cgit v1.2.3 From 92beb9c37ca76d9ff0ea8b8c25cfe191accb6977 Mon Sep 17 00:00:00 2001 From: Tomy Date: Tue, 26 Jan 2016 19:53:14 +0900 Subject: feedback --- ja-jp/php-jp.html.markdown | 777 +++++++++++++++++++++++++++++++++++++++++++++ ja-jp/php.html.markdown | 776 -------------------------------------------- 2 files changed, 777 insertions(+), 776 deletions(-) create mode 100644 ja-jp/php-jp.html.markdown delete mode 100644 ja-jp/php.html.markdown (limited to 'ja-jp') diff --git a/ja-jp/php-jp.html.markdown b/ja-jp/php-jp.html.markdown new file mode 100644 index 00000000..2ca17179 --- /dev/null +++ b/ja-jp/php-jp.html.markdown @@ -0,0 +1,777 @@ +--- +language: PHP +contributors: + - ["Malcolm Fell", "http://emarref.net/"] + - ["Trismegiste", "https://github.com/Trismegiste"] +translators: + - ["Kazushige Tominaga", "https://github.com/kazu9su"] +filename: learnphp.php +lang: ja-jp +--- + +このドキュメントでは、 PHP 5+ について説明します。 + +```php + +Hello World Again! + 12 +$int2 = -12; // => -12 +$int3 = 012; // => 10 (先頭の0は8進法を示す) +$int4 = 0x0F; // => 15 (先頭の0xは16進法を示す) + +// floats(浮動小数) (別名double) +$float = 1.234; +$float = 1.2e3; +$float = 7E-10; + +// 変数の削除 +unset($int1); + +// 計算式 +$sum = 1 + 1; // 2 +$difference = 2 - 1; // 1 +$product = 2 * 2; // 4 +$quotient = 2 / 1; // 2 + +// 式の省略 +$number = 0; +$number += 1; // $numberに1加算Increment $number by 1 +echo $number++; // 1 がプリントされる(式の評価の後に加算される) +echo ++$number; // 3 がプリントされる(式の評価の前に加算される) +$number /= $float; // 割り算した結果の商を$numberに割り当てる + +// 文字列はシングルクォートで囲むのが望ましいです +$sgl_quotes = '$String'; // => '$String' + +// 文字列中に、他の変数を埋め込みたい場合以外は、ダブルクォートを使用するのはやめましょう +$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' + +// Special characters are only escaped in double quotes +// 特殊文字はダブルクォートによってのみ、エスケープされます +$escaped = "This contains a \t tab character."; +$unescaped = 'This just contains a slash and a t: \t'; + +// 必要があれば、変数を波括弧で囲みます +$money = "I have $${number} in the bank."; + +// PHP 5.3から、nowdocs形式が変数の挿入をしない複数行の文字列の定義に使用できます +$nowdoc = <<<'END' +Multi line +string +END; + +// ヒアドキュメント形式なら、文字列中に変数の挿入を行えます。 +$heredoc = << 1, 'Two' => 2, 'Three' => 3); + +// PHP 5.4 から、新しいシンタックスが導入されました +$associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; + +echo $associative['One']; // 1とプリントされます + +// キーを指定しないシンプルな配列にも、自動的に数値キーが振られます +$array = ['One', 'Two', 'Three']; +echo $array[0]; // => "One" + +// 配列の最後に要素を追加する +$array[] = 'Four'; +// または、次のようにも書けます +array_push($array, 'Five'); + +// 配列から要素を削除 +unset($array[3]); + +/******************************** + * 出力 + */ + +echo('Hello World!'); +// 標準出力にHello World! とプリントします +// 標準出力はブラウザーで実行していればWebページに出力されます +// Stdout is the web page if running in a browser. + +print('Hello World!'); // echoの結果と同じです + +// echo は言語自体の構成要素であり、括弧なしで呼び出せます +// echo is actually a language construct, so you can drop the parentheses. +echo 'Hello World!'; +print 'Hello World!'; // printも同様です + +$paragraph = 'paragraph'; + +echo 100; // スカラー数値を直接出力します +echo $paragraph; // 変数も使用できます + +// PHPタグの短縮型が設定されているか、使用しているPHPのバージョンが +// 5.4.0 以上であれば、短縮echoシンタックスを使用できます +?> +

+ 2 +echo $z; // => 2 +$y = 0; +echo $x; // => 2 +echo $z; // => 0 + +// 変数の型と値を標準出力へダンプします +var_dump($z); // int(0) と出力されます + +// 人間が読めるフォーマットで変数を標準出力にプリントします +print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three ) + +/******************************** + * ロジック + */ +$a = 0; +$b = '0'; +$c = '1'; +$d = '1'; + +// assertは引数がfalseの場合、Exceptionを投げます + +//これらの比較は型が違ったとしても、常に真です。 +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); + +// 次の比較は値が等しく、かつ同じ型である場合のみ真です +assert($c === $d); +assert($a !== $d); +assert(1 === '1'); +assert(1 !== '1'); + +// spaceship演算子はPHP7から使用可能です +$a = 100; +$b = 1000; + +echo $a <=> $a; // 等しいので0になります +echo $a <=> $b; // $a < $b なので -1 です +echo $b <=> $a; // $b > $a なので 1 です + +// 変数は使用するコンテキストによって、変換されます + +$integer = 1; +echo $integer + $integer; // => 2 + +$string = '1'; +echo $string + $string; // => 2 (文字列は強制的に数値として処理されます) + +$string = 'one'; +echo $string + $string; // => 0 +// '+'演算子は文字列'one'を数値にキャストできないので、0と出力されます + +// 型のキャスティングによって、変数を指定したもう一つの型として扱うことができます +// Type casting can be used to treat a variable as another type + +$boolean = (boolean) 1; // => true + +$zero = 0; +$boolean = (boolean) $zero; // => false + +// 型をキャストするため専用の関数も存在します +$integer = 5; +$string = strval($integer); + +$var = null; // Null値 + + +/******************************** + * 制御構造 + */ + +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'; +} + +// 三項演算子 +print (false ? 'Does not get printed' : 'Does'); + +// PHP 5.3から、三項演算子の短縮形が使用できます +// $x ? $x : 'Does'と同義です +$x = false; +print($x ?: 'Does'); + +// null合体演算子はPHP 7から使用できます +$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'; +} + + + +// :を用いる別の構文はテンプレートで有用です +?> + + +この部分はifが真のとき表示されます + +それ以外の場合は、この部分が表示されます + + + 2, 'car' => 4]; + +//Foreachループによって、 配列を反復処理できます +foreach ($wheels as $wheel_count) { + echo $wheel_count; +} // Prints "24" + +echo "\n"; + +// 値と同じ様に、keyも反復処理できます +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" + + +/******************************** + * 関数 + */ + +// 関数を"function"で定義します +function my_function () { + return 'Hello'; +} + +echo my_function(); // => "Hello" + +// 有効な関数名は、文字またはアンダースコアで始めます。それ以降は +// どれだけ長い文字、数値、アンダースコアを続けても構いません + +function add ($x, $y = 1) { // $yはオプショナルな値であり、デフォルトで 1 です + $result = $x + $y; + return $result; +} + +echo add(4); // => 5 +echo add(4, 2); // => 6 + +// $result には、関数の外からアクセス出来ません +// print $result; // エラーになります + +// PHP 5.3 から、無名関数が使えます +$inc = function ($x) { + return $x + 1; +}; + +echo $inc(2); // => 3 + +function foo ($x, $y, $z) { + echo "$x - $y - $z"; +} + +// 関数は、関数を返すことができます +function bar ($x, $y) { + // 関数外の変数を利用したいときは、'use'を使います + return function ($z) use ($x, $y) { + foo($x, $y, $z); + }; +} + +$bar = bar('A', 'B'); +$bar('C'); // Prints "A - B - C" + +// 文字列を使って、定義済みの関数を呼び出すことができます +$function_name = 'add'; +echo $function_name(1, 2); // => 3 + +// プログラミング中に、動的に動かす関数を決める場合に便利です。 +// もしくは、call_user_func(callable $callback [, $parameter [, ... ]]) を使っても同じことができます + + +// 特に指定しなくても、渡された引数を受け取ることもできます +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 | + +/******************************** + * ファイルの読み込み + */ + +instanceProp = $instanceProp; + } + + // メソッドはクラス内で関数として定義されます + public function myMethod() + { + print 'MyClass'; + } + + // finalキーワードは関数の上書きを禁止します + final function youCannotOverrideMe() + { + } + +/* + * クラスプロパティまたはメソッドをstaticとして作成すれば、 + * クラスをインスタンス化(newすること)しなくてもアクセスできます。 + * プロパティをstaticとして定義すると、 + * インスタンス化されたクラスオブジェクトを通してのアクセスはできなくなります。 + */ + + public static function myStaticMethod() + { + print 'I am static'; + } +} + +// クラス定数は、いつでも静的にアクセスできます。 +echo MyClass::MY_CONST; // Outputs 'value'; + +echo MyClass::$staticVar; // Outputs 'static'; +MyClass::myStaticMethod(); // Outputs 'I am static'; + +// クラスをインスタンス化するには、newを使います。 +$my_class = new MyClass('An instance property'); +// 括弧はもし引数を渡す必要がなければ省略可能です。 + +// ->を使ってクラスのメンバにアクセスします。 +echo $my_class->property; // => "public" +echo $my_class->instanceProp; // => "An instance property" +$my_class->myMethod(); // => "MyClass" + + +// extendsを使用してクラスを継承します。 +class MyOtherClass extends MyClass +{ + function printProtectedProperty() + { + echo $this->prot; + } + + // メソッドを上書きします。 + 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 +{ +} + +// 「マジックメソッド」を使ってゲッターとセッターを生成できます。 +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; // __get() メソッドを使用します +$x->property = 'Something'; // __set() メソッドを使用します + +// クラスは抽象クラスにもできます(abstractキーワードを使用します)し、 +// インターフェースを実装することもできます(implementsキーワードを使用します)。 +// インターフェースはinterfaceキーワードで定義します。 + +interface InterfaceOne +{ + public function doSomething(); +} + +interface InterfaceTwo +{ + public function doSomethingElse(); +} + +// インターフェースは継承することができます +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'; + } +} + + +// クラスは1つ以上のインターフェースを実装できます。 +class SomeOtherClass implements InterfaceOne, InterfaceTwo +{ + public function doSomething() + { + echo 'doSomething'; + } + + public function doSomethingElse() + { + echo 'doSomethingElse'; + } +} + + +/******************************** + * トレイト + */ + +// トレイトはPHP 5.4.0 以上で使用可能で、traitキーワードで定義します。 + +trait MyTrait +{ + public function myTraitMethod() + { + print 'I have MyTrait'; + } +} + +class MyTraitfulClass +{ + use MyTrait; +} + +$cls = new MyTraitfulClass(); +$cls->myTraitMethod(); // Prints "I have MyTrait" + + +/******************************** + * 名前空間 + */ + +// このセクションは名前空間の定義はファイルの先頭で宣言される必要があるため、 +// 独立しています。 +// そのケースには当てはまらないふりをして続けましょう。 + + -Hello World Again! - 12 -$int2 = -12; // => -12 -$int3 = 012; // => 10 (先頭の0は8進法を示す) -$int4 = 0x0F; // => 15 (先頭の0xは16進法を示す) - -// floats(浮動小数) (別名double) -$float = 1.234; -$float = 1.2e3; -$float = 7E-10; - -// 変数の削除 -unset($int1); - -// 計算式 -$sum = 1 + 1; // 2 -$difference = 2 - 1; // 1 -$product = 2 * 2; // 4 -$quotient = 2 / 1; // 2 - -// 式の省略 -$number = 0; -$number += 1; // $numberに1加算Increment $number by 1 -echo $number++; // 1 がプリントされる(式の評価の後に加算される) -echo ++$number; // 3 がプリントされる(式の評価の前に加算される) -$number /= $float; // 割り算した結果の商を$numberに割り当てる - -// 文字列はシングルクォートで囲むのが望ましいです -$sgl_quotes = '$String'; // => '$String' - -// 文字列中に、他の変数を埋め込みたい場合以外は、ダブルクォートを使用するのはやめましょう -$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' - -// Special characters are only escaped in double quotes -// 特殊文字はダブルクォートによってのみ、エスケープされます -$escaped = "This contains a \t tab character."; -$unescaped = 'This just contains a slash and a t: \t'; - -// 必要があれば、変数を波括弧で囲みます -$money = "I have $${number} in the bank."; - -// PHP 5.3から、nowdocs形式が変数の挿入をしない複数行の文字列の定義に使用できます -$nowdoc = <<<'END' -Multi line -string -END; - -// ヒアドキュメント形式なら、文字列中に変数の挿入を行えます。 -$heredoc = << 1, 'Two' => 2, 'Three' => 3); - -// PHP 5.4 から、新しいシンタックスが導入されました -$associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; - -echo $associative['One']; // 1とプリントされます - -// キーを指定しないシンプルな配列にも、自動的に数値キーが振られます -$array = ['One', 'Two', 'Three']; -echo $array[0]; // => "One" - -// 配列の最後に要素を追加する -$array[] = 'Four'; -// または、次のようにも書けます -array_push($array, 'Five'); - -// 配列から要素を削除 -unset($array[3]); - -/******************************** - * 出力 - */ - -echo('Hello World!'); -// 標準出力にHello World! とプリントします -// 標準出力はブラウザーで実行していればWebページに出力されます -// Stdout is the web page if running in a browser. - -print('Hello World!'); // echoの結果と同じです - -// echo は言語自体の構成要素であり、括弧なしで呼び出せます -// echo is actually a language construct, so you can drop the parentheses. -echo 'Hello World!'; -print 'Hello World!'; // printも同様です - -$paragraph = 'paragraph'; - -echo 100; // スカラー数値を直接出力します -echo $paragraph; // 変数も使用できます - -// PHPタグの短縮型が設定されているか、使用しているPHPのバージョンが -// 5.4.0 以上であれば、短縮echoシンタックスを使用できます -?> -

- 2 -echo $z; // => 2 -$y = 0; -echo $x; // => 2 -echo $z; // => 0 - -// 変数の型と値を標準出力へダンプします -var_dump($z); // int(0) と出力されます - -// 人間が読めるフォーマットで変数を標準出力にプリントします -print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three ) - -/******************************** - * ロジック - */ -$a = 0; -$b = '0'; -$c = '1'; -$d = '1'; - -// assertは引数がfalseの場合、Exceptionを投げます - -//これらの比較は型が違ったとしても、常に真です。 -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); - -// 次の比較は値が等しく、かつ同じ型である場合のみ真です -assert($c === $d); -assert($a !== $d); -assert(1 === '1'); -assert(1 !== '1'); - -// spaceship演算子はPHP7から使用可能です -$a = 100; -$b = 1000; - -echo $a <=> $a; // 等しいので0になります -echo $a <=> $b; // $a < $b なので -1 です -echo $b <=> $a; // $b > $a なので 1 です - -// 変数は使用するコンテキストによって、変換されます - -$integer = 1; -echo $integer + $integer; // => 2 - -$string = '1'; -echo $string + $string; // => 2 (文字列は強制的に数値として処理されます) - -$string = 'one'; -echo $string + $string; // => 0 -// '+'演算子は文字列'one'を数値にキャストできないので、0と出力されます - -// 型のキャスティングによって、変数を指定したもう一つの型として扱うことができます -// Type casting can be used to treat a variable as another type - -$boolean = (boolean) 1; // => true - -$zero = 0; -$boolean = (boolean) $zero; // => false - -// 型をキャストするため専用の関数も存在します -$integer = 5; -$string = strval($integer); - -$var = null; // Null値 - - -/******************************** - * 制御構造 - */ - -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'; -} - -// 三項演算子 -print (false ? 'Does not get printed' : 'Does'); - -// PHP 5.3から、三項演算子の短縮形が使用できます -// $x ? $x : 'Does'と同義です -$x = false; -print($x ?: 'Does'); - -// null合体演算子はPHP 7から使用できます -$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'; -} - - - -// :を用いる別の構文はテンプレートで有用です -?> - - -この部分はifが真のとき表示されます - -それ以外の場合は、この部分が表示されます - - - 2, 'car' => 4]; - -//Foreachループによって、 配列を反復処理できます -foreach ($wheels as $wheel_count) { - echo $wheel_count; -} // Prints "24" - -echo "\n"; - -// 値と同じ様に、keyも反復処理できます -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" - - -/******************************** - * 関数 - */ - -// 関数を"function"で定義します -function my_function () { - return 'Hello'; -} - -echo my_function(); // => "Hello" - -// 有効な関数名は、文字またはアンダースコアで始めます。それ以降は -// どれだけ長い文字、数値、アンダースコアを続けても構いません - -function add ($x, $y = 1) { // $yはオプショナルな値であり、デフォルトで 1 です - $result = $x + $y; - return $result; -} - -echo add(4); // => 5 -echo add(4, 2); // => 6 - -// $result には、関数の外からアクセス出来ません -// print $result; // エラーになります - -// PHP 5.3 から、無名関数が使えます -$inc = function ($x) { - return $x + 1; -}; - -echo $inc(2); // => 3 - -function foo ($x, $y, $z) { - echo "$x - $y - $z"; -} - -// 関数は、関数を返すことができます -function bar ($x, $y) { - // 関数外の変数を利用したいときは、'use'を使います - return function ($z) use ($x, $y) { - foo($x, $y, $z); - }; -} - -$bar = bar('A', 'B'); -$bar('C'); // Prints "A - B - C" - -// 文字列を使って、定義済みの関数を呼び出すことができます -$function_name = 'add'; -echo $function_name(1, 2); // => 3 - -// プログラミング中に、動的に動かす関数を決める場合に便利です。 -// もしくは、call_user_func(callable $callback [, $parameter [, ... ]]) を使っても同じことができます - - -// 特に指定しなくても、渡された引数を受け取ることもできます -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 | - -/******************************** - * ファイルの読み込み - */ - -instanceProp = $instanceProp; - } - - // メソッドはクラス内で関数として定義されます - public function myMethod() - { - print 'MyClass'; - } - - // finalキーワードは関数の上書きを禁止します - final function youCannotOverrideMe() - { - } - -/* - * クラスプロパティまたはメソッドをstaticとして作成すれば、 - * クラスをインスタンス化(newすること)しなくてもアクセスできます。 - * プロパティをstaticとして定義すると、 - * インスタンス化されたクラスオブジェクトを通してのアクセスはできなくなります。 - */ - - public static function myStaticMethod() - { - print 'I am static'; - } -} - -// クラス定数は、いつでも静的にアクセスできます。 -echo MyClass::MY_CONST; // Outputs 'value'; - -echo MyClass::$staticVar; // Outputs 'static'; -MyClass::myStaticMethod(); // Outputs 'I am static'; - -// クラスをインスタンス化するには、newを使います。 -$my_class = new MyClass('An instance property'); -// 括弧はもし引数を渡す必要がなければ省略可能です。 - -// ->を使ってクラスのメンバにアクセスします。 -echo $my_class->property; // => "public" -echo $my_class->instanceProp; // => "An instance property" -$my_class->myMethod(); // => "MyClass" - - -// extendsを使用してクラスを継承します。 -class MyOtherClass extends MyClass -{ - function printProtectedProperty() - { - echo $this->prot; - } - - // メソッドを上書きします。 - 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 -{ -} - -// 「マジックメソッド」を使ってゲッターとセッターを生成できます。 -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; // __get() メソッドを使用します -$x->property = 'Something'; // __set() メソッドを使用します - -// クラスは抽象クラスにもできます(abstractキーワードを使用します)し、 -// インターフェースを実装することもできます(implementsキーワードを使用します)。 -// インターフェースはinterfaceキーワードで定義します。 - -interface InterfaceOne -{ - public function doSomething(); -} - -interface InterfaceTwo -{ - public function doSomethingElse(); -} - -// インターフェースは継承することができます -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'; - } -} - - -// クラスは1つ以上のインターフェースを実装できます。 -class SomeOtherClass implements InterfaceOne, InterfaceTwo -{ - public function doSomething() - { - echo 'doSomething'; - } - - public function doSomethingElse() - { - echo 'doSomethingElse'; - } -} - - -/******************************** - * トレイト - */ - -// トレイトはPHP 5.4.0 以上で使用可能で、traitキーワードで定義します。 - -trait MyTrait -{ - public function myTraitMethod() - { - print 'I have MyTrait'; - } -} - -class MyTraitfulClass -{ - use MyTrait; -} - -$cls = new MyTraitfulClass(); -$cls->myTraitMethod(); // Prints "I have MyTrait" - - -/******************************** - * 名前空間 - */ - -// このセクションは名前空間の定義はファイルの先頭で宣言される必要があるため、 -// 独立しています。 -// そのケースには当てはまらないふりをして続けましょう。 - - Date: Tue, 26 Jan 2016 19:56:15 +0900 Subject: update filename --- ja-jp/php-jp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ja-jp') diff --git a/ja-jp/php-jp.html.markdown b/ja-jp/php-jp.html.markdown index 2ca17179..112916f4 100644 --- a/ja-jp/php-jp.html.markdown +++ b/ja-jp/php-jp.html.markdown @@ -5,7 +5,7 @@ contributors: - ["Trismegiste", "https://github.com/Trismegiste"] translators: - ["Kazushige Tominaga", "https://github.com/kazu9su"] -filename: learnphp.php +filename: learnphp-jp.php lang: ja-jp --- -- cgit v1.2.3 From 97cbeab8356fe72f487a2978ad22a82e4cddc128 Mon Sep 17 00:00:00 2001 From: Jacob Ward Date: Wed, 17 Feb 2016 22:23:30 -0700 Subject: [bash-jp/ja-jp] Corrected spelling. seperated -> separated --- ja-jp/bash-jp.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ja-jp') diff --git a/ja-jp/bash-jp.html.markdown b/ja-jp/bash-jp.html.markdown index 88e5ff1c..ea8fa49f 100644 --- a/ja-jp/bash-jp.html.markdown +++ b/ja-jp/bash-jp.html.markdown @@ -66,7 +66,7 @@ echo "Last program return value: $?" echo "Script's PID: $$" echo "Number of arguments: $#" echo "Scripts arguments: $@" -echo "Scripts arguments seperated in different variables: $1 $2..." +echo "Scripts arguments separated in different variables: $1 $2..." # 入力値の読み込み echo "What's your name?" @@ -117,7 +117,7 @@ echo "There are $(ls | wc -l) items here." echo "There are `ls | wc -l` items here." # BashはJavaやC++のように、case文による分岐ができます -case "$VARIABLE" in +case "$VARIABLE" in #分岐条件として使いたいパターンを並べてください 0) echo "There is a zero.";; 1) echo "There is a one.";; -- cgit v1.2.3