473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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


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
8 2437
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
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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
4
by: Brian Olivier | last post by:
Hello, What I try to do is create a string variabel with some intelligence. I want this variabel to read the information sent by another page and interpret the result. The idea is that is a...
3
by: Zenobia | last post by:
how do I emulate the start attribute of ol tag deprecated in XHTML (strict)? Is it easily possible or only possible with great difficulty?
5
by: juglesh | last post by:
"$string = isset($xyz) ? $xyz : "something else";" Hello, someone gave code like this in another thread. I understand (by inference) what it does, but have not found any documentation on this...
61
by: /* frank */ | last post by:
I have to do a homework: make a CPU simulator using C language. I have a set of asm instructions so I have to write a program that should: - load .asm file - view .asm file - do a step by step...
9
by: wouter | last post by:
hey hi..... I wanna make a switch wich does this: if pagid is set do A, if catid is set do B, if projectid is set do C, else do D. So i was thinking something like this:
1
by: Pom | last post by:
Hello how can I emulate a serial port in windows? I want to intercept data sent to a 'com'port by a proprietary program. It sends statistics to a serial display, and I want that data in my...
2
by: sathyashrayan | last post by:
Dear group, My question may be novice. I have seen codes where the isset() is used to test weather a user's session ($_SESSION) is set before entering a page. A kind of direct access to a page is...
14
by: Eugeny Myunster | last post by:
Hello all, How can i emulate sizeof() only for integers?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.