vendredi 26 août 2016

PHP if loop is this the most effecient way?

Morning all,

I have an if statement here it works perfect for what I want it to do. It loops through some values e.g. sop_01 to sop_06 and writes their output separated with a
. I would like to know if this is the most efficient way of writing this code as to me it does not seem very efficient e.g. what happened if the values went from sop_01 to sop_1000 you would not write this out manually?

if (TRIM($row['sop_01']) <> null){
    $sop = TRIM($row['sop_01']);
    if (TRIM($row['sop_02']) <> ""){
        $sop = $sop . "<br>" . TRIM($row['sop_02']);
        if (TRIM($row['sop_03']) <> ""){
            $sop = $sop . "<br>" . TRIM($row['sop_03']);
            if (TRIM($row['sop_04']) <> ""){
                $sop = $sop . "<br>" . TRIM($row['sop_04']);
                if (TRIM($row['sop_05']) <> ""){
                    $sop = $sop . "<br>" . TRIM($row['sop_05']);
                    if (TRIM($row['sop_06']) <> ""){
                        $sop = $sop . "<br>" . TRIM($row['sop_06']);
                    }
                }
            }
        }
    }
} else { $sop = "hello world"; }

A little background info; if sop_01 is null then all the other values will be null

if sop_01 is <> null there is a possibility the other values are either “” or have a value

if sop_02 is empty it is not null it is "" (Due to how the data is stored in the database)

Please let me know if I can provide any further information

Bepster

Aucun commentaire:

Enregistrer un commentaire