The 5th Annual China PHP Conference


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

is_scalar Indique si une variable est un scalaire


bool is_scalar ( mixed $var )

Indique si la variable donnée est un scalaire.

Les variables scalaires sont celles qui contiennent des entiers, des nombres décimaux, des chaînes de caractères ou des booléens. Les types array, object et resource ne sont pas scalaires.


is_scalar() ne considère pas les valeurs des types ressource comme scalaires, étant donné que les ressources sont des types abstraits, basés sur des entiers. Ceci est susceptible de changer.


La fonction is_scalar() ne considère pas la valeur NULL comme un scalaire.

Liste de paramètres


La variable à évaluer.

Valeurs de retour

Retourne TRUE si var est un scalaire, FALSE autrement.


Exemple #1 Exemple avec is_scalar()

function show_var($var
    if (
is_scalar($var)) {
    } else {
$pi 3.1416;
$proteines = array("hémoglobine""cytochrome c oxidase""ferredoxine");



L'exemple ci-dessus va afficher :

array(3) {
  string(11) "hémoglobine"
  string(20) "cytochrome c oxidase"
  string(11) "ferredoxine"

Voir aussi

  • is_float() - Détermine si une variable est de type nombre décimal
  • is_int() - Détermine si une variable est de type nombre entier
  • is_numeric() - Détermine si une variable est un type numérique
  • is_real() - Alias de is_float
  • is_string() - Détermine si une variable est de type chaîne de caractères
  • is_bool() - Détermine si une variable est un booléen
  • is_object() - Détermine si une variable est de type objet
  • is_array() - Détermine si une variable est un tableau

add a note add a note

User Contributed Notes 4 notes

10 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.

echo('is_scalar() test:'.EOL);
"NULL: "      . print_R(is_scalar(NULL),     true) . EOL);
"false: "    . print_R(is_scalar(false),   true) . EOL);
"(empty): "  . print_R(is_scalar(''),      true) . EOL);
"0: "         . print_R(is_scalar(0),       true) . EOL);
"'0': "      . print_R(is_scalar('0'),     true) . EOL);

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

   * NULL is NOT a scalar
   * false, (empty string), 0, and "0" ARE scalars
Dr K
11 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.
efelch at gmail dot com
11 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.
popanowel HAT hotmailZ DOT cum
13 years ago
Hi ... for newbees here, I just want to mention that reference and scalar variable aren't the same. A reference is a pointer to a scalar, just like in C or C++.

<? php  // simple reference to scalar

$a = 2;
$ref = & $a;

"$a <br> $ref";

this should print out: "2 <br> 2".

Scalar class also exists. Look below:
<? php

class Object_t {


Object_t ()  // constructor
$this->a = 1;


$a = new Object_t; // we define a scalar object

$ref_a = &a;

"$a->a <br> $ref->a";

again, this should echo: "1 <br> 1";

Here is another method isued in OOP to acheive on working only over reference to scalar object. Using this, you won't ever have to  ask yourself if you work on a copy of the scalar or its reference. You will only possess reference to the scalar object. If you want to duplicate the scalar object, you will have to create a function for that purpose that would read by the reference the values and assign them to another scalar of the same type... or an other type, it is as you wish at that moment.

class objet_t {

$this->a = "patate_poil";

   function &
// here we create a scalar object in memory
      // and we return it by reference to the calling
      // control scope.
return &new $object_type;

$ref_object_t = get_ref(object_t);

"$ref_object_t->a <br>";
this should echo: "patate_poit <br>".

The only thing that I try to demonstrate is that scalar variable ARE object in memory while a reference is usualy a variable (scalar object) that contain the address of another scalar object, which contain the informations you want by using the reference.

Good Luck!

otek is popanowel HAT hotmailZ DOT cum
To Top