|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
isset/empty and coding style....
I can't explain this, so I'll give an example:
PHP Code:
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> ;-) ) |
|
#2
|
|||
|
|||
|
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? |
|
#3
|
|||
|
|||
|
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:
Cheers, Ben |
|
#4
|
|||
|
|||
|
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- |
|
#5
|
||||
|
||||
|
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]. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > isset/empty and coding style.... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|