©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PHP 4 >= 4.0.5, PHP 5, PHP 7)
is_scalar — 检测变量是否是一个标量
$var
)
如果给出的变量参数 var
是一个标量, is_scalar()
返回 TRUE
,否则返回 FALSE
。
标量变量是指那些包含了 integer 、 float 、 string 或 boolean 的变量,而 array 、 object 和 resource 则不是标量。
<?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 );
// 打印:3.1416
show_var ( $proteins )
// 打印:
// array(3) {
// [0]=>
// string(10) "hemoglobin"
// [1]=>
// string(20) "cytochrome c oxidase"
// [2]=>
// string(10) "ferredoxin"
// }
?>
Note:
尽管当前的 resource 类型是居于整数的,但 is_scalar() 不会把它们当作是标量,因为资源是抽象数据类型。不能依赖于执行细节,因为它可能会改变。
参见 is_bool() 、 is_numeric() 、 is_float() 、 is_int() 、 is_real() 、 is_string() 、 is_object() 、 is_array() 和 is_integer() 。
[#1] webmaster at oehoeboeroe dot nl [2009-05-04 12:15:33]
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.
[#2] [2006-07-31 08:59:07]
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
[#3] efelch at gmail dot com [2005-08-17 14:31:29]
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.
[#4] Dr K [2005-08-14 03:48:22]
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.
[#5] popanowel HAT hotmailZ DOT cum [2004-05-14 12:31:54]
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 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 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
[#6] bps7j at yahoNOSPAMo.com [2004-02-08 21:56:33]
is_scalar(null) is false. Apparently a variable needs to have a value to be considered a scalar.