is_scalar
(PHP 4 >= 4.0.5, PHP 5)
is_scalar — Finds whether a variable is a scalar
Description
Finds whether the given variable is a scalar.
Scalar variables are those containing an integer, float, string or boolean. Types array, object and resource are not scalar.
Note:
is_scalar() does not consider resource type values to be scalar as resources are abstract datatypes which are currently based on integers. This implementation detail should not be relied upon, as it may change.
Note:
is_scalar() does not consider NULL to be scalar.
Parameters
-
var
-
The variable being evaluated.
Return Values
Returns TRUE
if var
is a scalar FALSE
otherwise.
Examples
Example #1 is_scalar() example
<?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)
?>
The above example will output:
3.1416 array(3) { [0]=> string(10) "hemoglobin" [1]=> string(20) "cytochrome c oxidase" [2]=> string(10) "ferredoxin" }
See Also
- is_float() - Finds whether the type of a variable is float
- is_int() - Find whether the type of a variable is integer
- is_numeric() - Finds whether a variable is a number or a numeric string
- is_real() - Alias of is_float
- is_string() - Find whether the type of a variable is string
- is_bool() - Finds out whether a variable is a boolean
- is_object() - Finds whether a variable is an object
- is_array() - Finds whether a variable is an array
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Функции для работы с переменными
- boolval
- debug_zval_dump
- doubleval
- empty
- floatval
- get_defined_vars
- get_resource_type
- gettype
- import_request_variables
- intval
- is_array
- is_bool
- is_callable
- is_double
- is_float
- is_int
- is_integer
- is_long
- is_null
- is_numeric
- is_object
- is_real
- is_resource
- is_scalar
- is_string
- isset
- print_r
- serialize
- settype
- strval
- unserialize
- unset
- var_dump
- var_export
Коментарии
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.
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.
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