PHP 8.5.0 Alpha 1 available for testing

is_scalar

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

is_scalar Indica si una variable es un escalar

Descripción

is_scalar(mixed $value): bool

Indica si una expresión es evaluada como un valor escalar.

Consulte tipos escalares para más información.

Nota:

is_scalar() no considera los valores de tipo recurso como escalares, dado que los recursos son tipos abstractos, basados en enteros. Esto es susceptible de cambiar.

Nota:

La función is_scalar() no considera el valor NULL como un escalar.

Parámetros

value

La variable a evaluar.

Valores devueltos

Devuelve true si value es un escalar, false en caso contrario.

Ejemplos

Ejemplo #1 Ejemplo con is_scalar()

<?php
function show_var($var)
{
if (
is_scalar($var)) {
echo
$var, PHP_EOL;
} else {
var_dump($var);
}
}

$pi = 3.1416;
$proteines = array("hemoglobina", "citocromo c oxidasa", "ferredoxina");

show_var($pi);

show_var($proteines)
?>

El resultado del ejemplo sería:

3.1416
array(3) {
  [0]=>
  string(11) "hemoglobina"
  [1]=>
  string(20) "citocromo c oxidasa"
  [2]=>
  string(11) "ferredoxina"
}

Ver también

  • is_float() - Determina si una variable es de tipo float
  • is_int() - Determina si una variable es de tipo integer
  • is_numeric() - Determina si una variable es un número o una cadena numérica
  • is_real() - Alias de is_float
  • is_string() - Determina si una variable es de tipo string
  • is_bool() - Determina si una variable es un bool
  • is_object() - Determina si una variable es de tipo objeto
  • is_array() - Determina si una variable es un array

add a note

User Contributed Notes 3 notes

up
22
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
8
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