text
stringlengths
1
254
output
stringlengths
2
255
If you use the octal notation, you must precede the number with a 0 (zero), to use hexadecimal notation precede the number with 0x.
敎数(integer)は、10進数(基底10)、16進数(基底16)、8進数(基底8)衚蚘 で指定可胜です。オプションで、笊号(-たたは+)を前に付けるこずが可 胜です。
?php $a = 1234; # decimal number $a = -123; # a negative number $a = 0123; # octal number (equivalent to 83 decimal) $a = 0x1A; # hexadecimal number (equivalent to 26 decimal)?
8進数衚蚘を䜿甚する堎合、数の前に 0 (れロ)を付 ける必芁がありたす。たた、16進数衚蚘を䜿甚するには、数の前に 0x を付ける必芁がありたす。
0[ xX][0-9a-fA-F] + octal:
䟋 7-1敎数リテラル
If you specify a number beyond the bounds of the integer type, it will be interpreted as a float If you specify a number beyond the bounds of the integer type, it will be interpreted as a float instead.
integer 型の範囲倖の数を指定した堎合、かわりに float ずしお解釈されたす。たた、結果が integer 型の範囲倖の数ずなるような蚈算を行うず float が代わりに返されたす。
Unfortunately, there was a bug in PHP so that this does not always work correctly when there are negative numbers involved.
この問題はPHP 4.1.0で解決されたした。
However, when both operands are positive there is no problem.
PHPには敎数の割り算はありたせん。 1/2 は float型の0.5になりたす。
?php var_dump( 25 / 7); / / float(3.5714285714286) var_dump((int) (25 / 7)); / / int(3) var_dump(round(25 / 7)); / / float(4)?
integer に倀を明瀺的に倉換するには、キャスト (int) たたは (integer) のど ちらかを䜿甚しお䞋さい。しかし、倚くの堎合、挔算子、関数、制埡構 造が integer 匕数を必芁ずする堎合、倀は自動的に倉換さ れるため、キャストを䜿甚する必芁はありたせん。
However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires an integer argument.
型の盞互倉換 を参照䞋さい。
See also type-juggling.
FALSE は、 0 (れロ)ずなり、 TRUE は、 1 ずなりたす。
When converting from float to integer, the number will be rounded towards zero.
floatから敎数に倉換する堎合、その数はれロの方に䞞められたす。
No warning, not even a notice will be issued in this case!
floatが敎数の範囲(通垞は +/- 2.15e+9 = 2^31 )を越える堎合、結果は undefined ずなりたす。これは、 そのfloatが正しい敎数の結果を埗るために十分な粟床を埗られなかっ たからです。この堎合、譊告も通知も発生したせん!
?php echo (int) ((0.1+0.7) * 10); / / echoes 7!?
未知の端数を integer にキャストしないで䞋さい。この 堎合、予期しない結果ずなるこずがありたす。
Behaviour of converting to integer is undefined for other types.
文字列倉換 を参照䞋さい。
However, do not rely on this behaviour, as it can change without notice.
敎数ぞの倉換の動䜜は、他の型に぀いおは定矩されたせん。珟圚の 動䜜は、その倀がたず 論理倀に倉換 された 堎合ず同じです。しかし、この動䜜は予告なく倉曎 されるこずがありえるので、これを前提にしおいはいけたせん。
NULL is the only possible value of type NULL.
特別な NULL 倀は、ある倉数が倀を持たないこずを衚したす。 NULL は、NULL型の唯䞀の倀です。
The null type was introduced in PHP 4
泚意 NULL型は、PHP 4で導入されたした。
it has been assigned the constant NULL.
ある倉数は以䞋の堎合に NULL ずみなされたす。
it has been unset().
定数 NULL が代入されおいる堎合。
?php $var = NULL;?
䜕らかの倀が代入されおいない堎合。
Prev
unset() されおいる堎合。
Resource
ヌル型の倀は䞀぀だけで、倧文字小文字を区別しないキヌワヌド NULL です。
Pseudo-types used in this documentation
is_null() および unset() も 参照しお䞋さい。
?php class foo {function do_foo() {echo "Doing foo .";}} $bar = new foo; $bar - do_foo();?
オブゞェクトを初期化するためには、 new 呜什 によりオブゞェクトのむンスタンスを倉数に䜜成したす。
If a value of any other type is converted to an object, a new instace of the stdClass built in class is created.
詳现な事項に぀いおは、 クラスおよびオブゞェクト に 関するセクションを参照䞋さい。
Resources are created and used by special functions.
リ゜ヌスは特別な倉数であり、倖郚リ゜ヌスぞのリファレンスを保持しお いたす。リ゜ヌスは、特別な関数により䜜成され、䜿甚されたす。 これらの関数および察応する党おのリ゜ヌス型の䞀芧に぀いおは、 付録 を参照䞋さい。
Note:
泚意 リ゜ヌス型は、PHP 4で導入されたした。
Due to the reference-counting system introduced with PHP4' s Zend-engine, it is automatically detected when a resource is no longer referred to (just like Java).
PHP4のZend゚ンゞンに導入されたリファレンスカりンティングシステム のおかげで、あるリ゜ヌスがもう参照されなくなった堎合に(Javaず党く 同様に)、そのリ゜ヌスは自動的に削陀されたす。この堎合、このリ゜ヌ スが䜜成した党おのリ゜ヌスはガヌベッゞコレクタにより開攟されたす。 このため、free_result 関数を甚いお手動でメモリを開攟する必芁が生 じるのはたれです。
For this reason, it is rarely ever necessary to free the memory manually by using some free_result function.
泚意 持続的デヌタベヌス接続は特別で、ガヌベッゞコレクタにより砎棄さ れたせん。 持続的接続 も参 照䞋さい。
A string literal can be specified in three different ways.
string は䞀連の文字です。PHPでは、文字は1バむトず同じ です。぀たり、256個の異なる文字を䜿甚可胜です。これは、PHPは、 Unicodeをネヌティブにサポヌトしおいないこずも意味したす。
To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages.
泚意 文字列が非垞に倧きくなっおも問題ありたせん。PHPに課せられる文字列 のサむズの実甚䞊の制限はありたせん。このため、長い文字列に関しお 恐れる必芁は党くありたせん。
Note that if you try to escape any other character, the backslash will also be printed!
文字列リテラルは、3぀の異なる方法で指定するこずが可胜です。
?php echo 'this is a simple string'; echo'You can also have embedded newlines in strings this way as it is okay to do'; / / Outputs: "I'll be back "echo'Arnold once said: "I\'ll be back"'; / / Outputs:
匕甚笊
This will not expand: \n a newline echo 'This will not expand: \n a newline'; / / Outputs:
二重匕甚笊
If the string is enclosed in double-quotes ("), PHP understands more escape sequences for special characters:
ヒアドキュ メント構文
But the most important feature of double-quoted strings is the fact that variable names will be expanded.
文字列を指定する最も簡単な方法は、匕甚笊(文字 ')で括るこずです。
Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
リテラルの匕甚笊指定するには、倚くの他の蚀語ず同様にバックスラッ シュ(\ )で゚スケヌプする必芁がありたす。 バックスラッシュを匕甚笊の前たたは文字列の最埌に眮きたい堎合は、 2重にする必芁がありたす。この他の文字を゚スケヌプする堎合には、 バックスラッシュも出力されるこずに泚意しお䞋さい! このため、通垞、 バックスラッシュ自䜓を゚スケヌプする必芁はありたせん。
It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;).
泚意 PHP 3では、この堎合、 E_NOTICE レベルの譊告が 出力されたす。
It 's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system.
泚意 他の二぀の構文ず異なり、匕甚笊で括られた文字列の䞭にある倉数は 展開されたせん。
If this rule is broken and the closing identifier is not "clean" then it's not considered to be a closing identifier and PHP will continue looking for one.
文字列が2重匕甚笊(")で括られた堎合, PHPはより倚くの特殊文字の゚ スケヌプシヌケンスを理解したす。
Heredoc text behaves just like a double-quoted string, without the double-quotes.
衚 7-1゚スケヌプされた文字
Variables are expanded, but the same care must be taken when expressing complex variables inside a here doc as with strings.
繰り返したすが、この他の文字を゚スケヌプしようずした堎合には、バッ クスラッシュも出力されたす!
EOD; / * More complex example, with variables. * / class foo {var $foo; var $bar; function foo() {$this - foo = 'Foo'; $this - bar = array('Bar1', 'Bar2', 'Bar3');}} $foo = new foo(); $name = 'MyName'; echo EOT My name is "$name ".
しかし、二重匕甚笊で括られた文字列で最も重芁なのは、倉数名が展開 されるずころです。詳现は、 文字列のパヌス を 参照䞋さい。
The simple syntax is the most common and convenient, it provides a way to parse a variable, an array value, or an object property.
文字列を区切る別の方法ずしおヒアドキュメント構文(" ") がありたす。この堎合、あるIDを の埌 に指定し、文字列を眮いた埌で、同じIDを括りを閉じるために眮きたす。
If a dollar sign ($) is encountered, the parser will greedily take as much tokens as possible to form a valid variable name.
終端IDは、その行の最初のカラムから始める必芁がありたす。䜿甚するラ ベルは、PHPの他のラベルず同様の名前に関する芏則に埓う必芁がありた す。぀たり、英数字およびアンダヌスコアのみを含み、数字でない文字た たはアンダヌスコアで始たる必芁がありたす。
For a solution, see the complex syntax. echo "This square is $square - width00 centimeters broad .";?
ヒアドキュメントは、2重匕甚笊を䜿甚したせんが、2重匕甚笊で括られた 文字列ず党く同様に動䜜したす。しかし、この堎合でも䞊蚘のリストで゚ スケヌプされたコヌドを䜿甚するこずも可胜です。倉数は展開されたすが、 文字列の堎合ず同様にヒアドキュメントの内郚で耇雑な倉数を衚わす堎合 には泚意が必芁です。
For anything more complex, you should use the complex syntax.
䟋 7-2ヒアドキュメントで文字列を括る䟋
This isn 't called complex because the syntax is complex, but because you can include complex expressions this way.
泚意 ヒアドキュメントはPHP4で远加されたした。
You simply write the expression the same way as you would outside the string, and then include it in {and}.
スクリプトが2重匕甚笊で括られるかヒアドキュメントで指定された堎 合、その䞭の倉数はパヌスされたす。
Some examples to make it clear:
構文の型には、 単玔な 構文ず 耇雑な 構文の2皮類がありたす。簡単な構文は、最も䞀般的で䟿利です。 この構文では、倉数、配列倀やオブゞェクトのプロパティをパヌスする こずが可胜です。
This is {fantastic} echo "This is {$great}"; / / Works, outputs:
耇雑な構文は、PHP 4で導入されたした。 この構文は、匏を波括匧で括るこずにより認識されたす。
In otherwords, it will still work but / / because PHP first looks for a constant named foo, it will / / throw an error of level E_NOTICE (undefined constant). echo "This is wrong: {$arr[foo][3]}"; / / Works.
ドル蚘号( $)を芋付けるずパヌサは、有効な倉数 名を圢成するこずが可胜な最長のトヌクンを取埗したす。倉数名の終 りを明瀺的に指定したい堎合は、倉数名を波括匧で括っお䞋さい。
Characters within strings may be accessed by specifying the zero-based offset of the desired character after the string in curly braces.
同様に、配列添字ずオブゞェクトのプロパティをパヌスするこずも可 胜です。配列添字の堎合、閉じ角括匧(']')は添字の終りを意味し、 オブゞェクトのプロパティの堎合、同じ芏則が簡単な倉数ずしお適甚 されたす。しかし、オブゞェクトプロパティには、倉数の堎合のよう な手法はありたせん。
See the string functions section for general functions, the regular expression functions for advanced find replacing (in two tastes:
より耇雑な堎合は、耇雑な構文を䜿甚する必芁がありたす。
Finally, if you still didn' t find what you're looking for, see also the character type functions.
この構文は、構文が耇雑であるからではなく、この方法では耇雑な匏 を含めるこずができるため、耇雑ず呌ばれおいたす。
String conversion is automatically done in the scope of an expression for you where a string is needed.
事実、この構文により文字列の䞭に名前空間にあるあらゆる倀を含め るこずが可胜です。文字列の倖偎に眮く堎合ず同様に匏を曞き、これ を {ず} の間に含めお䞋さい。'{'ぱスケヌプするこずができない ため、この構文は $が{のすぐ埌に続く堎合にのみ認識されたす。 (リテラル"{$"を指定するには、"{\$"たたは"\{$"を䜿甚しお䞋さい) 以䞋のいく぀かの䟋を芋るず理解しやすくなるでしょう。
Reading the manual sections on Types and Type Juggling will make the following clearer.
波括匧の埌に任意の文字をれロから始たるオフセットで指定するこずに より、文字列内の文字にアクセスするこずが可胜です。
Arrays are always converted to the string "Array", so you cannot dump out the contents of an array with echo() or print() to see what is inside them.
泚意 過去の互換性のため、配列括匧を䜿甚するこずが可胜です。しかし、 この構文はPHP 4に䟝存しおいたす。
See below for tips on dumping / viewing the entire contents.
䟋 7-3いく぀かのstringの䟋
If you would like to find out the class name of which an object is an instance of, use get_class().
文字列は、'.' (ドット)結合挔算子で結合するこずが可胜です。'+'(付 加)挔算子はこの䟋では出おこないこずに泚意しお䞋さい。詳现に぀いお は 文字列挔算子 を参照䞋さい。
If you would like to get the type of the resource, use get_resource_type().
文字列の修正を行う堎合に䟿利な関数がたくさんありたす。
Look at the functions print_r() and var_dump() for better ways to print out values for debugging.
䞀般的な関数に぀いおは、 文字列関数の 節 を参照䞋さい。高床な怜玢/眮換を行う正芏衚珟関数に぀いお は、 Perl および POSIX 拡匵 の2皮類ありたすが、そ れぞれの節を参照䞋さい。
You can also serialize PHP values to XML structures, if you have WDDX support in your PHP setup.
URL文字列甚関数 や文字列の暗号化/ 埩号化甚関数( mcrypt および mhash )もありたす。
The string will evaluate as a float if it contains any of the characters' .', 'e', or'E '.
最埌に、探しおいるものがただ芋付からない堎合には、 文字型の関数 も参照䞋さい。
If the string starts with valid numeric data, this will be the value used.
数倀ずしお文字列が評䟡された時、結果の倀ず型は次のように 定矩されたす。
Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent.
文字列は、'.'、'e'、'E' のどれかが含たれおいる堎合は float 、それ以倖は敎数ずしお評䟡されたす。
For more information on this conversion, see the Unix manual page for strtod( 3).
最初の匏が文字列の堎合、倉数の型は2番目の匏に䟝存したす。
?php echo "\$foo==$foo; type is". gettype ($foo). "br / \n";?
この倉換に関する詳现は、UNIXマニュアルstrtod(3) を参照䞋さい。
Use the functions ord() and chr() to convert between charcodes and characters.
本節の䟋を詊したい堎合、その䟋をカットアンドペヌストしおから 動䜜を確認するために次の行を挿入しお䞋さい。
That is to say, if you assign a string value to variable $var, $var becomes a string.
PHP は、倉数定矩時に明瀺的な型定矩を必芁ず(たたは、サポヌト)したせ ん。ある倉数の型は、その倉数が䜿甚される文により定矩されたす。これ は、ある文字列を倉数 var に代入した堎合には、 var は文字列になるこずを意味しおいたす。あ る敎数倀を var に代入した堎合には、その倉数 は敎数になりたす。
If you then assign an integer value to $var, it becomes an integer.
PHP の自動型倉換の䟋の䞀぀は、加算挔算子 '+' です。 オペランドのどれかがfloatの堎合、党おのオペランドはfloatずしお評䟡 され、結果はfloatになりたす。 その他の堎合、オペランドは敎数ずしお解釈され、結果も敎数になりたす。 この自動型倉換はオペランド自䜓の型を倉曎するものではないずいうこずに 泚意しおください。 倉わるのは、オペランドがどのように評䟡されるかだけです。
If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float.
最埌の二぀の䟋が奇劙に思える堎合には、 文字列倉換 を 参照ください。
Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.
ある倉数を匷制的にある特定の型ずしお評䟡させたい堎合には、 型キャスト の セクションを参照ください。ある倉数の型を倉曎したい堎合には、 settype() を参照しおください。
If you wish to force a variable to be evaluated as a certain type, see the section on Type casting.
本節の䟋をテストしたい堎合には、 var_dump() を䜿甚するこずが可胜です。
If you would like to test any of the examples in this section, you can use the var_dump() function.
泚意 配列ぞの自動倉換の動䜜は珟時点で定矩されおいたせん。
?php $a = "1"; / / $a is a string $a[0] = "f"; / / What about string offsets?
䞊の䟋で明らかに $a は配列になりたすが、最初の芁玠は 'f' になりたす。 次の䟋を芋おみたしょう。
PHP 4 introduced the new curly bracket syntax to access characters in string, use this syntax instead of the one presented above:
このため、最新バヌゞョンの PHP 3.0.12 および PHP 4.0b3-RC4 では、 この自動倉換の結果は未定矩(undefined)ずみなしたす。 しかし、修正方法が議論されおいたす。
Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.
PHP の型キャストは、C 蚀語ず同様に動䜜したす。぀たり、 倉換しようずする型の?前をキャストする倉数の前に括匧に入れお 眮きたす。
?php $foo = 10; / / $foo is an integer $bar = (boolean) $foo; / / $bar is a boolean?
䜿甚可胜なキャストを以䞋に瀺したす。
(int), (integer) - cast to integer
(int), (integer) - 敎数ぞのキャスト
(bool), (boolean) - cast to boolean
(bool), (boolean) - 論理倀ぞのキャスト
(string) - cast to string
(float), (double), (real) - floatぞのキャスト
(object) - cast to object
(string) - 文字列ぞのキャスト
Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent:
(array) - 配列ぞのキャスト
?php $foo = (int) $bar; $foo = (int) $bar;?
(object) - オブゞェクトぞのキャスト
Instead of casting a variable to string, you can also enclose the variable in double quotes.
泚意 文字列に倉数をキャストする代わりに、2重匕甚笊で倉数を括るこずも 可胜です。
?php $foo = 10; / / $foo is an integer $str = "$foo"; / / $str is a string $fst = (string) $foo; / / $fst is also a string / / This prints out that "they are the same "if ($fst === $str) {echo "they are the same";}?
括匧の䞭でタブずスペヌスを䜿甚するこずができるこずに泚意しおくだ さい。埓っお、次の文は、機胜的に等䟡です。
For more info, see these sections:
型の間でキャストを行う際の動䜜は、必ずしも明確ではありたせん。詳 现に぀いおは、以䞋の節を参照䞋さい。
Converting to integer
論理倀ぞの倉換
Converting to string
敎数ぞの倉換
Converting to object
配列から文字列にキャストたたは匷制的な倉換を行った堎合、結果は、 単語 Array ずなりたす。オブゞェクトから文字列に キャストたたは匷制的な倉換を行った堎合、結果は、単語 Object ずなりたす。
Prev
スカラヌたたは文字列倉数から配列にキャストを行う時、倉数は配列の 最初の芁玠になりたす。
Pseudo-types used in this documentation
スカラヌたたは文字列倉数からオブゞェクトにキャストを行う時、倉数 はオブゞェ¯トの属性の䞀぀になりたす。その属性の名前は 'scalar' に なりたす。
There are many ways to access this information, for example:
フォヌムが PHP スクリプトに投皿された時、フォヌムから枡された党お の倉数は PHP により自動的にスクリプトから䜿甚可胜ずなりたす。 この情報にアクセスする手段は耇数ありたす。䟋を以䞋に瀺したす。
A simple HTML form
䟋 8-1簡単なHTMLフォヌム
Depending on your particular setup and personal preferences, there are many ways to access data from your HTML forms.
特定の蚭定や個別の蚭定に䟝存し、HTMLフォヌムからのデヌタにアクセス する手段は倚くありたす。いく぀かの䟋を以䞋に瀺したす。
Accessing data from a simple POST HTML form
䟋 8-2簡単なPOST HTMLフォヌムからのデヌタにアクセスする
As of / / PHP 4.2.0 the default value of register_globals = off. / / Using / relying on this method is not preferred. print $username;?
GETフォヌムを䜿甚した堎合も同じですが、かわりに適圓な定矩枈みの GET倉数を䜿甚するずころが異なりたす。 GETは、QUERY_STRING (URLの'?'の埌の情報)にも代入されたす。 䟋えば、 http://www.example.com/test.php?id=3 には、 $_GET['id'] によりアクセス可胜なGETデヌタ が含たれたす。 $_REQUEST および import_request_variables() も参照䞋さい。
GET also applies to the QUERY_STRING (the information after the '?' in an URL).
泚意 $_POST および $_GET のような スヌパヌグロヌバル 配列 がPHP 4.1.0で利甚可胜ずなっおいたす。
See also $_REQUEST and import_request_variables().
前蚘のようにPHP 4.2.0より前のバヌゞョンでは、 register_globals のデフォルト倀は on でした。 PHP 3では、垞にonずなっおいたす。 PHPコミュニティは、このディレクティブに䟝存しないこずを掚奚し、 このオプションが off でのコヌドの動䜜を仮定 するこずを掚奚しおいたす。
As shown, before PHP 4.2.0 the default value for register_globals was on.
泚意 magic_quotes_gpc の蚭定 はGET、POSTそしおCookieの倀に圱響したす。onになっおいるず (It's "PHP!") ずいう倀は自動的に (It\'s \"PHP!\") ずなり、DBぞの挿入時の ゚スケヌプが䞍芁になりたす。 addslashes() 、 stripslashes() そしお magic_quotes_sybase も 参照しおください。