1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-11 09:14:39 +02:00

translate to 487

This commit is contained in:
Tomy
2016-01-17 13:04:25 +09:00
parent b5bdff8f9e
commit b8784faf28

View File

@@ -328,7 +328,7 @@ switch ($x) {
//デフォルトで何かを実行します //デフォルトで何かを実行します
} }
// While, do...while and for loops are probably familiar // while, do, forの構文は、おそらく他の言語とも共通なものです
$i = 0; $i = 0;
while ($i < 5) { while ($i < 5) {
echo $i++; echo $i++;
@@ -351,14 +351,14 @@ echo "\n";
$wheels = ['bicycle' => 2, 'car' => 4]; $wheels = ['bicycle' => 2, 'car' => 4];
// Foreach loops can iterate over arrays //Foreachループによって、 配列を反復処理できます
foreach ($wheels as $wheel_count) { foreach ($wheels as $wheel_count) {
echo $wheel_count; echo $wheel_count;
} // Prints "24" } // Prints "24"
echo "\n"; echo "\n";
// You can iterate over the keys as well as the values // 値と同じ様に、keyも反復処理できます
foreach ($wheels as $vehicle => $wheel_count) { foreach ($wheels as $vehicle => $wheel_count) {
echo "A $vehicle has $wheel_count wheels"; 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 () { function my_function () {
return 'Hello'; return 'Hello';
} }
echo my_function(); // => "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; $result = $x + $y;
return $result; 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); // => 5
echo add(4, 2); // => 6 echo add(4, 2); // => 6
// $result is not accessible outside the function // $result には、関数の外からアクセス出来ません
// print $result; // Gives a warning. // print $result; // エラーになります
// Since PHP 5.3 you can declare anonymous functions; // PHP 5.3 から、無名関数が使えます
$inc = function ($x) { $inc = function ($x) {
return $x + 1; return $x + 1;
}; };
@@ -417,9 +417,9 @@ function foo ($x, $y, $z) {
echo "$x - $y - $z"; echo "$x - $y - $z";
} }
// Functions can return functions // 関数は、関数を返すことができます
function bar ($x, $y) { function bar ($x, $y) {
// Use 'use' to bring in outside variables // 関数外の変数を利用したいときは、'use'を使います
return function ($z) use ($x, $y) { return function ($z) use ($x, $y) {
foo($x, $y, $z); foo($x, $y, $z);
}; };
@@ -428,14 +428,15 @@ function bar ($x, $y) {
$bar = bar('A', 'B'); $bar = bar('A', 'B');
$bar('C'); // Prints "A - B - C" $bar('C'); // Prints "A - B - C"
// You can call named functions using strings // 文字列を使って、定義済みの関数を呼び出すことができます
$function_name = 'add'; $function_name = 'add';
echo $function_name(1, 2); // => 3 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() { function parameters() {
$numargs = func_num_args(); $numargs = func_num_args();
if ($numargs > 0) { if ($numargs > 0) {
@@ -450,38 +451,39 @@ function parameters() {
parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World | parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |
/******************************** /********************************
* Includes * ファイルの読み込み
*/ */
<?php <?php
// PHP within included files must also begin with a PHP open tag. // 読み込まれたファイル中のPHPは、同じくPHPのオープンタグで始める必要があります
include 'my-file.php'; include 'my-file.php';
// The code in my-file.php is now available in the current scope. // my-file.php中のコードは、現在のスコープの中で使用可能です
// If the file cannot be included (e.g. file not found), a warning is emitted. // もしファイルを読み込めなければ (例:file not found)、警告が発せられます
include_once 'my-file.php'; include_once 'my-file.php';
// If the code in my-file.php has been included elsewhere, it will // my-file.phpのコードが既にどこかで読み込まれていれば、
// not be included again. This prevents multiple class declaration errors // ファイルを読み込みません。これは、クラスの多重定義のエラーを防ぎます
require 'my-file.php'; require 'my-file.php';
require_once 'my-file.php'; require_once 'my-file.php';
// Same as include(), except require() will cause a fatal error if the // include()と同じように、require()はもしファイルを読み込むことができなければ、
// file cannot be included. // 致命的エラーの原因となります
// Contents of my-include.php: // my-include.phpの内容
<?php <?php
return 'Anything you like.'; return 'Anything you like.';
// End file // End file
// Includes and requires may also return a value. // include()とrequire()は一つの値を返します
$value = include 'my-include.php'; $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 // ファイルパスを指定しない場合は、include_path の設定を利用します。
// the include_path, include will finally check in the calling script's // もしファイルがinclude_path中に見つからない場合は、
// own directory and the current working directory before failing. // 呼び出し元スクリプトのディレクトリと作業ディレクトリの中も探します。
// それでも見つからない場合、失敗します。
/* */ /* */
/******************************** /********************************