PHP 資料類型

WBOY
發布: 2024-08-29 12:37:56
原創
906 人瀏覽過

PHP 或超文本預處理器是一種基於 Web 的應用程式開發程式語言,可在其中合併 HTML 編碼以建立 Web 應用程式。在 PHP 中,有八種不同的資料類型用於在腳本中聲明和呼叫變數。它們是用於真或假值的“布林”,用於數字值的“整數”,用於十進制數字的“浮點/雙精度”,用於字元的“字串”,用於固定元素大小的“數組”,用於表示類別實例的“物件” ,'NULL' 表示void,'resources' 表示PHP 腳本外部的元素。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

前 3 種 PHP 資料型別

用於儲存值的 PHP 變數可能與各種資料類型相關聯,從最簡單的 int 到更複雜的資料類型(例如陣列)。 PHP 被稱為鬆散類型程式語言,這意味著變數資料類型是在運行時根據其屬性決定的,並且沒有明確定義。它分析給定值的屬性,然後確定分配給它的資料類型。 PHP 支援 8 種原始資料類型,可進一步分為以下 3 種:

讓我們透過範例詳細了解每一個。

PHP 資料類型

1.標量類型

它們可以進一步分為以下原始類型:

a.布爾

這些類型的可能輸出為 0 或 1,即 true 或 false。它們用於條件測試案例,其中滿足條件時事件傳回 true,不滿足時事件傳回 false。它還將 NULL 和空字串視為 false。

代碼:

<?php
// TRUE is assigned to a variable value
$variable_value = true;
var_dump($variable_value);
?>
登入後複製

輸出:

PHP 資料類型

b.整數

整數資料型別保存 -2,147,483,648 和 2,147,483,647 之間的非十進位整數值。此最大值和最小值取決於系統,無論是 32 位元還是 64 位元。透過使用常數 PHP_INT_MAX,我們可以找出最大值。它還保存以 10 為基數、以 8 為基數和以 6 為基數的值。

代碼:

<?php
// example for decimal (base 10)
$dec1 = 100;
$dec2 = 200;
// example for decimal (base 8)
$oct1 = 10;
// example for decimal (base 6)
$hex1 = 0x15;
$addn = $dec1 + $dec2;
echo $addn;
?>
登入後複製

輸出:

PHP 資料類型

c.浮動/雙

具有小數點或指數的數字稱為浮點數/實數。它可以有正數和負數。該數字應顯示預先定義的小數位數。

代碼:

<?php
$dec1 = 0.134;
var_dump($dec1);
$exp1 = 23.3e2;
var_dump($exp1);
$exp2 = 6E-9;
var_dump($exp2);
?>
登入後複製

輸出:

PHP 資料類型

d.字串

字串資料型態基本上就是字元的集合,包括數字、字母和字母。它們可以保存高達 2GB 的值。如果必須在字串中顯示變量,則應使用雙引號聲明它們。另外,單引號也可以。

代碼:

<?php
$name = "Jay";
$str1 = 'Declaring name in single quote as $name';
echo $str1;
echo "\n";
$str2 = "Declaring name in double quote as $name";
echo $str2;
echo "\n";
$str3 = 'Just a string';
echo $str3;
?>
登入後複製

輸出:

PHP 資料類型

2.複合型

這些是無法指派新值的。數組和物件都屬於這一類。

a.數組

它是一種資料結構,具有固定大小的具有相似資料類型的元素的集合。它也用於以有序映射的形式儲存已知數量的鍵值對。它可用於多種用途,如列表、雜湊表(映射實作)、集合、堆疊、字典、佇列等;多維數組也是可能的。

一個簡單的陣列範例如下:

代碼:

<?php
$animals = array("Dog", "Cat", "Cow");
var_dump($animals);
$animal_babies = array(
"Dog" => "Puppy",
"Cat" => "Kitten",
"Cow" => "Calf"
);
var_dump($animal_babies);
?>
登入後複製

輸出:

PHP 資料類型

b.對象

它允許儲存資料(稱為其屬性)並提供有關如何處理資料(稱為物件的方法)的資訊。物件充當類別的實例,該類別用作其他物件的模板。關鍵字“new”用於建立物件。

每個物件都繼承父類別的屬性和方法。它需要每個物件中有一個明確聲明和一個「類別」。

代碼:

<?php
// Declaring a class
class statement{
// properties
public $stmt = "Insert any string here";
// Declaring a method
function show_statement(){
return $this->stmt;
}
}
// Creation of new object
$msg = new statement;
var_dump($msg);
?>
登入後複製

輸出:

PHP 資料類型

3.特殊型

PHP 中有 2 種特殊的資料類型屬於此類,因為它們是唯一的。他們是:

a. NULL

In PHP, this special NULL is used for representing empty variables, i.e. the variable has no data in it, and NULL is the only possible value to it. If it has been set to unset() or if no value has been set to it, a variable assigned to the constant NULL becomes a NULL data type.

Here we are setting NULL directly to val1. For the val2 variable, we are assigning a string value first and then setting it as NULL. In both cases, the final value of variables is NULL.

Code:

<?php
$val1 = NULL;
var_dump($val1);
echo "<br>";
$val2 = "Any string";
$val2 = NULL;
var_dump($val2);
?>
登入後複製

Output:

PHP 資料類型

b. Resources

A resource is not an actual data type, whereas it is a special variable that keeps a reference to a resource external to PHP. They hold special handlers for files and database connections that are open. Special functions usually create and use these resources.

To run this code, we must have the file.txt created in the system with reading permission given to it. It throws an error in case “handle” is not a resource. Also, make sure to connect to any existing database in your system.

Code:

<?php
// Open an existing file to read
$handle = fopen("file.txt", "r");
var_dump($handle);
echo "<br>";
// Connecting to MySQL database server with settings set to default
$db = mysql_connect("localhost", "root", "");
var_dump($db);
?>
登入後複製

Apart from the above data types, we also have something called pseudo-types: the keywords in PHP document used to indicate the types or values that an argument can have. Some of them are:

  • mixed: They allow a parameter to accept more than one type. Ex: gettype()
  • number: With a number, a parameter can be afloat or an integer.
  • void, callback, array|object are some of the other pseudo-types

Conclusion

Here we have covered almost all of the data types which are available in PHP. All of the above 8 primitive types are implicitly supported by PHP, and there is no need for the user to specify them manually. Arrays and objects can hold multiple values, whereas, for rest, all can hold only a single value (except NULL, which holds no value).

以上是PHP 資料類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!