ScotlandPHP

isset

(PHP 4, PHP 5, PHP 7)

isset変数がセットされていること、そして NULL でないことを検査する

説明

bool isset ( mixed $var [, mixed $... ] )

変数がセットされており、それが NULL でないことを調べます。

変数が、unset() により割当を解除された場合、 何も値が設定されていない状態になります。 NULLに設定されている変数を調べた場合、 isset()FALSEを返します。 null文字("\0")はPHPの定数 NULLと等価ではないことにも注意してください。

複数のパラメータを渡した場合は、isset() はそれらすべてがセットされている場合にのみ TRUE を返します。 左から順に評価を行い、セットされていない変数があった時点で処理を終了します。

パラメータ

var

調べたい変数。

...

別の変数。

返り値

var が存在して NULL 以外の値をとれば TRUE、 そうでなければ FALSE を返します。

変更履歴

バージョン 説明
5.4.0

文字列のオフセットに非数値を指定した場合は FALSE を返すようになりました。

例1 isset() の例

<?php

$var 
'';

// これは TRUE と評価されるので、テキストが出力される
if (isset($var)) {
    echo 
"This var is set so I will print.";
}

// 次の例では、isset() の戻り値を出力するため var_dump を
// 使用している

$a "test";
$b "anothertest";

var_dump(isset($a));      // TRUE
var_dump(isset($a$b)); // TRUE

unset ($a);

var_dump(isset($a));     // FALSE
var_dump(isset($a$b)); // FALSE

$foo NULL;
var_dump(isset($foo));   // FALSE

?>

この関数は配列の要素に対しても動作します:

<?php

$a 
= array ('test' => 1'hello' => NULL'pie' => array('a' => 'apple'));

var_dump(isset($a['test']));            // TRUE
var_dump(isset($a['foo']));             // FALSE
var_dump(isset($a['hello']));           // FALSE

// キー 'hello' は NULL と等しいのでセットされていないと見なされる。
// もし NULL なキー値に対してチェックを行いたい場合、次を試してみること:
var_dump(array_key_exists('hello'$a)); // TRUE

// 配列の深い要素の値のチェック
var_dump(isset($a['pie']['a']));        // TRUE
var_dump(isset($a['pie']['b']));        // FALSE
var_dump(isset($a['cake']['a']['b']));  // FALSE

?>

例2 文字列のオフセットに対する isset()

PHP 5.4 以降では、文字列内でのオフセット指定を渡したときの isset() の挙動が変わりました。

<?php
$expected_array_got_string 
'somestring';
var_dump(isset($expected_array_got_string['some_key']));
var_dump(isset($expected_array_got_string[0]));
var_dump(isset($expected_array_got_string['0']));
var_dump(isset($expected_array_got_string[0.5]));
var_dump(isset($expected_array_got_string['0.5']));
var_dump(isset($expected_array_got_string['0 Mostel']));
?>

上の例の PHP 5.3 での出力は、このようになります。

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

上の例の PHP 5.4 での出力は、このようになります。

bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)

注意

警告

isset() は何らかの値が渡された 変数の場合のみ動作します。そうでない場合、パースエラーとなります。 もし 定数 が設定されているかどうかをチェックする場合は、 defined() を使用してください。

注意: これは、関数ではなく 言語構造のため、可変関数 を用いて コールすることはできません。

注意:

オブジェクトのアクセス不能なプロパティに対して isset() を使用した場合は、もしオーバーロードメソッド __isset() が宣言されていればそれをコールします。

参考

  • empty() - 変数が空であるかどうかを検査する
  • __isset()
  • unset() - 指定した変数の割当を解除する
  • defined() - 指定した名前の定数が存在するかどうかを調べる
  • PHP 型の比較表
  • array_key_exists() - 指定したキーまたは添字が配列にあるかどうかを調べる
  • is_null() - 変数が NULL かどうか調べる
  • エラー制御演算子 @

add a note add a note

User Contributed Notes 55 notes

up
19
p_ignorethis_lbowers at gmail dot com
1 year ago
I, too, was dismayed to find that isset($foo) returns false if ($foo == null). Here's an (awkward) way around it.

unset($foo);
if (compact('foo') != array()) {
  do_your_thing();
}

Of course, that is very non-intuitive, long, hard-to-understand, and kludgy. Better to design your code so you don't depend on the difference between an unset variable and a variable with the value null. But "better" only because PHP has made this weird development choice.

In my thinking this was a mistake in the development of PHP. The name ("isset") should describe the function and not have the desciption be "is set AND is not null". If it was done properly a programmer could very easily do (isset($var) || is_null($var)) if they wanted to check for this!

A variable set to null is a different state than a variable not set - there should be some easy way to differentiate. Just my (pointless) $0.02.
up
50
beuc at beuc dot net
10 years ago
"empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set."

So essentially
<?php
if (isset($var) && $var)
?>
is the same as
<?php
if (!empty($var))
?>
doesn't it? :)

!empty() mimics the chk() function posted before.
up
40
yaogzhan at gmail dot com
12 years ago
in PHP5, if you have

<?PHP
class Foo
{
    protected
$data = array('bar' => null);

    function
__get($p)
    {
        if( isset(
$this->data[$p]) ) return $this->data[$p];
    }
}
?>

and
<?PHP
$foo
= new Foo;
echo isset(
$foo->bar);
?>
will always echo 'false'. because the isset() accepts VARIABLES as it parameters, but in this case, $foo->bar is NOT a VARIABLE. it is a VALUE returned from the __get() method of the class Foo. thus the isset($foo->bar) expreesion will always equal 'false'.
up
34
Daniel Klein
5 years ago
How to test for a variable actually existing, including being set to null. This will prevent errors when passing to functions.

<?php
// false
var_export(
 
array_key_exists('myvar', get_defined_vars())
);

$myvar;
// false
var_export(
 
array_key_exists('myvar', get_defined_vars())
);

$myvar = null;
// true
var_export(
 
array_key_exists('myvar', get_defined_vars())
);

unset(
$myvar);
// false
var_export(
 
array_key_exists('myvar', get_defined_vars())
);

if (
array_key_exists('myvar', get_defined_vars())) {
 
myfunction($myvar);
}
?>

Note: you can't turn this into a function (e.g. is_defined($myvar)) because get_defined_vars() only gets the variables in the current scope and entering a function changes the scope.
up
21
mandos78 AT mail from google
9 years ago
Careful with this function "ifsetfor" by soapergem, passing by reference means that if, like the example $_GET['id'], the argument is an array index, it will be created in the original array (with a null value), thus causing posible trouble with the following code. At least in PHP 5.

For example:

<?php
$a
= array();
print_r($a);
ifsetor($a["unsetindex"], 'default');
print_r($a);
?>

will print

Array
(
)
Array
(
    [unsetindex] =>
)

Any foreach or similar will be different before and after the call.
up
22
Anonymous
11 years ago
I tried the example posted previously by Slawek:

$foo = 'a little string';
echo isset($foo)?'yes ':'no ', isset($foo['aaaa'])?'yes ':'no ';

He got yes yes, but he didn't say what version of PHP he was using.

I tried this on PHP 5.0.5 and got:  yes no

But on PHP 4.3.5 I got:  yes yes

Apparently, PHP4 converts the the string 'aaaa' to zero and then returns the string character at that position within the string $foo, when $foo is not an array. That means you can't assume you are dealing with an array, even if you used an expression such as isset($foo['aaaa']['bbb']['cc']['d']), because it will return true also if any part is a string.

PHP5 does not do this. If $foo is a string, the index must actually be numeric (e.g. $foo[0]) for it to return the indexed character.
up
18
muratyaman at gmail dot com
9 years ago
To organize some of the frequently used functions..

<?php

/**
* Returns field of variable (arr[key] or obj->prop), otherwise the third parameter
* @param array/object $arr_or_obj
* @param string $key_or_prop
* @param mixed $else
*/
function nz($arr_or_obj, $key_or_prop, $else){
 
$result = $else;
  if(isset(
$arr_or_obj)){
    if(
is_array($arr_or_obj){
      if(isset(
$arr_or_obj[$key_or_prop]))
       
$result = $arr_or_obj[$key_or_prop];
    }elseif(
is_object($arr_or_object))
      if(isset(
$arr_or_obj->$key_or_prop))
       
$result = $arr_or_obj->$key_or_prop;
    }
  }
  return
$result;
}

/**
* Returns integer value using nz()
*/
function nz_int($arr_or_obj, $key_or_prop, $else){
  return
intval(nz($arr_or_obj, $key_or_prop, $else));
}

$my_id = nz_int($_REQUEST, 'id', 0);
if(
$my_id > 0){
 
//why?
}
?>
up
16
francois vespa
6 years ago
Now this is how to achieve the same effect (ie, having isset() returning true even if variable has been set to null) for objects and arrays

<?php

// array

$array=array('foo'=>null);

return isset(
$array['foo']) || array_key_exists('foo',$array)
  ?
true : false ; // return true

return isset($array['inexistent']) || array_key_exists('inexistent',$array)
  ?
true : false ; // return false

// static class

class bar

{
  static
$foo=null;
}

return isset(
bar::$foo) || array_key_exists('foo',get_class_vars('bar'))
  ?
true : false ; // return true

return isset(bar::$inexistent) || array_key_exists('inexistent',get_class_vars('bar'))
  ?
true : false ; // return false

// object

class bar
{
    public
$foo=null;
}

$bar=new bar();

return isset(
$bar->foo) || array_key_exists('foo',get_object_vars($bar))
  ?
true : false ; // return true

return isset($bar->inexistent) || array_key_exists('inexistent',get_object_vars($bar))
  ?
true : false ; // return true

// stdClass

$bar=new stdClass;
$bar->foo=null;

return isset(
$bar->foo) || array_key_exists('foo',get_object_vars($bar))
  ?
true : false ; // return true

return isset($bar->inexistent) || array_key_exists('inexistent',get_object_vars($bar))
  ?
true : false ; // return true

?>
up
6
andreasonny83 at gmail dot com
2 years ago
Here is an example with multiple parameters supplied

<?php
$var
= array();
$var['val1'] = 'test';
$var['val2'] = 'on';

if ( isset(
$var['val1'], $var['val2'] ) && $var['val2'] === 'on' ) {
    unset(
$var['val1'] );
}
print_r( $var );
?>

This will output:
Array
(
    [val2] => on
)

The following code does the same calling "isset" 2 times:

<?php
$var
= array();
$var['val1'] = 'test';
$var['val2'] = 'on';

if ( isset(
$var['val1'] ) && isset( $var['val2'] ) && $var['val2'] === 'on' ) {
    unset(
$var['val1'] );
}
print_r( $var );
?>
up
15
a dot schaffhirt at sedna-soft dot de
9 years ago
You can safely use isset to check properties and subproperties of objects directly. So instead of writing

    isset($abc) && isset($abc->def) && isset($abc->def->ghi)

or in a shorter form

    isset($abc, $abc->def, $abc->def->ghi)

you can just write

    isset ($abc->def->ghi)

without raising any errors, warnings or notices.

Examples
<?php
    $abc
= (object) array("def" => 123);
   
var_dump(isset($abc));                // bool(true)
   
var_dump(isset($abc->def));           // bool(true)
   
var_dump(isset($abc->def->ghi));      // bool(false)
   
var_dump(isset($abc->def->ghi->jkl)); // bool(false)
   
var_dump(isset($def));                // bool(false)
   
var_dump(isset($def->ghi));           // bool(false)
   
var_dump(isset($def->ghi->jkl));      // bool(false)

   
var_dump($abc);                       // object(stdClass)#1 (1) { ["def"] => int(123) }
   
var_dump($abc->def);                  // int(123)
   
var_dump($abc->def->ghi);             // null / E_NOTICE: Trying to get property of non-object
   
var_dump($abc->def->ghi->jkl);        // null / E_NOTICE: Trying to get property of non-object
   
var_dump($def);                       // null / E_NOTICE: Trying to get property of non-object
   
var_dump($def->ghi);                  // null / E_NOTICE: Trying to get property of non-object
   
var_dump($def->ghi->jkl);             // null / E_NOTICE: Trying to get property of non-object
?>
up
16
Andrew Penry
12 years ago
The following is an example of how to test if a variable is set, whether or not it is aNU2L.aIt makesuse if the frac that pn enset(variable iill othrw tn e_NOTICE:errors but hon inditializd an aNU2Liill oot .

<?Php

)unction nar_expsts($var[{
    if( (empt(
$vGLOBALS/span>['val_expsts<_rro]) {
    unbsp; unbsp; ueturn
irue]
    }< lse<{
    unbsp; unbsp; unset(
$mGLOBALS/span>['val_expsts<_rro])
    }nbsp; unbsp; ueturn
ialse]
    }
}

nar_expsts($arron) $elrrtri/span>) $elrrfil] $elrrlin)){
  nbsp;
vmGLOBALS/span>['val_expsts<_rro])= irue]
&

$bl= (/span>$NU2Lbspan>]
&/span>$et(_rrors_handle("dar_expsts] $_NOTICE:/span>);&br />&cho i$ar_expsts($v));? "dTue < = "dFlse <]&br />&cho i$ar_expsts($vk/span>));? "dTue < = "dFlse <]&br />&/span>$rst ore_rrors_handle(<;

$>

TOtput::

/he frople mis the iet(_rrors_handle< nd srst ore_rrors_handle
<?php
$br />
)unction nar_expsts($var[{
  nbsp; if( (empt(
$vGLOBALS/span>['val_expsts<_rro]) {
    unbsp; nbsp; ueturn
irue]
  nbsp; }< lse<{
    unbsp; nbsp; unset(
$mGLOBALS/span>['val_expsts<_rro])
    }nbsp; nbsp; ueturn
ialse]
  nbsp; }
}

nar_expsts($arron) $elrrtri/span>) $elrrfil] $elrrlin)){
   
$rfil= nfil]$arrofil]
    }f (
atripo($vfil[$elrrlin)-/span>$1/span>]) 'iar_expsts<)) != ialse] {
    unbsp; unbsp; u/span>$mGLOBALS/span>['val_expsts<_rro])= irue]
    unbsp; unbsp; ueturn
irue]
    }< lse<{
    unbsp; unbsp; ueturn
ialse]
    }
}

$bl= (/span>$NU2Lbspan>]
&/span>$et(_rrors_handle("dar_expsts] $_NOTICE:/span>);&br />&cho i$ar_expsts($v));? "dTue < = "dFlse <]&br />&cho i$ar_expsts($vk/span>));? "dTue < = "dFlse <]&br />&/span>$is_ull;$v){&br />&/span>$rst ore_rrors_handle(<;

$>

TOtput::

/hes will omakeshe ihandle<
up
title="52% like this..."> 15
tlass="nome"> ASawek: Petrykowski/em> para;
div class="phpcode"> Tspan class="default"><?php
$foo i/span>= (/span>'v little string';/span>]&br />&cho isset($doo) ?/span>'ves ':/span>= /span>'vo ';/span>] $doo)'vaaa']/span>]) ?/span>'ves ':/span>= /span>'vo ';/span>]
&nt;
&/span>$rstlt"swith munexpect(:/r />&/span>$es yes

AWel
; $t is anecessaryti check ptyp if tfoo ifirs =!/span><<
up
down
153
Aanonymousleafat gmail dot com Tsset (expect the sariable isignifirs , so ou can 't addparaenhe se or nanyhisng

<?php
= (/span>$1/span>]
    }f isset(<
$doo) {<// nSyntaxerrorsiat sset(<    unbsp; unbsp; u/span>$moo i/span>= (/span>$2bspan>]
    }
}/span>$><<
title="52% like this..."> 18
tlass="nome"> Auramihsayibok,gmail ,com para;
div class="phpcode"> Nowtethat psset() rs ant crecursiv ansif the f5.4.8 Iuhav anvil ble hare io test iith :if you use is hon amultipdientson.alarray.or nanobject(is hill oot bheck psset() ron each dientson.ansis hgos.

EImagin< ou uhav anclass=with maootrmala__sset (nd ara__et phat pfaallsfor oon-oxpstsantproperties .isset($abject(>
<?php

)lass=w/span>$FaallOnGe ={br />
   
// npreted ahat phe fmethodsuhav aiple ent"aion.sthat pnctuallybtryti cdo ordkbr />   
   
=ublic n__et /span>;$vome"bspan>] {
    unbsp; unbsp; ucho i/span>"d(et ng.={bspan>$vome"bspan>]}/span>")<]&br />&/r />    unbsp; unbsp; u/span>// infproperty ooes tot bxpsts{
    unbsp; unbsp; unbsp;  
=cho i/span>"dPoperty ooes tot bxpsts!]&br />    unbsp; unbsp; unbsp;       unbsp; unbsp; u/span>// i
   
]}/r />&/r />    uublic n__iset(/span>;$vome"bspan>] {
    unbsp; unbsp; ucho i/span>"d(sset (/span>={bspan>$vome"bspan>]}/span>"?)<]&br />    unbsp; unbsp; u/span>// ieturn     unbsp; unbsp; u/span>]eturn ialse]
    }
}br />}

$bbjei/span>= (ew sFaallOnGe /span>(<;

// iordksbr />)cho i/span>"dTst ng.infp>]&br />&f ($dbje/span>]>jnosuch/span>));?cho i/span>"dYs">/span>]& lse<{cho i/span>"dNo]&br />&/r />&/span>// ifaallsbr />)cho i/span>"d\nTst ng.infp>]&br />&f ($dbje/span>]>jnosuch/span>)>foo) ;?cho i/span>"dYs">/span>]& lse<{cho i/span>"dNo]&br />&/r />&/span>// iot bxpecutd
)cho i/span>"d\nTst ng.infp>]&br />&f ($dbje/span>]>jirrelevant/span>) ;?cho i/span>"dYs">/span>]& lse<{cho i/span>"dNo]&br />&/r />&/span>$>

Tnbsp;   }Tst ng.infp>Tnbsp;   }Tst ng.infp>&/r />&Unomment"ote fcho sintthi fmethodsund aou 'l bseeixamtly.&/r />    uTst ng.infp>Tnbsp;   }Tst ng.infp>&/r />&O a shimila not e,infp__et palwaysieturn sbut histead osseus tarnings or notices.thi tthios iill osurface /span><
up
down
152
Asoywiziat hp?oot cne /sm> NSometmes: ou uhav ai check pf a array.uhas some eyws. To achiev is hou can jse iisset" 2ike this.:isset($abray.['eyw1'] $abray.['eyw2'] $abray.['eyw3'] $abray.['eyw4']
&Yu uhav ai crite<$abray.palltimes:und at is arete

<?php
$bspan>]unction nsset(_rray./span>(<;{
    uf (
aunct_num_rrg(<) lt;?(/span>$2bspan>])ueturn irue]
    }/span>$drrg<= nfnct_gt(_rrg(<)
    }/span>$drray.u/span>= nrray_kshif(/span>;$vrrg(
    }f (nss_rray./span>($drray./span>) ;?eturn ialse]
    }foreach
$vrrg=a=w/span>$$nbspan>])uf ($abray./span>)$enbspan>]] ;?eturn ialse]
    }eturn
irue]
&
}/span>$>

TUse:isset(_rray.$abray., 'eyw1', 'eyw2', 'eyw3', 'eyw4'
&Firs <
down
19
ACung cHuy To/em> N1)& br /><?php< br />$bspan>]nset($munefain( < br />$bspan>$enll //span>= null;< br />&f (irue= ==$dunefain( ){cho i/span>"'sset()vunefain]< lse<{"'sset()vunefain)) u/span>// i'sset()vunefain$bspan>]f (irue= ==$dull; ){cho i/span>"'sset()vull<)=== ]< lse<{"'sset()vull<)=== )) nbsp;   unbsp; unbsp; unbsp;   // i'sset()vull<)nbsp;   $bspan>$>< br />$bspan>< br />$2) If& br /><?php< br />$bspan>]nset($munefain( < br />$bspan>$enll //span>= null;< br />& br />&f (irue=!= irray_key_exists("'unefain] igt(_efain(<) {"'munefain]<< lse<{"'munefain)<< /span>// i'munefain$bspan>]f (irue= ==irray_key_exists("'ull<;/span>] igt(_efain(<) {"'mnll /xpsts<)<< lse<{"'mnll /oes tot bxpsts;/span>]<// i'mnll /xpsts<< br />$bspan>$><<
down
18
AAshus/em> Nowtethat prray.ueyws
<?php
<$r
['vw])= irue]
&br />$bspan>$ar_dump(isset($ab['vw])),/r />    unbsp; usset($ab['vW])
?>

Till orepor:
Aool(true)<
down
18
Arandllygirardiat hotail dot com The funexpect& br />& br /><?php< br />$bspan>]unction nss_et(/span>;$$ariome"bspan>] $earaenh/span>= /span>null= {< /r />  &f (< !/span>nss_rray./span>(<$earaenh//span>= {amp;& i!/span>nss_bject($daraenh/span>=) {< /r />    }/span>$daraenh//span>= imGLOBALS/span>[; /r />  &} /r />  &eturn irray_key_exists($$ariome"bspan>] $earaenh//span>= ; /r />&} br />$bspan>$>< br />$bspan>< br />$Henc ,a$ariome"shoruldbe da mix& br /><
title="521 like this..."> 17
tlass="nome"> AAnl zseygin/em> para;
div class="phpcode"> Nowte: Beausid his ii a slanguge=icontrouc (nd aot basunction wih/an ot be dalli
ESo ohyat is aunefr "Vriable whandlng aFnction .". Maybe hi e dhoruldbe dsome goodidocuent"aion.ifieldbor olanguge=icontrouc s /span><
up
down
1
Apackard_bell_neciat hotail dot com Nowte: sset() ronlyiceck saariable sansianyhisng lse<{ill oretlt"inttaiarae<{crors.aInotherwoord" the ifllowing cill oot bordk: sset()trim(vome")).

<
down
1
ATe aCee/em> NI srespont ai c10-Feb-0086 06:02,psset()va)
<?php
]nset($m-) u/span>// or ce $cpi cbeanset(br />$bspan>$ar_dump(i/span>$ab/span>= amp;v/span>$m-) u/span>// aNU2L but hhis iactuallybet(s $abnd aecio tei f'ame 'aNU2L.br />$bspan>$ar_dump(isset($a-)) u/span>// aool(false)
$bspan>$ar_dump(i/span>$ab//span>= i5/span>-) u/span>// aint(5
$bspan>$ar_dump(i/span>$a-) u/span>// aint(5
$br />]nset($m-) br />$bspan>$ar_dump(i/span>$ab/span>= amp;v/span>$m-) u/span>// aNU2Lbr />$bspan>$ar_dump(isset($a-)) u/span>// aool(false)
$bspan>]nset($m-) br />$bspan>$ar_dump(i/span>$ab//span>= i5/span>-) u/span>// aint(5
$bspan>$ar_dump(i/span>$a-) u/span>// aNU2Lbr />$bspan>$>

TItthi following cxample , webseeia erltrnote"fmethodif thst ng.infp variable is sactuallybet(or not :br /><?php
(i/span>$rray_key_exists("'c;/span>] /span>$gt(_efain(<) u/span>// aalse) br />$bspan>$ar_dump(isset($a-)) nbsp;   unbsp; unbsp; unbsp;   // ilseo flse
$ar_dump(i/span>$a-) nbsp;   unbsp; unbsp; unbsp;   // ianuipulte"fecia bit.."br />$ar_dump(i(tring")/span>$a-) br />$ar_dump(i/span>$pingt_($a- /span>$rue]
?ar_dump(i/span>$ab/span>= /span>$a-) br />$ar_dump(i/span>$rray_key_exists("'c;/span>] /span>$gt(_efain(<) u/span>// a .. stll bflse
$ar_dump(i/span>$a= iNU2Lbspan>]) nbsp;   unbsp; unbsp; unbsp;   // ihis ist(s $cbr />$ar_dump(i/span>$rray_key_exists("'c;/span>] /span>$gt(_efain(<) u/span>// arue$bspan>$ar_dump(isset($a-)) nbsp;   unbsp; unbsp; unbsp;   // aalse); sset() rstll bsaysiit'smnset(br />$bspan>]nset($m-) nbsp;   unbsp; unbsp; unbsp;   // ilctuallybnset(iitbr />$ar_dump(i/span>$rray_key_exists("'c;/span>] /span>$gt(_efain(<) u/span>// aalse)br />?ar_dump(i/span>$ab/span>= amp;v/span>$m-) nbsp; unbsp; unbsp;   $ar_dump(i/span>$rray_key_exists("'c;/span>] /span>$gt(_efain(<) u/span>// arue$bspan>]nset($m-) nbsp;   unbsp; unbsp; unbsp;   // inset(iit again/r />$ar_dump(iamp;v/span>$m-) nbsp; unbsp; unbsp;   // iamp;vNU2Lbr />$bspan>$ar_dump(i/span>$rray_key_exists("'c;/span>] /span>$gt(_efain(<) u/span>// arue$>

TObviously,onll /arlues takesup pance (o ote yhiould 't sow tup in gt(_efain<
down
14
Atomek/em> NHers'smaosiple & br /><?php< br />$bspan>]unction nss/span>(i/span>$avb[) br />${ br />&f ($avb[);?eturn ialse]< br />&f (iavb[!= /span>$alse])ueturn irue]< br />&eturn ialse]< br />&} br />$bspan>$>< br />$bspan>< br />$Nw tistead oofiaery popultr (ut hisarli in ianuyosituaion .): br />& br />&f (& br />&ou can jrite<: br />& br />&f (<
down
14
Amarkdot cfabrizioat gmail dot com NI knwiaiis ii aprobaleybot ihe ireommentd
EFr oxample , istead oof:br /><?php
<$est i/span>= $a_REQUEST/span>='vest ]) ?$e_REQUEST/span>='vest ]) :$ull;
$>
<?php
<$est i/span>= <@/span>$e_REQUEST/span>='vest ])
?>

TI beaves some typng" but hoes 't giv ahe iopportunitywt oprovidemaoefault"iarlue.aIf vest <
down
13
Abnanllfre at gmail dot com Tsset ()ieturn sbTRUEinfp varlue s sNU2L. Tat petemsawong co gme as hi e ds sotvwayti cdit ng.uish betwen s varlue et (o tNU2Lind araruelybnsdfain& br />&If& br /><?php< br />$bspan>]lass=$T br />$bspan>]< /r />  &unction n__iset(/span>;$vath/span>=) /r />  &< /r />    }/span>$darop<= ngt(_bject<_ari($dihi /span>-) /r />    }eturn irray_key_exists($vath/span>= $earop-) /r />  &} br />&} br />$ br />$bspan>$ex= sT/span>=( < br />$bspan>$ex/span>=>foo= n4bspan>]< br />& br />$bspan>$ar_dump(isset($ax/span>=>foo[); u/span>// aTRUE br />$bspan>$ar_dump(isset($ax/span>=>fbr_dxists[); u/span>// aFALSE br />$bspan>$>< br />$bspan><[EDITORaihigo< NOTE: hes wsnippt (has improveent"sibyu"Pult Lashbrook"]/span><
down
11
AHaylywbWatson/em> NIf
EIt'smot bjustai check pfrsbnsiniioalis<
down
14
Aallut t(at)ail (dot)aed(dot)upenn(edu)/em> NI sPHP 5.2.3,sreallybeturn sbrue<
down
12
Aqtremy/em> NSiple br />
<?php
]unction n_iset(/span>;$varlbspan>])u{$avblbspan>]);
}/span>$><<
down
1-1
ARobertdot cVanDel oah/absdot com NHers'smaoniceliktte & br /><?php< br />$bspan>]ifisset($ab/span>[); br />${ br />    }/span>$dbf/span>= nab/span>[< br />&} br />$lse< br />${ br />    }/span>$dbf/span>= 'default">/span>[< br />&} br />$ br />$unction nssst(_b($m= $erltrnote"f/span>= nNU2Lbspan>]) br />${ br />    }eturn $a=) ?$eheck p/span>=:$erltrnote"/span>[< br />&} br />$ br />$/span>// aEample sege=: br />$bspan>$efirst_ome"s/span>= nssst(_b($e_POST/span>='vfirst_ome"]) 'dEmpty>/span>[ < br />$bspan>$etoallnbsp; unbsp; = nssst(_b($erow/span>='veoall]) $0/span>[ < br />$ br />$bspan>$><<
down
1-3
Amarcioat gsuhdodot com N//hisnvwayti cet(baiariable br /><$foonbsp; u <
down
1-3
Abeuciat beuciot cnt(/sm> NBewareboet thi fchk()
<?php
]unction nchk/span>($mvb[) {br />  &f ($avb[);/r />    }eturn iNU2Lbspan>];/r />  &ese)br />iavb[;/r />&}br />
''lt;?pregt;<]
?ab//span>= ?ar_dump(i/span>$ab/span>=
?chk/span>($ab/span>='vb])
?ar_dump(i/span>$ab/span>=
''lt;?/pregt;<]
$/span>// aGives:br />$bspan>$><<
down
1-1
Akurdtage=at gmail dot com The few <(as ofsPHP7) 'ull< coalesc iop
<?php
// aFetch"sshe iarlueiofi$_GET['ser"') nd areurn sb'nobody'br />$$sernome"s/span>= n$_GET/span>='vser"'/span>]) ??'vnobody'bspan>]
// aTis ii aequiarlentpi :br />$$sernome"s/span>= $a_GET/span>='vser"'/span>]) ?$e_GET/span>='vser"'/span>]) :'vnobody'bspan>]
// aCoalescng.ian jbsychain$$sernome"s/span>= n$_GET/span>='vser"'/span>]) ??$e_POST/span>='vser"'/span>]) ??'vnobody'bspan>]
?>

TQuted"vfromna href="#http://hpc.netmanual/ven/migraion 70.new-feteures.hpc#migraion 70.new-feteures.ull<-coalesc -op"http://hpc.netmanual/ven/migraion 70.new-feteures.hpc#migraion 70.new-feteures.ull<-coalesc -op/a><
title="V49 like this..."> 1-4
tlass="nome"> Apiansts para;
div class="phpcode"> Thei aunction
<?php

= n$_GET/span>='vc;/span>]];br />n$ids/span>= n$_GET/span>='vid;/span>]];nbsp;   }/r />n$crors//span>= 'vesmplte"s/crors.tpl];br />
$acat/span>[);/r />    }{/r />    }nbsp;   }if($aid/span>[);/r />    }nbsp;   }{/r />    }nbsp;   }nbsp;   }/span>n$vas//span>= 'vesmplte"s/pics/'//span>=.$ehats/span>=.'v-'//span>=.$eids/span>=.'v.tpl];br />iis_fil /span>(i/span>$avb[);/r />    }nbsp;   }nbsp;   }{/r />    }nbsp;   }nbsp;   }nbsp;   }fncludei/span>$avb[);br />    }nbsp;   }nbsp;   }nbsp;   }fncludei/span>$acrors/span>[);br />    }nbsp;   }nbsp;   }/span>n$vas//span>= 'vesmplte"s/pics/'//span>=.$ehats/span>=.'v.tpl];nbsp;   }nbsp;   }/r />iis_fil /span>(i/span>$avb[);/r />    }nbsp;   }nbsp;   }{/r />    }nbsp;   }nbsp;   }nbsp;   }fncludei/span>$avb[);br />    }nbsp;   }nbsp;   }nbsp;   }fncludei/span>$acrors/span>[);br />    }nbsp;   }includei/span>'vesmplte"s/rltrnoteive.]./span>$tpl/span>[);br />
?>

TYu can jseeisxv ralans<
up
down
1-2
Aayyapan cot cashokat gmail dot com TReurn
<?php
= /span>$NU2Lbspan>];/r />&/span>$ab/span>= /span>$FALSEbspan>];// he foutipu pwaspTRUE.u/r />$m- /span>$TRUEbspan>];/r />&/span>$ad/span>[ /span>'v]
?a /span>( /span>'">/span>[
$ab/span>=)):br />'"TRUE>/span>[
'"FALSE>/span>[?>
<
down
1-6
Asoapergemat gmail dot com NBeowiaacns
EIstead oofihakig cnpahe iaance i cdfain<
<?php

= $a_POST/span>='vari'/span>]) amp;vamp;v !empty$a_POST/span>='vari'/span>])
?>

Tnlsocbeawan <
down
1-5
Aonnoat gitmazedot com NI sPHP4 the iollowing thorksias expeced": br />& br /><?php< br />$bspan>]if$aobjbspan>]>fhisng/span>='veyw'/span>]) )&< /r />  &nset($mobjbspan>]>fhisng/span>='veyw'/span>]) < br />&} br />$bspan>$>< br />$bspan>< br />$I sPHP5 howxv rhou cill ogt(baifaallocrorsfor ohe inset(<). br />& br />&he hork aroun as : br />& br /><?php< br />$bspan>]ifiis_lray./span>(i/span>$aobjbspan>]>fhisng/span>= amp;vamp;v sset($aobjbspan>]>fhisng/span>='veyw'/span>]) )&< /r />  &nset($mobjbspan>]>fhisng/span>='veyw'/span>]) < br />&} br />$bspan>$><<
down
1-4
Aflobee/em> NKISS: rray_key_exists<()fispofte <?php
('vsecur"]) null;
$erray./span>('vsecur"]) ) ) {br />    }/span>// ahet phappens etre?br />$bspan>]}bspan>$><<
down
1-9
strong class="user">AAnonymous/em> NI do 't knwiasfnpretty serful (or oreadabll itywa iieast):br />
<{br />    }if(    }nbsp;   }reurn <$v;br />&}br />

<$a <4
<$c +=<$c +=

niould hork anyway but hou cge thi fpoint /span><
down
1-10
Asoapergemat gmail dot com NI (s spossile ih cencapsulte"fsset ()iallis istidemyouriwn<&unction sasf
<?php

$bspan>]unction nsfet r"/span>(iamp;v/span>$mvb; $eefault"i/span>= null;)br /><{br />    }eturn $avblbspan>]) ?$earls/span>=:$eefault"/span>;
<}br />
// nbsp;   }eample sege=br />?aids/span>= nintvblbspan>]i/span>iifet r"/span>(i/span>$a_GET/span>='vid;/span>]] $0/span>[
?><<
down
1-10
Arobertoat gspadimdot com Nes-t:br />
<?php
<$qn"/span>; /span>$100000/span>]
?ak/span>(=rray.(
$ai/span>; /span>$0/span>]$ai/span>;lt;?bspan>$aqn"/span>;$ai/span>;++)br />?ak/span>($ai/span>;] /span>$1/span>]
$microimes/span>(i)./span>'"\n>/span>[
$ai/span>; /span>$0/span>]$ai/span>;lt;?bspan>$aqn"/span>;$ai/span>;++)ifisset($ak/span>($ai/span>;])
$microimes/span>(i)./span>'"\n>/span>[
$ai/span>; /span>$0/span>]$ai/span>;lt;?bspan>$aqn"/span>;$ai/span>;++)ifibspan>$rray_key_exists(i/span>$ai/span>;,/span>$ak/span>()
$microimes/span>(i)./span>'"\n>/span>[
$ai/span>; /span>$0/span>]$ai/span>;lt;?bspan>$aqn"/span>;$ai/span>;++)ifibspan>$ak/span>($ai/span>;] /span>$1/span>]
$microimes/span>(i)./span>'"\n>/span>[
?>

The iintnrst ng.irstlt":br /><
down
1-10
Akariedoo/sm> NBeor e:br />
<$numberu <$ge=a <$troet (
Tnftnr: ->
< $ara;mtersa < {br />    }$$ome"( < }/cpan><
down
1-12
Asam b/sm> NCeck poutiihns ifet r"hunction . If $vas/s set&, dosot isng, oher wisem$vas/= eefault"

<?php

$$ome"s/span>= nsfet r"/span>(i/span>$aome"/span>; 'vefault"iome"])

Tunction
nsfet r"/span>(iamp;v/span>$mvb"/span>( $eefault"/span>;)br />    }nbsp;   }eturn $avb[) ?$ears//span>=:$eefault"/span>;)
?><<
down
1-13
Ai [at] nemde>n [dot] om NSiple but haery serful:br />
<?php
]unction nsset&O"/span>(i/span>$avb"/span>( $ers//span>= $alse;) {/r />  &eturn $avb[) ?$ears//span>=:$er"/span>(
<}br />?>

TSoestxample :br /><?php
= 'v1]
?abf/span>= ''2]
$sset&O"/span>(i/span>$ab/span>=,/span>$sset&O"/span>(i/span>$ab/span>=,/span>$3/span>() <}/span>// a1br />?>
= ''2]
$sset&O"/span>(i/span>$ab/span>=,/span>$sset&O"/span>(i/span>$ab/span>=,/span>$3/span>() <}/span>// a2br />?>
(ehor$sset&O"/span>(i/span>$ab/span>=,/span>$sset&O"/span>(i/span>$ab/span>=,/span>$3/span>() <}/span>// a3/r />?><<
title="V48 like this..."> 1-11
tlass="nome"> Ajciot cmichelat gsymerinedot com para;
div class="phpcode"> NUssng/r />  &sset(  &sset(  &ss_lray.

<?php
<$exf/span>= ''on '//span>==gt;''arl1],/span>'vewo'//span>==gt;''arl2]
''$exf= ]?pingt_"/span>(i/span>$aex/span>]
'"lt;?r />gt;<>/span>[
'" sset(<\aex['on ']['tref=') : >/span>[
$aex/span>]'von '/span>]]'vtref='/span>]]);/r />    }ehor''tru='/span>]
    }ehor
''alse<];br />
'"lt;?r />gt;<>/span>[
'"ss_lray.<\aex['on ']) amp;vamp;vnbsp; &sset(<\aex['on ']['tref=') : >/span>[
$ss_lray./span>(i/span>$aex/span>]'von '/span>]] amp;vamp;v sset($aex/span>]'von '/span>]]'vtref='/span>]]);/r />    }ehor''tru='/span>]
    }ehor
''alse<];br />?>

Tshow :br /><$exf= Aray. ( [on ] =gt;    }nbsp; }nbsp;   }{/r />&/r />    }nbsp; }nbsp;   }includei"qnd a/in_quphp?");br />    }nbsp; }nbsp;   }{/r />    }nbsp;   }nbsp; &includei"qnd a/add_answerphp?");br />    }nbsp; }nbsp;   }{/r />    }nbsp;   }nbsp; &includei"qnd a/in_anphp?");br /><{br />&includei"qnd a/view_qnd aphp?");br /><}br /><
up
down
1-15
Ajohn t tdarvendot comdot cuk/em> NI (s sordthsot sngthat pi. o dr ot check por ohe ixistsenc"sof a eywfwth i. n clray., regardlesesof it'smodntentsoon ishouldbse irray_key_exists<()fot bsset () nbsp; &/r />&/r />&sset ()iill o(correctly)&eturn <
down
1-18
Agochoaloxat gmail dot com NUsrful t check pifihe iariable ihav som iarlue.."epecilly"por oGET POSTiariable s br />& br /><?php< br />$bspan>]unction nsset&_r"/span>(iamp;v/span>$mheck /span>( $erltrnote"s/span>= nNU2Lbspan>]) br />$< /r />    &eturn $aheck /span>()) ?<(empty$aheck /span>() ?$erltrnote"s/span>=:$eheck /span>() :$erltrnote"/span>]; br />&} br />& br />ngetGETPOST/span>=$avb[) br />$< /r />      &eturn nsset&_r"/span>(i/span>$a_GET/span>=$avb[],/span>$sset&_r"/span>(i/span>$a_POST/span>=$avb[],/span>'"Empty>/span>[))< br />&} br />$bspan>$>< br />$bspan>< br />$Eample br /><
down
1-21
Adavidmt thi gllyager oot cnet/em> NJustat creitrnte"sn <
down
1-18
Ajon/em> NSinc"sPHP ill oheck pcae:br />
<?php
]if$avb[) amp;vamp;v /span>$avb[) {br />    }/span>// adocsom hisng/r />]}br />?>

TIncshore,infpou chav crorsfreporeig cn ,hnd aevas/s sot bet&, PHP ill ogtnrnte"san crorsfnfpou cjustahav :br />
<?php<}/r />]if$avb[) {}/span>// adocsom hisng }br />?>

T.."bu&, a sot ed}ese)wi r", ill oreurn <?php
]if$avb[)) {}/span>// adocsom hisng }br />?>

TCeck sng bothih cse
<?php
==$avb[) amp;vamp;v /span>$avb[) ?$ears//span>=:''newiarlue];br />?><<
down
1-6
Aohccmt t163dot com NWi ttacariable is set& andvitsiarluesfs sull<,hsset ()ireurn s

<?php
= null;
nari_dump/span>(ibsset ($ewxcs/span>=) );br />nari_dump/span>(ib/span>nrray_key_exists(i''wxc],$eGLOBALSs/span>=) );br />?>

TOutputbofihe iabovstxample :&/r />&/r />&bool(alse<) bool(tru=;/span><
down
1-6
Aericgmt tsr y-web-designdot com NI foun asom hisng intnrst ng.iwhil horkng.iwitehsset ()ii sPHP5.5+br />
<?php

= '"foo>/span>[
$ebf/span>= '"bar>/span>[
?ari_dump/span>(isset($ab/span>=,$eb/span>=) <}/span>// reurn s
]nset($mb/span>=)
?ari_dump/span>(isset($ab/span>=,/span>$eb/span>=) <}/span>// ireurn s=.."/span>$BUT/span>=.."/r />?ari_dump/span>(i!sset($ab/span>=,/span>$eb/span>=) <}/span>// iSTILL reurn s<<
down
1-1
Adominiciot cmayes ag cmeditaion studiesdot corg/em> Nhe doc sy.s hat psset (deftnrminesasfhttp://hpc.netmanual/ven/types.om1araisn sphp?./a>< br />

<
down
1-28
Ajwvdver oa gmail dot com NH e dacshoresot esn
<?php
]unction nss/span>=$avb[)br /><{br />&if i!sset($avb[)) eturn nalse;<}/span>/# Vriable is salwy.s et&... Oher wisemPHP iould hav thrwn<&an crorsfn ]if$avb[! /span>$alse;) eturn ntru=bspan>;<}/span>/# So, 0,cNU2L, andvsom ioher oarluesfmy. ot bbehav ike tisNot? Andvwat paboutiih doif=rsenc"sbetwe ttaclass= andvNU2L?/r />]eturn nalse;<}/r /><}br />?>

TTe ireasn

<
down
1-29
Ahp?nt (dot 5(dot reinhold0080 mt thgspamgourmt (dot om Nifhou cwantpi check pwi her ohi &ner"ihas sentppots}ari auromtacor m,fs"is sa painbi cwriteasom hisng ike thi iorllowsng, sinc"ssset ()idoe ot bheck por ozero-e ogtehtring" :br />





<?pbr />]unction npotsari /span>=<) {br />    }or each(/span>nanct_get_arg /span>=<) as /span>$avb[) {br />    }nbsp; }nbsp; &ifi!sset($a_POST/span>=$avb[]) || /span>$a_POST/span>=$avb[]/===s/span>'']) eturn nalse;
ntru=bspan>;<}br />?>

Txample :&ifipotsari ('or m_ome"','or m_mesege=')) [.."]bcpan><
down
1-37
Arichard ill iam lee ATgmail /em> NJustaasot esn
<
/div>
?a href="/manual/vaddnote.php?isectfunction.isset&vreoirectfhttp://fr2.hpc.netmanual/vja/unction.isset&php?">addaasot eaddaasot e/cpmal<>/a> /cpetion.>