PHP: Hypertext Preprocessor (original) (raw)
is_scalar
(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)
is_scalar — Finds whether a variable is a scalar
Description
See scalar types for more information.
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
value
The variable being evaluated.
Return Values
Returns [true](reserved.constants.php#constant.true)
if value
is a scalar, [false](reserved.constants.php#constant.false)
otherwise.
Examples
Example #1 is_scalar() example
<?php function show_var($var) { if (is_scalar($var)) { echo $var, PHP_EOL; } else { var_dump($var); } }$pi = 3.1416; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>r</mi><mi>o</mi><mi>t</mi><mi>e</mi><mi>i</mi><mi>n</mi><mi>s</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi><mo stretchy="false">(</mo><mi mathvariant="normal">"</mi><mi>h</mi><mi>e</mi><mi>m</mi><mi>o</mi><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>i</mi><mi>n</mi><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi mathvariant="normal">"</mi><mi>c</mi><mi>y</mi><mi>t</mi><mi>o</mi><mi>c</mi><mi>h</mi><mi>r</mi><mi>o</mi><mi>m</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>x</mi><mi>i</mi><mi>d</mi><mi>a</mi><mi>s</mi><mi>e</mi><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi mathvariant="normal">"</mi><mi>f</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>o</mi><mi>x</mi><mi>i</mi><mi>n</mi><mi mathvariant="normal">"</mi><mo stretchy="false">)</mo><mo separator="true">;</mo><mi>s</mi><mi>h</mi><mi>o</mi><msub><mi>w</mi><mi>v</mi></msub><mi>a</mi><mi>r</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">proteins = array("hemoglobin", "cytochrome c oxidase", "ferredoxin");show_var(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">in</span><span class="mord mathnormal">s</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mopen">(</span><span class="mord">"</span><span class="mord mathnormal">h</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">o</span><span class="mord mathnormal">bin</span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">"</span><span class="mord mathnormal" style="margin-right:0.03588em;">cy</span><span class="mord mathnormal">t</span><span class="mord mathnormal">oc</span><span class="mord mathnormal">h</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">m</span><span class="mord mathnormal">eco</span><span class="mord mathnormal">x</span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mord mathnormal">a</span><span class="mord mathnormal">se</span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">"</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">erre</span><span class="mord mathnormal">d</span><span class="mord mathnormal">o</span><span class="mord mathnormal">x</span><span class="mord mathnormal">in</span><span class="mord">"</span><span class="mclose">)</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">h</span><span class="mord mathnormal">o</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">v</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mopen">(</span></span></span></span>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
Found A Problem?
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.
`
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:
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
`
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.