PHP Doku:: Prüft ob eine Variable skalar ist - function.is-scalar.html

Verlauf / Chronik / History: (29) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenVariablenbehandlungFunktionen zur Behandlung von Variablenis_scalar

Ein Service von Reinhard Neidl - Webprogrammierung.

Funktionen zur Behandlung von Variablen

<<is_resource

is_string>>

is_scalar

(PHP 4 >= 4.0.5, PHP 5)

is_scalar Prüft ob eine Variable skalar ist

Beschreibung

bool is_scalar ( mixed $var )

Prüft ob die gegebene Variable skalar ist.

Unter skalaren Variablen versteht man solche die einen integer, float, string oder boolean Wert enthalten. Variablen vom Typ array, object und resource sind nicht skalar.

Hinweis:

is_scalar() betrachtet Variablen vom Typ resource nicht als skalar da Resourcen abstrakte Datentypen darstellen auch wenn sie zur Zeit als Integer repräsentiert sind. Sie sollten sich nicht auf dieses Implementationsdetail verlassen da es sich ändern kann.

Parameter-Liste

var

Die zu prüfende Variable.

Rückgabewerte

Liefert TRUE wenn var skalar ist, sonst FALSE.

Beispiele

Beispiel #1 is_scalar() Example

<?php
function show_var($var
{
    if (
is_scalar($var)) {
        echo 
$var;
    } else {
        
var_dump($var);
    }
}
$pi 3.1416;
$proteins = array("Hämoglobin""Cytochrom C Oxidase""Ferredoxin");

show_var($pi);
show_var($proteins)

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

3.1416
array(3) {
  [0]=>
  string(10) "Hämoglobin"
  [1]=>
  string(20) "Cytochrome C Oxidase"
  [2]=>
  string(10) "Ferredoxin"
}

Siehe auch

  • is_float() - Prüft, ob eine Variable vom Typ float ist
  • is_int() - Prüft, ob eine Variable vom Typ int ist
  • is_numeric() - Prüft, ob eine Variable eine Zahl oder ein numerischer String ist
  • is_real() - Alias von is_float
  • is_string() - Prüft, ob Variable vom Typ string ist
  • is_bool() - Prüft, ob eine Variable vom Typ boolean ist
  • is_object() - Prüft, ob eine Variable vom Typ object ist
  • is_array() - Prüft, ob die Variable ein Array ist


7 BenutzerBeiträge:
- Beiträge aktualisieren...
webmaster at oehoeboeroe dot nl
4.05.2009 21:15
Here's a little function that will test whether a variable can be used as offset to an array.

<?php
function is_offset(&$var) {
    return (
is_scalar($var) || is_null($var)) && !is_resource($var);
}
?>

The resource check is currently redundant, but according to the manual that may change in the future.

31.07.2006 17:59
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
docey
21.05.2006 4:02
just a warning as it appears that an empty value is not a scalar.

although the manual says scalars are:
-> integers (eg. 243)
-> floats (eg. 14.4)
-> strings (eg "this is a string")
-> booleans (eg true or false)

this might seem easy but rembere that empty values are:
-> NULL
-> false
-> empty string
-> 0
-> "0"

thus the true list of scalars must be:
-> integers large then zero
-> floats larger then zero
-> strings with a lenght large then zero
-> string that do not contain only the number 0
-> boolean value of true.

especialy the boolean value false can be tricky as true is
considerd to be scalar but false is not. or a string that is
initialized with a zero lenght here's an example.

$string = ""; //initialize our string.

function store_var($var)
{
 if(is_scalar($var)){
  print("our var is a scalar.");
 }else{
  print("you tried to store something else here.");
 }
}

$this->store_var($string); // does not work as expected

this might be usefull when you create method to store a
variable inside a class but do not want to store other stuff
like object, arrays or resources, but storing an empy value
is oke. then is_scalar does not work as empty values are
incorrectly not seens as a bool, string, integer or float.

this is an important downside to is_scalar wich might cause
some people to loose there hair from headscrachting.

this might be added to the manual as a warning or something.
efelch at gmail dot com
17.08.2005 23:31
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.
Dr K
14.08.2005 12:48
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.
popanowel HAT hotmailZ DOT cum
14.05.2004 21:31
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;

  echo
"$a <br> $ref";

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

Scalar class also exists. Look below:
<? php

 
class Object_t {

     var
$a;

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

  }

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

 
$ref_a = &a;

  echo
"$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.
<?php

 
class objet_t {
     var
$a;

     function
object_t
    
{
       
$this->a = "patate_poil";
     }
  }

   function &
get_ref($object_type)
   {
     
// 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);

   echo
"$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
bps7j at yahoNOSPAMo.com
9.02.2004 6:56
is_scalar(null) is false.  Apparently a variable needs to have a value to be considered a scalar.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)