PHP 8.4.0 RC3 available for testing

is_scalar

(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)

is_scalar 変数がスカラかどうかを調べる

説明

is_scalar(mixed $value): bool

指定した変数がスカラかどうかを調べます。

スカラ変数には intfloatstring あるいは bool が含まれます。 arrayobjectresourcenull はスカラではありません。

注意:

リソース型は現在整数に基づく抽象型であるため、 is_scalar()resource 型の値を スカラ値と判定しません。この実装の詳細は変更される可能性があるため、 前堤にするべきではありません。

注意:

is_scalar() は、NULL をスカラとは見なしません。

パラメータ

value

評価する変数。

戻り値

value がスカラの場合に true、 それ以外の場合に false を返します。

例1 is_scalar() の例

<?php
function show_var($var)
{
if (
is_scalar($var)) {
echo
$var;
} else {
var_dump($var);
}
}
$pi = 3.1416;
$proteins = array("hemoglobin", "cytochrome c oxidase", "ferredoxin");

show_var($pi);
show_var($proteins)

?>

上の例の出力は以下となります。

3.1416
array(3) {
  [0]=>
  string(10) "hemoglobin"
  [1]=>
  string(20) "cytochrome c oxidase"
  [2]=>
  string(10) "ferredoxin"
}

参考

  • is_float() - 変数の型が float かどうか調べる
  • is_int() - 変数が整数型かどうかを検査する
  • is_numeric() - 変数が数字または数値形式の文字列であるかを調べる
  • is_real() - is_float のエイリアス
  • is_string() - 変数の型が文字列かどうかを調べる
  • is_bool() - 変数が boolean であるかを調べる
  • is_object() - 変数がオブジェクトかどうかを検査する
  • is_array() - 変数が配列かどうかを検査する

add a note

User Contributed Notes 3 notes

up
17
Dr K
19 years ago
Having hunted around the manual, I've not found a clear statement of what makes a type "scalar" (e.g. if some future version of the language introduces a new kind of type, what criterion will decide if it's "scalar"? - that goes beyond just listing what's scalar in the current version.)

In other lanuages, it means "has ordering operators" - i.e. "less than" and friends.

It (-:currently:-) appears to have the same meaning in PHP.
up
11
Anonymous
18 years ago
Another warning in response to the previous note:
> just a warning as it appears that an empty value is not a scalar.

That statement is wrong--or, at least, has been fixed with a later revision than the one tested. The following code generated the following output on PHP 4.3.9.

CODE:
<?php
echo('is_scalar() test:'.EOL);
echo(
"NULL: " . print_R(is_scalar(NULL), true) . EOL);
echo(
"false: " . print_R(is_scalar(false), true) . EOL);
echo(
"(empty): " . print_R(is_scalar(''), true) . EOL);
echo(
"0: " . print_R(is_scalar(0), true) . EOL);
echo(
"'0': " . print_R(is_scalar('0'), true) . EOL);
?>

OUTPUT:
is_scalar() test:
NULL:
false: 1
(empty): 1
0: 1
'0': 1

THUS:
* NULL is NOT a scalar
* false, (empty string), 0, and "0" ARE scalars
up
6
efelch at gmail dot com
19 years ago
A scalar is a single item or value, compared to things like arrays and objects which have multiple values. This tends to be the standard definition of the word in terms of programming. An integer, character, etc are scalars. Strings are probably considered scalars since they only hold "one" value (the value represented by the characters represented) and nothing else.
To Top