Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: PHP Code efficiency/Cleaness  (Read 122 times)


  • Hero Member
  • *****
  • Posts: 704
  • Karma: +8/-0
    • GrumpyYoungMan
PHP Code efficiency/Cleaness
« on: 16 Dec 2022, 10:58:28 am »
Code: [Select]
if(!empty(REQUEST[1]) && array_key_exists(REQUEST[1], MODULES) !== false)
    require_once MODULES[REQUEST[1]]['path'] . '/' . MODULES[REQUEST[1]]['file'] . '.php';

more efficient than:
Code: [Select]
if(!empty(REQUEST[1])) {
    if (array_key_exists(REQUEST[1], MODULES)) {
        require_once MODULES[REQUEST[1]]['path'] . '/' . MODULES[REQUEST[1]]['file'] . '.php';
    else {
else {
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 919
  • Karma: +171/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: PHP Code efficiency/Cleaness
« Reply #1 on: 17 Dec 2022, 01:11:56 pm »
Rule #1, don't create branches you don't need.

Rule #2, don't create nesting blocks (what JS re-re's called "closures" that have nothing to do with closing a damned thing) you don't need.

Rule #3, formatting is your friend.

Rule #4, when a function returns boolean true or false, you don't need to !== false on it.

Code: [Select]
if (
!empty(REQUEST[1]) &&
array_key_exists(REQUEST[1], MODULES)
) require_once(
MODULES[REQUEST[1]]['path'] . '/' . MODULES[REQUEST[1]]['file'] . '.php'
); else mainIndex($db);

Again, array_key_exists literally only returns boolean, so I've no clue why you had "!== false" on it.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.


SMF spam blocked by CleanTalk