PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

論理型 (boolean)> <コメント
Last updated: Fri, 14 Nov 2008

view this page in

目次

導入

PHP は、8 種類の基本型をサポートします。

4 種類のスカラー型:

  • 論理値 (boolean)
  • 整数 (integer)
  • float (浮動小数点数, double も同じ)
  • 文字列 (string)

2 種類の複合型:

  • 配列 (array)
  • オブジェクト (object)

そして、最後に 2 種類の特別な型:

  • リソース (resource)
  • ヌル (NULL)

本マニュアルでは、可読性を向上させるため、以下のような擬似的な型も使用します。

そして擬似変数 $...

マニュアル内のいくつかの場所で "double" 型という記述が残っているかもしれません。 double は float と同じものだと考えてください。2 種類の名前が存在するのは、 歴史的な理由によるものです。

変数の型は、基本的にプログラマが設定するものではありません。 その変数が使用される文脈に応じ、PHP が実行時に決定します。

注意: の型と値を正確に知りたい場合は、 var_dump() 関数を使用してください。 デバッグのために、単純に人間が読みやすい形で型を表示したい場合には gettype() を使用してください。型をチェックする場合には gettype() を使用してはいけません is_type 関数を使用してください。いくつかの例を以下に示します。

<?php
$a_bool 
TRUE;   // a boolean
$a_str  "foo";  // a string
$a_str2 'foo';  // a string
$an_int 12;     // an integer

echo gettype($a_bool); // prints out:  boolean
echo gettype($a_str);  // prints out:  string

// 数値であれば、4を足す
if (is_int($an_int)) {
    
$an_int += 4;
}

// $bool が文字列であれば, それをprintする
// (そうでなければ何も出力されない)
if (is_string($a_bool)) {
    echo 
"String: $a_bool";
}
?>

ある変数の型を強制的に他の型に変換したい場合、変数を キャスト するか、 settype() 関数を使用します。

変数は、その型に依存して異なった動作をする場合があることに注意してください。 詳細な情報については、 型の変換 のセクションを参照ください。 またPHP 型の比較表 もご覧ください。さまざまな型の変数の比較に関する例があります。



論理型 (boolean)> <コメント
Last updated: Fri, 14 Nov 2008
 
add a note add a note User Contributed Notes
Jeffrey
26-Oct-2008 07:31
The Object (compound) Type

Like every programming language, PHP offers the usual basic primitive types which can hold only one piece of data at a time (scalar). I am particularly fond of the "object" type (compound) because that allows me to group many basic PHP types together, and I can name it anything I want.

<?php
class Person
{
 
$firstName;                   // a PHP String
 
$middleName;                  // a PHP String
 
$lastName;                    // a PHP String
 
$age;                         // a PHP Integer
 
$hasDriversLicense;           // a PHP Boolean
}
?>

Here, I have grouped several basic PHP types together, (3) Strings, (1) Integer, and (1) Boolean... then I named that group "Person". Since I used the proper syntax to do so, this code is pure PHP, which means that if you run this code, you would have an extra PHP "type" available to you in your scripts, like so:

<?php
$myAge
= 16;                    // a PHP Integer - always available
$yourAge = 15.5;                // a PHP Float   - always available
$hasHair = true;                // a PHP Boolean - always available
$greeting = "Hello World!"      // a PHP String  - always available

$person = new Person();         // a PHP Person  - available NOW!
?>

You can make your own object types and have PHP execute it as if it were part of the PHP language itself. See more on classes and objects in this manual at: http://www.php.net/manual/en/language.oop5.php
jonah_whalehosting_ca
12-Apr-2007 07:33
In reply to Philip, form data could also be an array.  so there are two types you can expect from $_REQUEST (and it's associates): string and array.
arjini at gmail dot com
06-Dec-2005 09:32
Note that you can chain type castng:

var_dump((string)(int)false); //string(1) "0"
shahnaz khan
18-Mar-2005 01:40
if we use gettype() before initializinf any variable it give NULL
for eg.

<?php
$foo
;
echo
gettype($foo);
?>

it will show

NULL
Trizor of www.freedom-uplink.org
30-Jun-2004 03:14
The differance of float and double dates back to a FORTRAN standard. In FORTRAN Variables aren't as loosly written as in PHP and you had to define variable types(OH NOES!). FLOAT or REAL*4 (For all you VAX people out there) defined the variable as a standard precision floating point, with 4 bytes of memory allocated to it. DOUBLE PRECISION or REAL*8 (Again for the VAX) was identical to FLOAT or REAL*4, but with an 8 byte allocation of memory instead of a 4 byte allocation.

In fact most modern variable types date back to FORTRAN, except a string was called a CHARACHTER*N and you had to specify the length, or CHARACHTER*(*) for a variable length string. Boolean was LOGICAL, and there weren't yet objects, and there was support for complex numbers(a+bi).

Of course, most people reading this are web programmers and could care less about the mathematical background of programming.

NOTE: Object support was added to FORTRAN in the FORTRAN90 spec, and expanded with the FORTRAN94 spec, but by then C was the powerful force on the block, and most people who still use FORTRAN use the FORTRAN77.

論理型 (boolean)> <コメント
Last updated: Fri, 14 Nov 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites