0

I'm newbie to PHP and I have a simple question.

UPDATE: I am using PHP 5.6 (Best solution would be to update the PHP version but let's suppose that I can only use PHP 5.6)

I have a code like below:

function findOrCreateMasterRecord ($masterTableName, $masterName) {

    if (isset($sampleArr[$masterTableName][$masterName])) {
        return $sampleArr[$masterTableName][$masterName];
    }

    return getNewMasterIndex($masterTableName, $masterName);

}

This code works properly. But I want to make "if" block more simple because it approaches twice to same index($sampleArr[$masterTableName][$masterName]) and I think this is...somewhat..not good.

Is there a way to make this function to be more effective?

Thank you.

passion053
  • 367
  • 4
  • 18
  • If you're using PHP 7+ then you can make use of the [null coalescing operator (`??`)](https://stackoverflow.com/questions/34571330/php-ternary-operator-vs-null-coalescing-operator) – iainn Aug 06 '18 at 10:07
  • 1
    I would leave it as is, and concentrate on stuff that has more impact, to be honest. Sure, in php7 you could make it it somewhat more readable using `??`, but that doesn't really do anything much different. It does need you (see answer below) maybe to shorten your variables to something unreadable to keep it in one line. That's a bigger no-no then your current check in my book – Nanne Aug 06 '18 at 10:29
  • I'm voting to close this question as off-topic because this question is working and should therefore be posted on code review. – Andreas Aug 06 '18 at 20:41
  • I would totally vote in favor of what @Nanne has mentioned. In terms of learning the language, this is an interesting question to ask. I wouldn't think there are any real world benefits to shortening the code for the sake for shortening it (reducing method complexity is good but it doesn't always correlate with line numbers) – meewog Aug 24 '20 at 16:53
  • There is a bug in this code, making condition pointless. The variable $sampleArr is never set. – PeterM Aug 24 '20 at 16:59

2 Answers2

3

In PHP 7+ you could use the null coalescing operator: ??

function findOrCreateMasterRecord ($masterTableName, $masterName)
{
    return $sampleArr[$masterTableName][$masterName] ?? getNewMasterIndex($masterTableName, $masterName);
}

If not in PHP 7, a ternary operator could shorten your code but will still be redundant:

function findOrCreateMasterRecord ($masterTableName, $masterName)
{
    return isset($sampleArr[$masterTableName][$masterName]) ? $sampleArr[$masterTableName][$masterName] : getNewMasterIndex($masterTableName, $masterName);
}

With shorter variable names for a better read:

// PHP 7
function findOrCreateMasterRecord ($table, $name)
{
    return $arr[$table][$name] ?? getNewMasterIndex($table, $name);
}

// Under PHP 7
function findOrCreateMasterRecord ($table, $name)
{
    return isset($arr[$table][$name]) ? $arr[$table][$name] : getNewMasterIndex($table, $name);
}
AymDev
  • 6,626
  • 4
  • 29
  • 52
0

You can reduce to the following, as your condition will never be met:

<?php
function findOrCreateMasterRecord ($masterTableName, $masterName) {
    return getNewMasterIndex($masterTableName, $masterName);
}
Progrock
  • 7,373
  • 1
  • 19
  • 25