Bobonov wrote:
even if all the 3 solution works, only one is the best in performance terms.
In fact, I agree with your performance concerns. The third option is certainly the slowest for the reasons you state; however, the second option could be as efficient as "isset" or as inefficient as the combination of "array_keys" and "in_array" depending on the implementation. However, these are the options for testing for the existence of an element in a PHP array (or object). The third option is required for PHP 4.0.5 and earlier and the second option is supported in PHP 4.0.7 and later. (In PHP 4.0.6, the "array_key_exisits" function was named "key_exists".)
The problem with "isset" is that it is not intended for testing for the existence of an array element. That is why it will produce the following warning when error reporting is set to the highest level:
Sy wrote:
This isn't an error as such, but it will cause a warning to be displayed if you have errors set at the appropriate level in PHP.
Line 104. of TinyMCE.module.php, reads:
if (!$this->wysiwygactive ) {if (!$this->wysiwygactive && isset($_SESSION["tiny_live_textareas"]) ) {
Change this to:
if (!$this->wysiwygactive && isset($_SESSION["tiny_live_textareas"]) ) {
I get the warning:
Notice: Undefined index: tiny_live_textareas in C:\eska\modules\TinyMCE\TinyMCE.module.php on line 104
I always have error reporting set to display absolutely everything, which is good practice for development.
The problem is that "isset" is the logical negation of "is_null" (possibly applied to multiple arguments). In fact, the original poster could have changed the line in question to:
Code: Select all
if(is_null($GLOBALS['CMS_VERSION'])) die('Attempt to use ADODB from outside of CMS');
Because "$GLOBALS['CMS_VERSION']" should never exist and be set to
NULL, this test will work either way while still triggering a warning just before the call to "die" if the "CMS_VERSION" array key does not exist. However, there is still a difference between "isset" and "array_key_exisits" that is important in general:
- isset - returns TRUE if the "variable" has been set and the value is not NULL. It returns FALSE if it has never been set, if it has been "unset" (which may not work the way you think it does if the variable is a global variable), or if the variable does exist and the value of the variable is NULL.
- array_key_exists - returns TRUE is there exists an element in the array for that key whether the value is NULL or not. It returns FALSE otherwise.
This may seem like a largely pedantic point and it may well be. However, using "isset" in place of "array_key_exists" is a habit that may lead to bugs that are difficult to identify when array values can be set to
NULL (or, more likely, uninitialized). It is a problem more easily avoided by consistently using "array_key_exists" as intended.
For a truly pedantic experience that clearly illustrates the relationship between "isset" and "is_null" while implicitly demonstrating the potential problem with not using "array_key_exists", try the following:
Code: Select all
<__html>
<head>
<title>isset and is_null test</title>
</head>
</__body>
<p>
isset( $variable ): <?php var_dump( isset( $variable ) ); ?><br />
is_null( $variable ): <?php var_dump( is_null( $variable ) ); ?><br />
</p>
<p>
$variable = NULL;<?php $variable = NULL; ?><br /><br />
isset( $variable ): <?php var_dump( isset( $variable ) ); ?><br />
is_null( $variable ): <?php var_dump( is_null( $variable ) ); ?><br />
</p>
<p>
$variable = 1;<?php $variable = 1; ?><br /><br />
isset( $variable ): <?php var_dump( isset( $variable ) ); ?><br />
is_null( $variable ): <?php var_dump( is_null( $variable ) ); ?><br />
</p>
<p>
unset( $variable );<?php unset( $variable ); ?><br /><br />
isset( $variable ): <?php var_dump( isset( $variable ) ); ?><br />
is_null( $variable ): <?php var_dump( is_null( $variable ) ); ?><br />
</p>
<p>
isset( $arr ): <?php var_dump( isset( $arr ) ); ?><br />
is_null( $arr ): <?php var_dump( is_null( $arr ) ); ?><br />
</p>
<p>
$arr = array();<?php $arr = array(); ?><br /><br />
isset( $arr ): <?php var_dump( isset( $arr ) ); ?><br />
is_null( $arr ): <?php var_dump( is_null( $arr ) ); ?><br />
</p>
<p>
isset( $arr['foo'] ): <?php var_dump( isset( $arr['foo'] ) ); ?><br />
is_null( $arr['foo'] ): <?php var_dump( is_null( $arr['foo'] ) ); ?><br />
</p>
<p>
$arr['foo'];<?php $arr['foo']; ?><br /><br />
isset( $arr['foo'] ): <?php var_dump( isset( $arr['foo'] ) ); ?><br />
is_null( $arr['foo'] ): <?php var_dump( is_null( $arr['foo'] ) ); ?><br />
</p>
<p>
$arr['foo'] = NULL;<?php $arr['foo'] = NULL; ?><br /><br />
isset( $arr['foo'] ): <?php var_dump( isset( $arr['foo'] ) ); ?><br />
is_null( $arr['foo'] ): <?php var_dump( is_null( $arr['foo'] ) ); ?><br />
</p>
<p>
$arr['foo'] = 1;<?php $arr['foo'] = 1; ?><br /><br />
isset( $arr['foo'] ): <?php var_dump( isset( $arr['foo'] ) ); ?><br />
is_null( $arr['foo'] ): <?php var_dump( is_null( $arr['foo'] ) ); ?><br />
</p>
<p>
unset( $arr['foo'] );<?php unset( $arr['foo'] ); ?><br /><br />
isset( $arr['foo'] ): <?php var_dump( isset( $arr['foo'] ) ); ?><br />
is_null( $arr['foo'] ): <?php var_dump( is_null( $arr['foo'] ) ); ?><br />
</p>
<p>
unset( $arr );<?php unset( $arr ); ?><br /><br />
isset( $arr['foo'] ): <?php var_dump( isset( $arr['foo'] ) ); ?><br />
is_null( $arr['foo'] ): <?php var_dump( is_null( $arr['foo'] ) ); ?><br />
</p>
<p>
isset( $arr ): <?php var_dump( isset( $arr ) ); ?><br />
is_null( $arr ): <?php var_dump( is_null( $arr ) ); ?><br />
</p>
<__body>
</__html>