By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

How to emulate isset() ? (is it possible?)

P: n/a

Take a look at this code (you can execute it):

error_reporting(E_ALL);

function byVal( $v) {}
function byRef(&$v) {}

print '<pre>';

byVal ($first['inexistent_index']); // gives a notice
var_dump($first); // gives a notice

print '<hr />';

byRef ($second['inexistent_index']); // does NOT give a notice
var_dump($second); // does NOT give a notice

print '<hr />';

isset($third); // does NOT give a notice
var_dump ($third); // gives a notice

print '</pre>';
In the $first case, using byVal(), I get *two* notices.
In the $second case, using byRef(), I get *zero* notice.
In the $third case, using isset(), I get *one* notice.

This means that:

1) byVal() does NOT define the array and raises a notice
(and var_dump() raises another notice).

2) byRef() defines the array and does NOT raise notices
(neither var_dump() raises a notice, since $second is defined).

3) isset() does NOT define the array and does NOT raise notices
(but var_dump() raises a notice, since $third is NOT defined).

As you can see, isset() is weird, and I need to emulate its behaviour.

The question is: is it possible to do that in PHP?

Greetings, Giovanni
May 29 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Giovanni R. wrote:
Take a look at this code (you can execute it):

error_reporting(E_ALL);

function byVal( $v) {}
function byRef(&$v) {}

print '<pre>';

byVal ($first['inexistent_index']); // gives a notice
var_dump($first); // gives a notice

print '<hr />';

byRef ($second['inexistent_index']); // does NOT give a notice
var_dump($second); // does NOT give a notice

print '<hr />';

isset($third); // does NOT give a notice
var_dump ($third); // gives a notice

print '</pre>';
In the $first case, using byVal(), I get *two* notices.
In the $second case, using byRef(), I get *zero* notice.
In the $third case, using isset(), I get *one* notice.

This means that:

1) byVal() does NOT define the array and raises a notice
(and var_dump() raises another notice).

2) byRef() defines the array and does NOT raise notices
(neither var_dump() raises a notice, since $second is defined).

3) isset() does NOT define the array and does NOT raise notices
(but var_dump() raises a notice, since $third is NOT defined).

As you can see, isset() is weird, and I need to emulate its behaviour.

The question is: is it possible to do that in PHP?

Greetings, Giovanni

Hi

what exactly is the desired behaviour of that new, emulated isset?

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 29 '07 #2

P: n/a
gosha bine <st********@gmail.comwrote:
what exactly is the desired behaviour of that new, emulated isset?
The fact is that I'm lazy. ;-)

I'd like to use a single function - a kind of wrapper for isset() - to
check whether the $var isset(), and to sanitize it according to my will.

Something like this:

function wrapper(&$var) {

if ( !isset($var) || !strlen($var) ) return '';

$var = trim ($var);

// other checks

return $var;

}

Here it is how it could be used:

print wrapper($array['inexistent_index']);

In this way, even if $array['inexistent_index'] isn't defined, I don't
get a notice. The fact is that wrapper() adds that index to the array
and sets $array['inexistent_index'] to NULL. :-(

So I was asking myself how isset() works and if a similar function could
be developed using PHP or not.

Giovanni
May 29 '07 #3

P: n/a
Giovanni R. wrote:
gosha bine <st********@gmail.comwrote:
>what exactly is the desired behaviour of that new, emulated isset?

The fact is that I'm lazy. ;-)

I'd like to use a single function - a kind of wrapper for isset() - to
check whether the $var isset(), and to sanitize it according to my will.

Something like this:

function wrapper(&$var) {

if ( !isset($var) || !strlen($var) ) return '';

$var = trim ($var);

// other checks

return $var;

}

Here it is how it could be used:

print wrapper($array['inexistent_index']);

In this way, even if $array['inexistent_index'] isn't defined, I don't
get a notice. The fact is that wrapper() adds that index to the array
and sets $array['inexistent_index'] to NULL. :-(

So I was asking myself how isset() works and if a similar function could
be developed using PHP or not.

Giovanni

I'm afraid that's not possible, Giovanni. "isset" is a special function
in that it doesn't evaluate its argument before call. It isn't possible
to write such function in php.

The usual workarounds are to use '@' operator to suppress notices

wrapper(@$array['inexistent_index']);

or to split array[index] into two distinct arguments:

wrapper($array, 'inexistent_index');

Even better would be to use an OO wrapper with getter method(s)
"get($index)" or "getSomething".

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 29 '07 #4

P: n/a
Giovanni R. kirjoitti:
As you can see, isset() is weird, and I need to emulate its behaviour.
Isset can't be emulated, because it's not a function, it's a language
construct, similar to echo and unset. You can't replace it's
functionality with a wrapper function, because it's not a function in
the first place. That's why it's "wierd" I suppose. However it returns a
value, and can be used as an expression, so the analogy to echo is not
complete. Echo is even more weird in that sense.

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 29 '07 #5

P: n/a
Giovanni R. wrote:
is it possible?
Not really -- it's a language construct rather than a normal function.

If you really want to emulate it, you'll probably need to write an
extension to PHP (in C).

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 95 days, 3:02.]

Non-Intuitive Surnames
http://tobyinkster.co.uk/blog/2007/0...tive-surnames/
May 29 '07 #6

P: n/a
gosha bine <st********@gmail.comwrote:
I'm afraid that's not possible, Giovanni.
Never mind, it's not a big problem. :-)
The usual workarounds are to use '@' operator to suppress notices
wrapper(@$array['inexistent_index']);
Hey, when using the @ operator that index is NOT added to the array!
Good: it's almost the same result of isset(). :-)

I hadn't tried that code before because, as far as I *knew*, the @
operator only suppressed notices, while the real problem was that
byRef() was adding the index to the array, besides raising the notice.

I'm now asking myself if this is not a bug - an undocumented feature. :)

Thanks to all for your answer.

Giovanni

May 29 '07 #7

P: n/a
"Giovanni R." <li*******@NOSPAMlibero.itwrote:
Here it is how it could be used:

print wrapper($array['inexistent_index']);

In this way, even if $array['inexistent_index'] isn't defined, I don't
get a notice. The fact is that wrapper() adds that index to the array
and sets $array['inexistent_index'] to NULL. :-(
This code will say you all about the existence of the $array and its
element 'inexistent_index', also if this element is NULL:

if( isset($array) ){
echo "the array exists\n";
if( array_key_exists('inexistent_index', $array) ){
echo "the element array[inexistent_index] exists\n";
if( $array['inexistent_index'] === NULL ){
echo "...and it is NULL\n";
} else {
echo "...and it is not NULL\n";
}
} else {
echo "the element array[inexistent_index] does not exist\n";
}
} else {
echo "the array does not exist\n";
}

Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

May 30 '07 #8

P: n/a
Umberto Salsi <sa***@icosaedro.italiawrote:
This code will say you all about the existence of the $array and its
element 'inexistent_index', also if this element is NULL: ...
Umbe', scusa, ma a che mi serve? ;-)

Anyway, I used your code and it confirms that: byVal($array['index'])
does not create neither the index nor the array and generates the
notice; byRef($array['index']) does not generate the notice, but creates
the index and the array; byRef(@$array['index']) does not create neither
the index nor the array, and does not generate the notice, like isset().

Ciao, Giovanni

May 30 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.