473,748 Members | 7,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ind ex']); // gives a notice
var_dump($first ); // gives a notice

print '<hr />';

byRef ($second['inexistent_ind ex']); // does NOT give a notice
var_dump($secon d); // 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 2459
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_ind ex']); // gives a notice
var_dump($first ); // gives a notice

print '<hr />';

byRef ($second['inexistent_ind ex']); // does NOT give a notice
var_dump($secon d); // 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********@gma il.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_ind ex']);

In this way, even if $array['inexistent_ind ex'] isn't defined, I don't
get a notice. The fact is that wrapper() adds that index to the array
and sets $array['inexistent_ind ex'] 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********@gma il.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_ind ex']);

In this way, even if $array['inexistent_ind ex'] isn't defined, I don't
get a notice. The fact is that wrapper() adds that index to the array
and sets $array['inexistent_ind ex'] 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_ind ex']);

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

wrapper($array, 'inexistent_ind ex');

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

--
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*********@gma il.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********@gma il.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_ind ex']);
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*******@NOSP AMlibero.itwrot e:
Here it is how it could be used:

print wrapper($array['inexistent_ind ex']);

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

if( isset($array) ){
echo "the array exists\n";
if( array_key_exist s('inexistent_i ndex', $array) ){
echo "the element array[inexistent_inde x] exists\n";
if( $array['inexistent_ind ex'] === NULL ){
echo "...and it is NULL\n";
} else {
echo "...and it is not NULL\n";
}
} else {
echo "the element array[inexistent_inde x] 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***@icosaedr o.italiawrote:
This code will say you all about the existence of the $array and its
element 'inexistent_ind ex', 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
11878
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 the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
4
1798
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 variabel is set, a line break is printed. First question is if this is possible? Second if so, what should be the
3
2690
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
1992
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 type of syntax. Any one have links to this shortuct(?) syntax and other types of syntax? thanks
61
16273
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 simulation - display registers contents I tried to search con google with no success.
9
9733
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
5136
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 python program (that display isn't needed). Is this possible?
2
37185
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 not possible. But I tested with the "input type="button" and the clicking of the event is not happened. Can any one tell me why. The code: <html> <body>
14
1715
by: Eugeny Myunster | last post by:
Hello all, How can i emulate sizeof() only for integers?
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.