第3章 データ型
3.1 プリミティブ型
数値型
| 型 | PHP 8.x | TypeScript 5.x | VB.NET 4.8.1 |
|---|---|---|---|
| 整数 | int |
number |
Byte, Short, Integer, Long
|
| 浮動小数点 | float |
number |
Single, Double, Decimal
|
| 任意精度 | GMP/BCMath | bigint |
BigInteger(System.Numerics) |
PHP
<?php
$integer = 42;
$float = 3.14;
$hex = 0x1A;
$octal = 0o755;
$binary = 0b1010;
$scientific = 1.2e3;
// 数値区切り(PHP 7.4+)
$million = 1_000_000;
TypeScript
const integer = 42;
const float = 3.14;
const hex = 0x1A;
const octal = 0o755;
const binary = 0b1010;
const scientific = 1.2e3;
// bigint
const big: bigint = 9007199254740991n;
// 数値区切り
const million = 1_000_000;
VB.NET
Dim b As Byte = 255 ' 0 to 255
Dim s As Short = 32767 ' -32,768 to 32,767
Dim i As Integer = 2147483647 ' -2^31 to 2^31-1
Dim l As Long = 9223372036854775807L
Dim f As Single = 3.14F
Dim d As Double = 3.14159265359
Dim dec As Decimal = 3.14159265359D
Dim hex As Integer = &H1A
Dim octal As Integer = &O755
Dim binary As Integer = &B1010
文字列型
| 言語 | 型名 | 特徴 |
|---|---|---|
| PHP | string |
バイト列、マルチバイト関数で Unicode 対応 |
| TypeScript | string |
UTF-16 |
| VB.NET | String |
UTF-16、不変(immutable) |
PHP
<?php
$single = 'Hello'; // シングルクォート(変数展開なし)
$double = "Hello, $name"; // ダブルクォート(変数展開あり)
$heredoc = <<<EOT
複数行
文字列
EOT;
$nowdoc = <<<'EOT'
変数展開しない
複数行文字列
EOT;
TypeScript
const single = 'Hello';
const double = "Hello";
const template = `Hello, ${name}`; // テンプレートリテラル
const multiline = `
複数行
文字列
`;
VB.NET
Dim s As String = "Hello"
Dim withQuote As String = "He said ""Hello""" ' エスケープは二重引用符
' 文字列補間(VB 14+)
Dim name As String = "World"
Dim greeting As String = $"Hello, {name}"
' 複数行(暗黙的な行継続)
Dim multiline As String = "Line 1" & vbCrLf &
"Line 2" & vbCrLf &
"Line 3"
真偽値型
| 言語 | 型名 | 真値 | 偽値 |
|---|---|---|---|
| PHP | bool |
true |
false |
| TypeScript | boolean |
true |
false |
| VB.NET | Boolean |
True |
False |
型変換時の真偽判定
| 値 | PHP | TypeScript | VB.NET |
|---|---|---|---|
0 |
false |
false(条件式) |
False |
"" |
false |
false(条件式) |
N/A(型変換エラー) |
"0" |
false |
true |
N/A |
[] / 空配列 |
false |
true |
N/A |
null |
false |
false(条件式) |
N/A |
3.2 null/undefined/Nothing の扱い
| 言語 | null相当 | undefined相当 | 備考 |
|---|---|---|---|
| PHP | null |
なし | 型は null
|
| TypeScript | null |
undefined |
別の型として扱う |
| VB.NET | Nothing |
なし | 参照型のデフォルト値 |
PHP
<?php
$value = null;
// null チェック
if ($value === null) {
echo "null です";
}
// isset vs is_null
$arr = ['key' => null];
isset($arr['key']); // false(nullでも存在しても)
is_null($arr['key']); // true
array_key_exists('key', $arr); // true
TypeScript
let value: string | null = null;
let optional: string | undefined = undefined;
// null チェック
if (value === null) {
console.log("null です");
}
// undefined チェック
if (optional === undefined) {
console.log("undefined です");
}
// どちらでもない(nullish)チェック
if (value == null) { // null または undefined
console.log("nullish です");
}
VB.NET
Dim refType As String = Nothing ' 参照型のデフォルト
Dim valueType As Integer? = Nothing ' Nullable値型
' Nothing チェック
If refType Is Nothing Then
Console.WriteLine("Nothing です")
End If
' Nullable値型のチェック
If valueType.HasValue Then
Console.WriteLine(valueType.Value)
End If
3.3 型推論
PHP(型推論なし)
<?php
// PHP には型推論がない
// 型は実行時に決定される
$value = 42; // 実行時に int
$value = "hello"; // 実行時に string に変更可能
TypeScript
// 変数宣言時の型推論
const num = 42; // number と推論
const str = "hello"; // string と推論
const arr = [1, 2, 3]; // number[] と推論
// 関数の戻り値推論
function add(a: number, b: number) { // 戻り値は number と推論
return a + b;
}
// 複雑な推論
const mixed = [1, "two", 3]; // (string | number)[] と推論
VB.NET(Option Infer On)
Option Infer On
Dim num = 42 ' Integer と推論
Dim str = "hello" ' String と推論
Dim arr = {1, 2, 3} ' Integer() と推論
' 匿名型
Dim person = New With {.Name = "John", .Age = 30}
' LINQ での推論
Dim numbers = {1, 2, 3, 4, 5}
Dim evens = From n In numbers Where n Mod 2 = 0 ' IEnumerable(Of Integer)
3.4 型変換
暗黙的な型変換
| 言語 | 暗黙的変換 | 制御方法 |
|---|---|---|
| PHP | 広範囲に許可 |
declare(strict_types=1) で制限 |
| TypeScript | 限定的 | 型チェックで防止 |
| VB.NET |
Option Strict Off で許可 |
Option Strict On で制限 |
PHP
<?php
// 暗黙的な型変換
$result = "10" + 5; // 15(int)
$result = "10" . 5; // "105"(string)
$result = "10abc" + 5; // 15(警告は出る)
// strict_types で制限
declare(strict_types=1);
function add(int $a, int $b): int
{
return $a + $b;
}
add("10", 5); // TypeError
TypeScript
// 暗黙的変換は限定的
const result = "10" + 5; // "105"(string)
// const result: number = "10"; // エラー
// 明示的な変換
const num = parseInt("10");
const str = String(42);
const numFromStr = Number("42");
VB.NET
Option Strict Off ' 暗黙的変換を許可
Dim result As Integer = "10" ' OK: 暗黙的に変換
Dim str As String = 42 ' OK: 暗黙的に変換
Option Strict On ' 暗黙的変換を禁止
' 明示的な変換が必要
Dim result As Integer = CInt("10")
Dim result2 As Integer = Integer.Parse("10")
Dim str As String = CStr(42)
Dim str2 As String = 42.ToString()
' 変換関数一覧
CBool(value) ' Boolean
CByte(value) ' Byte
CInt(value) ' Integer
CLng(value) ' Long
CDbl(value) ' Double
CDec(value) ' Decimal
CStr(value) ' String
CDate(value) ' Date
3.5 リテラル記法
配列リテラル
<?php
// PHP
$indexed = [1, 2, 3];
$assoc = ['name' => 'John', 'age' => 30];
$nested = [
'user' => ['name' => 'John'],
'items' => [1, 2, 3]
];
// TypeScript
const indexed = [1, 2, 3];
const assoc = { name: 'John', age: 30 };
const nested = {
user: { name: 'John' },
items: [1, 2, 3]
};
' VB.NET
Dim indexed() As Integer = {1, 2, 3}
Dim assoc As New Dictionary(Of String, Object) From {
{"name", "John"},
{"age", 30}
}
' 匿名型
Dim obj = New With {.Name = "John", .Age = 30}
特殊リテラル
| 言語 | 正規表現 | 日付 |
|---|---|---|
| PHP | '/pattern/' |
new DateTime('2024-01-01') |
| TypeScript | /pattern/ |
new Date('2024-01-01') |
| VB.NET | New Regex("pattern") |
#1/1/2024# または New Date(2024, 1, 1)
|
' VB.NET の日付リテラル
Dim d As Date = #1/1/2024#
Dim d2 As Date = #January 1, 2024#
Dim dt As Date = #1/1/2024 12:30:00 PM#