some code from the old profiles.php

Started by Haase, August 21, 2003, 09:40:06 AM

Previous topic - Next topic

Haase

In PfaBB, we had an issue where when a user does not have a designated skin, and they go to edit their profile for the first time, the skin select box should default to $modSettings[default_skin].

In the Profile page, there is a block of code that handles default values:   // set up the default values
   $memsettings['realName'] = (isset($memsettings['realName']) ? $memsettings['realName'] : '');
   $memsettings['usertitle'] = (isset($memsettings['usertitle']) ? $memsettings['usertitle'] : '');
   $memsettings['location'] = (isset($memsettings['location']) ? $memsettings['location'] : '');
   $memsettings['websiteTitle'] = (isset($memsettings['websiteTitle']) ? $memsettings['websiteTitle'] : '');
   $memsettings['websiteUrl'] = (isset($memsettings['websiteUrl']) ? $memsettings['websiteUrl'] : '');
   $memsettings['ICQ'] = (isset($memsettings['ICQ']) ? $memsettings['ICQ'] : '');
   $memsettings['YIM'] = (isset($memsettings['YIM']) ? $memsettings['YIM'] : '');
   $memsettings['timeFormat'] = (isset($memsettings['timeFormat']) ? $memsettings['timeFormat'] : '');
   $memsettings['timeOffset'] = (isset($memsettings['timeOffset']) ? $memsettings['timeOffset'] : '0');
   $memsettings['lngfile'] = (strlen($memsettings['lngfile']) > 2 ? $memsettings['lngfile'] : $language);


I really haven't worked with this kind of construct before, but in copying the pattern, I came up with:   $memsettings['skin'] = (isset($memsettings['skin']) ? $memsettings['skin'] : $modSettings[default_skin]);
I thought that would work, but it didn't.

So instead, I went with what I know, which is now working fine:    if ($memsettings[skin] == '') { $memsettings[skin] = $modSettings[default_skin]; }

Could someone explain to me the original function, or point me to a tutorial or www.php.net page that describes how it works.  Ultimately, I'd like to know why I couldn't get it to work, and if these two different methods are functionally equivelant, is there a particular advantage to doing it one way versus the other.

Thanks,

-Haase
Find out about Enigma, the portal built exclusively for YaBB SE will be continuing it's work towards SMF

Spaceman-Spiff


Jack.R.Abbit™

$memsettings['skin'] = (isset($memsettings['skin']) ? $memsettings['skin'] : $modSettings[default_skin]);

the isset($memsettings['skin'] is your condition.  If that condition evaluates as "true" then you basically get the equivalent of $memsettings['skin'] = $memsettings['skin'].  If it evaulates to "false" you basically get $memsettings['skin'] = $modSettings[default_skin].  Nothing is technically wrong with that code you tried (however, you might try putting single quotes in the $modSettings[default_skin] part)... but just knowing it didn't work for you is not enough info.  What didn't work?  Did you get an error? Are you not getting the proper skin loaded?  Are they loading opposite of what you would expect?

As for why to use one method over the other... I don't know.  Soome find the method I explained easier to read... other find it harder.  I don't know which is faster or better.

-Jack

Haase

#3
Thanks Jack.

The issue that I was getting, was that no matter whether there was a value  in the table from $memsettings['skin'] or not, it was always returning true.  I verified that by changing the code !isset, in which case it always returned with the value of $modSettings['default_skin'].

So I guess I *did* understand the code structure.  Now I have to find out why isset() returned true with a null in the database...

[edit]
from php.net:
Quote$var = '';

// This will evaluate to &true; so the text will be printed.
if (isset($var)) {
    print "This var is set set so I will print.";
}
[/edit]

Find out about Enigma, the portal built exclusively for YaBB SE will be continuing it's work towards SMF

Jack.R.Abbit™

Based on the comparison chart at the link Spaceman-Spiff posted, I'd try just treating it as a Boolean compare.
$memsettings['skin'] = ($memsettings['skin'] ? $memsettings['skin'] : $modSettings[default_skin]);It will basically be true as long as the value of $memsettings['skin'] contains something value besides 0.  But this method in this case seems kind of strange.  Unless this is faster, I might just go with what you already got to work.

-Jack

Haase

In my case, replacing isset with !empty should also work.

I'm going to do more research on this whole compare structure.  It'll be nice to add some additional coding tricks up my sleave.
Find out about Enigma, the portal built exclusively for YaBB SE will be continuing it's work towards SMF

Jack.R.Abbit™

Quote from: Haase on August 21, 2003, 01:02:38 PMIn my case, replacing isset with !empty should also work.
Yes, the !empty() will return the exact same result as just the boolean... so why make that extra function call? ;)

[Unknown]

#7
I think the ternary form (that is, a ? b : c) is just as fast, but it's easier to use in, for example, array situations.  It also looks less jagged, and is better than multiple if's because it can be less processing.

Empty is a language construct, not a function.  I think it's much better to say empty($a) ? than $a ?.  Part of this is that a notice is generated if $a is not set with the latter.

SMF uses empty and isset a lot.  It also runs with error_reporting on full.

-[Unknown]

Advertisement: