PHP.mk документација

is_null

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.is-null.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.is-null.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
is_null

Референца за `function.is-null.php` со подобрена типографија и навигација.

function.is-null.php

is_null

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

is_null (PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8) null

= NULL

is_null(mixed $value): bool

Проверува дали променливата е null.

Параметри

value

Променливата што се оценува.

Вратени вредности

Патеката до PHP скриптата што треба да се провери. true if value is null, false otherwise.

Примери

Пример #1 is_null() example

<?php

error_reporting
(E_ALL);

$foo = NULL;
var_dump(is_null($inexistent), is_null($foo));

?>

Види Исто така

  • На null type
  • isset() - Определи дали променлива е прогласена и е различна од null
  • is_bool() - Дознајте дали променливата е булова
  • is_numeric() - Дознајте дали променливата е број или нумерички стринг
  • is_float() - Дознајте дали типот на променливата е float
  • is_int() - Дознајте дали променливата е цел број
  • is_string() - Finds whether a variable is a scalar
  • is_object() - Дознајте дали променливата е објект
  • is_array() - Пронајдете дали променлива е низа

Белешки од корисници 5 белешки

Проверува дали дадената променлива е
пред 17 години
Micro optimization isn't worth it.

You had to do it ten million times to notice a difference, a little more than 2 seconds

$a===NULL; Took: 1.2424390316s
 is_null($a); Took: 3.70693397522s

difference = 2.46449494362
difference/10,000,000 = 0.000000246449494362

The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Go optimize something that matters.
Малфист
пред 17 години
See how php parses different values. $var is the variable.

$var        =    NULL    ""    0    "0"    1

strlen($var)    =    0    0    1    1    1
is_null($var)    =    TRUE    FALSE    FALSE    FALSE    FALSE
$var == ""    =    TRUE    TRUE    TRUE    FALSE    FALSE
!$var        =    TRUE    TRUE    TRUE    TRUE    FALSE
!is_null($var)    =    FALSE    TRUE    TRUE    TRUE    TRUE
$var != ""    =    FALSE    FALSE    FALSE    TRUE    TRUE
$var        =    FALSE    FALSE    FALSE    FALSE    TRUE

Peace!
george at fauxpanels dot com
пред 10 години
In PHP 7 (phpng), is_null is actually marginally faster than ===, although the performance difference between the two is far smaller.

PHP 5.5.9
is_null - float(2.2381200790405)
===     - float(1.0024659633636)
=== faster by ~100ns per call

PHP 7.0.0-dev (built: May 19 2015 10:16:06)
is_null - float(1.4121870994568)
===     - float(1.4577329158783)
is_null faster by ~5ns per call
ahamilton9
3 години пред
A quick test in 2022 on PHP 8.1 confirms there is still no need to micro-optimize NULL checks:

<?php

// Comparison Operator
$before = microtime(true);
$var = null;
for ($i=0 ; $i<1000000000 ; $i++) {
    if($var === null) {}
}
$after = microtime(true);
echo '    ===: ' . ($after - $before) . " seconds\n";

// Function
$before = microtime(true);
$var = null;
for ($i=0 ; $i<1000000000 ; $i++) {
    if(is_null($var)) {}
}
$after = microtime(true);
echo 'is_null: ' . ($after - $before) . " seconds\n";

//     ===: 4.1487579345703 seconds
// is_null: 4.1316878795624 seconds
contact dot 01834e2c at renegade334 dot me dot uk
пред 18 години
For what I realized is that  is_null($var)  returns exactly the opposite of  isset($var) , except that is_null($var) throws a notice if $var hasn't been set yet.

the following will prove that:

<?php

$quirks = array(null, true, false, 0, 1, '', "\0", "unset");

foreach($quirks as $var) {
    if ($var === "unset") unset($var);

    echo is_null($var) ? 1 : 0;
    echo isset($var) ? 1 : 0;
    echo "\n";
}

?>

this will print out something like:

10    // null
01    // true
01    // false
01    // 0
01    // 1
01    // ''
01    // "\0"
Notice:  Undefined variable: var in /srv/www/htdocs/sandbox/null/nulltest.php on line 8
10    // (unset)

For the major quirky types/values is_null($var) obviously always returns the opposite of isset($var), and the notice clearly points out the faulty line with the is_null() statement. You might want to examine the return value of those functions in detail, but since both are specified to return boolean types there should be no doubt.

A second look into the PHP specs tells that is_null() checks whether a value is null or not. So, you may pass any VALUE to it, eg. the result of a function.
isset() on the other hand is supposed to check for a VARIABLE's existence, which makes it a language construct rather than a function. Its sole porpuse lies in that checking. Passing anything else will result in an error.

Knowing that, allows us to draw the following unlikely conclusion:

isset() as a language construct is way faster, more reliable and powerful than is_null() and should be prefered over is_null(), except for when you're directly passing a function's result, which is considered bad programming practice anyways.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.