Previous Thread
Next Thread
Print Thread
Hop To
Strange (invalid??) PHP code #223551 03/04/2009 6:03 AM
Joined: Mar 2009
Posts: 15
Mattie Offline OP
stranger
OP Offline
stranger
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?

Re: Strange (invalid??) PHP code [Re: Mattie] #223662 03/06/2009 11:49 AM
Joined: Dec 2003
Posts: 1,796
AllenAyres Offline
Pooh-Bah
Offline
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
Re: Strange (invalid??) PHP code [Re: AllenAyres] #223670 03/06/2009 1:41 PM
Joined: Mar 2009
Posts: 15
Mattie Offline OP
stranger
OP Offline
stranger
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.

Re: Strange (invalid??) PHP code [Re: Mattie] #223699 03/06/2009 7:23 PM
Joined: Apr 2007
Posts: 3,938
SD Offline
Former Developer
Offline
Former Developer
Joined: Apr 2007
Posts: 3,938
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

Re: Strange (invalid??) PHP code [Re: SD] #223714 03/07/2009 4:26 AM
Joined: Mar 2009
Posts: 15
Mattie Offline OP
stranger
OP Offline
stranger
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.

Re: Strange (invalid??) PHP code [Re: Mattie] #223725 03/07/2009 12:00 PM
Joined: Apr 2007
Posts: 3,938
SD Offline
Former Developer
Offline
Former Developer
Joined: Apr 2007
Posts: 3,938
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..

Re: Strange (invalid??) PHP code [Re: SD] #223728 03/07/2009 12:36 PM
Joined: Mar 2009
Posts: 15
Mattie Offline OP
stranger
OP Offline
stranger
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.

Re: Strange (invalid??) PHP code [Re: Mattie] #223729 03/07/2009 12:51 PM
Joined: Apr 2007
Posts: 3,938
SD Offline
Former Developer
Offline
Former Developer
Joined: Apr 2007
Posts: 3,938
[Linked Image]

no problemo.. wink

Re: Strange (invalid??) PHP code [Re: SD] #223730 03/07/2009 2:12 PM
Joined: Dec 2003
Posts: 1,796
AllenAyres Offline
Pooh-Bah
Offline
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
Re: Strange (invalid??) PHP code [Re: AllenAyres] #223772 03/08/2009 4:01 AM
Joined: Mar 2009
Posts: 15
Mattie Offline OP
stranger
OP Offline
stranger
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


Forum Search
ShoutChat Box
Comment Guidelines: Do post respectful and insightful comments. Don't flame, hate, spam.
Recent Topics
Mobile app?
by Baldeagle - 12/06/2019 9:32 PM
How do you change Text Line spacing?
by jorb - 11/23/2019 12:14 AM
What happened to FAQ or Forum Help
by Ruben - 11/20/2019 11:58 AM
Search feature encountering an Error message
by jorb - 11/20/2019 12:06 AM
UBB Dev
by JAISP - 11/03/2019 11:01 AM
Who's Online Now
0 registered members (), 69 guests, and 401 spiders.
Key: Admin, Global Mod, Mod
Random Gallery Image
Latest Gallery Images
Artistic structures
Artistic structures
by isaac, August 29
Stones
Stones
by isaac, August 19
Amusing Terain Scenics
Amusing Terain Scenics
by isaac, August 19
Sky places
Sky places
by isaac, August 19
Powered by UBB.threads™ PHP Forum Software 7.7.4
(Snapshot build 20191023)