PHP - is_scalar() 函式



定義和用法

函式 `is_scalar()` 用於檢查變數是否為標量。標量變數是指包含整數、浮點數、字串或布林值型別的變數。陣列、物件和資源型別不是標量。

語法

bool is_scalar ( mixed $value )

引數

序號 引數及描述
1

被評估的變數。

返回值

如果 `value` 是標量,則此函式返回 **true**,否則返回 **false**。此函式不認為 NULL 為標量。

依賴

PHP 4.0.5 及以上版本

示例

以下示例演示了 `is_scalar()` 函式的用法:

<?php
   $a = "Tutorialspoint";
   echo "a is ".( is_scalar($a)? 'scalar' : 'not scalar') . "<br>";

   $b = 0;
   echo "b is ".( is_scalar($b)? 'scalar' : 'not scalar') . "<br>";

   $c = 40;
   echo "c is ".( is_scalar($c)? 'scalar' : 'not scalar') . "<br>";

   $d = NULL;
   echo "d is ".( is_scalar($d)? 'scalar' : 'not scalar') . "<br>";

   $e = array("a", "b", "c");
   echo "e is ".( is_scalar($e)? 'scalar' : 'not scalar') . "<br>";

   $f = 3.1416;
   echo "f is ".( is_scalar($f)? 'scalar' : 'not scalar') . "<br>";

   $g = new stdClass();
   echo "g is ".( is_scalar($g)? 'scalar' : 'not scalar') . "<br>";
?>

輸出

這將產生以下結果:

a is scalar
b is scalar
c is scalar
d is not scalar
e is not scalar
f is scalar
g is not scalar
php_variable_handling_functions.htm
廣告