PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old July 22nd, 2004, 07:41 AM
bah26 bah26 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 18 bah26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
isset/empty and coding style....

I can't explain this, so I'll give an example:

PHP Code:
 $myVariable = (isset($_SESSION['myVar'])) ? $_SESSION['myVar'] : null;
 
//OR
 
$myVariable $_SESSION['myVar']; 


The later will generate an error notice, but the first requires more code.....

Which is a better style??

Cheers,

Ben

Last edited by bensmyth : July 22nd, 2004 at 07:50 AM. Reason: (messed up the php tags... (should really use <preview> ;-) )

Reply With Quote
  #2  
Old July 22nd, 2004, 03:16 PM
q3utom q3utom is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 16 q3utom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
didnt you just post on 34sp forums???

someone replied to it.

What are you exactly trying to do here ? Security features to stop ppl acessing certain pageS?

Reply With Quote
  #3  
Old July 26th, 2004, 07:43 AM
bah26 bah26 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 18 bah26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Its basically a question about coding style....

Should I check to see if a variable has been set before assigning it.
Or, is this unnecessary code?

Does your answer vary in different scenarios.....

eg.
PHP Code:
 $var $myVar;
$var = (isset($myVar)) ? $myVar null;
 
$var myFunc($myVar);
$var myFunc((isset($myVar)) ? $myVar null);
 
$var myFunc2(12$varX$myVar)
$var myFunc2(12$varX, (isset($myVar)) ? $myVar null);
 
$var myFunc3(12$varX$myVar$varY)
$var myFunc3(12$varX, (isset($myVar)) ? $myVar null$varY); 



Cheers,

Ben

Reply With Quote
  #4  
Old July 26th, 2004, 07:56 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 5
Generally speaking if you know what the variable will be then theres no need to test otherwise you should validate it in someway to make sure it contains what you expect.

In the case of function calls like the above it might be better to run a check on them first and only run the function if they contain what you expect, otherwise print out an error. Alternatively, if you have written the function, then validating what comes in within the function may be your best option.

As a good rule of thumb you want to be as liberal about what you accept and as strict about what you return as possible. That way poor use of the function is caught easily but once the function is run you know what you are dealing with.

Hope this is of some help,

-KM-

Reply With Quote
  #5  
Old July 26th, 2004, 04:36 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
Personally, I'd use the second example... but as kode_monkey says, use your judgement.

When referring to $_SESSION or $_POST or others, i usually just assign it [as per your second example].

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > isset/empty and coding style....


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway