Previous Thread
Next Thread
Print Thread
Hop To
#223551 03/04/2009 7:03 AM
Joined: Mar 2009
Posts: 15
M
stranger
stranger
M Offline
Joined: Mar 2009
Posts: 15
I come across many strangely used isset(..) syntax in many files, mainly in the scripts folder:
PHP Code

	$BodySig = $Body;
	isset($user['USER_AVATAR']) && $Picture = $user['USER_AVATAR'];
	if (!isset($Picture)) { $Picture = "http://"; }
 

Just look at the second line. And there are many more like these throughout the code.
I cannot help think that this must be remainders of old if(...) statements. Can you explain what is supposed to happen here?

Joined: Dec 2003
Posts: 1,796
Pooh-Bah
Pooh-Bah
Joined: Dec 2003
Posts: 1,796
isset — Determine whether a variable is set

http://us.php.net/isset

I check the manual at php.net when I'm trying to figure out what is going on.


- Allen
- ThreadsDev | PraiseCafe
Joined: Mar 2009
Posts: 15
M
stranger
stranger
M Offline
Joined: Mar 2009
Posts: 15
No need to get cynical. I use my php_manual.chm frequently, but never seen isset been used like that.

As I see it, line 2 in the above code reads as one of the following 2 options:
PHP Code
false and $Picture = $user['USER_AVATAR']; 
or
PHP Code
true and $Picture = $user['USER_AVATAR']; 

This only makes sense if php uses a construct called "complete boolean evaluation". I know this construct from basic, but I've never seen it be used in php.

But hey, maybe php does indeed behave the same and break an evaluation as soon as an "and" clause hits a "false" condition.
Still, even if that's the case, I wonder why you would write your code like that. If you code it like in line 3, it requires only 1 character more, but it would be a proper "if" clause which will never fail. Not even if the syntax rules in a next php version be changed to not break an evaluation upon the first "false".

Just my 2 cents.

Joined: Apr 2007
Posts: 3,940
Likes: 1
SD Offline
Former Developer
Former Developer
Joined: Apr 2007
Posts: 3,940
Likes: 1
lol -- i don't think allen was being cynical at all.. his post perty much mirrors what i might have posted..

php.net is where i refer to all the time wink


and the code makes sense.. $Picture only gets assigned, if there is a variable that is set in the 1st place..

if it's false, the code doesn't continue on...

there are other ways to skin this cat too, but that works.. wink

Joined: Mar 2009
Posts: 15
M
stranger
stranger
M Offline
Joined: Mar 2009
Posts: 15
Thanks Sirdude, but I find it a sound cynical to refer to the manual and quote the isset explanation. Especially since that doesn't answer the question.

I do use my php_manual.chm very often and did so too when I ran into this code. I just never was able to find a construct like the one used, hence my question.

It's one thing to know where to look (php manual and many websites) but it's another thing to know what to look for. Not isset that's for sure...
This morning I woke up and realised I had probably been looking in the wrong place all the time.

Just like Allen did, I too looked for isset, while in fact I should have looked at the && evaluation. And indeed there it's explained perfectly.

I do now understand that this code:
PHP Code
isset($user['USER_AVATAR']) && $Picture = $user['USER_AVATAR'];
if (!isset($Picture)) { $Picture = "http://"; }
 

does exactly the same as this code:
PHP Code
if (isset($user['USER_AVATAR'])) $Picture = $user['USER_AVATAR'];
if (!isset($Picture)) { $Picture = "http://"; }
 

But why would one opt for 2 different styles of evaluating an isset in 2 consecutive lines?
That still puzzles me.

Joined: Apr 2007
Posts: 3,940
Likes: 1
SD Offline
Former Developer
Former Developer
Joined: Apr 2007
Posts: 3,940
Likes: 1
lol, i have bigger fish to fry.. you can ponder all you want, my explanation showed how the logic evaluated..

and i DID re-check over at php.net, just to make sure before i posted.. i guess that makes me 'cynical' laugh

so all is one with the force now, i guess..

and like i said, you can do it many other ways:

eg
PHP Code
$Picture = isset($user['USER_AVATAR']) ? $user['USER_AVATAR'] : 'http://'; 

i'd say it's there more so as a legacy dealio.. threads has been around for awhile now and i've noticed different ways of doing things..

Joined: Mar 2009
Posts: 15
M
stranger
stranger
M Offline
Joined: Mar 2009
Posts: 15
Reading your reply, I'm starting to think that we have been on different wave length from the start.

My problem was not that I don't understand isset. My problem was that I didn't know about the so called "short circuit" evaluation in php.

Ofcourse that's my shortcomming, but that's why I couldn't find an answer in the isset explanation.

Joined: Apr 2007
Posts: 3,940
Likes: 1
SD Offline
Former Developer
Former Developer
Joined: Apr 2007
Posts: 3,940
Likes: 1
[Linked Image from static.ewites.com]

no problemo.. wink

Joined: Dec 2003
Posts: 1,796
Pooh-Bah
Pooh-Bah
Joined: Dec 2003
Posts: 1,796
hehe, I answered the question you asked. Not being a developer myself, I go to the manual at php.net if I don't understand something. We have no idea what level a person is when they come to this forum to ask a question so I try to answer based on the perceived level of coding skills smile I am definitely not cynical and meant no put-down to anyone who might understand php, especially better than me smile


- Allen
- ThreadsDev | PraiseCafe
Joined: Mar 2009
Posts: 15
M
stranger
stranger
M Offline
Joined: Mar 2009
Posts: 15
No hard feelings.
Looks like I should have phrased my question different.
At the time it just felt like beeing put down because I had been looking for an answer to my "problem" for weeks. And if someone then tells me to "go look at php.net" and quotes something I have read dozens of times over the past weeks...
Well I think it's clear how I felt wink


Link Copied to Clipboard
ShoutChat
Comment Guidelines: Do post respectful and insightful comments. Don't flame, hate, spam.
Recent Topics
spam issues
by ECNet - 03/19/2024 11:45 PM
Looking for a forum
by azr - 03/15/2024 11:26 PM
Editing Links in Post
by Outdoorking - 03/15/2024 9:31 AM
Question on barkrowler and the like
by Mors - 02/29/2024 6:51 PM
Member Permissions Help
by domspeak - 02/27/2024 6:31 PM
Who's Online Now
1 members (Ruben), 476 guests, and 111 robots.
Key: Admin, Global Mod, Mod
Random Gallery Image
Latest Gallery Images
Los Angeles
Los Angeles
by isaac, August 6
3D Creations
3D Creations
by JAISP, December 30
Artistic structures
Artistic structures
by isaac, August 29
Stones
Stones
by isaac, August 19
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20230217)