Process POSTed data and add default string if missing My code takes the input submitted by $ _POST, via a checkbox array. I process the array and check the sent data if there is a particular string in it, which is set as my default. If I have a string, which is my default, I just go through implode and add &quot;,&quot;, if my default string is missing in the data. I add and sort the array anew, with the first string being the default. I want to shorten the code because I can't make it shorter.
&lt;?php
if(isset($_POST['submit'])) {
    //for update
    $site_id['locale'] = 'tr';
    
    //new
    $exampleArray = isset($_POST['lala']) ? $_POST['lala'] : '';
    
    $example = null;
    if(($key = array_search($site_id['locale'], $exampleArray, true)) !== false) {
        unset($exampleArray[$key]);
        
        $FirstString = array($site_id['locale']);
        $exampleArray = array_diff($exampleArray, $FirstString);
        usort($exampleArray);
        $exampleArray = array_merge($FirstString, $exampleArray); 
        //print_r($exampleArray);
        $myArray = array();
        foreach ( $exampleArray as $key =&gt; $value ) {
            $myArray[] = $value;
        }
        echo implode( ', ', $myArray );
    } else {
        $FirstString = array($site_id['locale']);
        $exampleArray = array_diff($exampleArray, $FirstString);
        usort($exampleArray);
        $exampleArray = array_merge($FirstString, $exampleArray); 
        //print_r($exampleArray);
        $myArray = array();
        foreach ( $exampleArray as $key =&gt; $value ) {
            $myArray[] = $value;
        }
        echo implode( ', ', $myArray );
    }
    
}
?&gt;
&lt;form method=&quot;POST&quot; action=&quot;&quot;&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;lala[]&quot; value=&quot;bg&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;lala[]&quot; value=&quot;us&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;lala[]&quot; value=&quot;gr&quot; /&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; /&gt;
&lt;/form&gt;
Addressing two issues here:
Setting a default value
PHP now has a null coalesce operator (??).
This allows you to easily set a default value if the variable is null.
$exampleArray = $_POST['lala'] ?? [];

(Here, I am setting the default value to an empty array)
Refactoring code
Any time you catch yourself copy/pasting a chunk of code, that’s a signal to start refactoring.  So the code you provided had almost everything in the if and else block repeated.  Just pull the repeated stuff out. In this case, you don’t even need the else.

if(isset($_POST['submit'])) {
    //for update
    $site_id['locale'] = 'tr';
    
    //new
    $exampleArray = $_POST['lala']) ?? [];
    
    $example = null;
    if(($key = array_search($site_id['locale'], $exampleArray, true)) !== false) {
        unset($exampleArray[$key]);
    }
        
    $FirstString = array($site_id['locale']);
    $exampleArray = array_diff($exampleArray, $FirstString);
    usort($exampleArray);
    $exampleArray = array_merge($FirstString, $exampleArray); 
    //print_r($exampleArray);
    $myArray = array();
    foreach ( $exampleArray as $key =&gt; $value ) {
        $myArray[] = $value;
    }
    echo implode( ', ', $myArray );

    // Note, should always redirect after a post. 
    // Post means change data, so after you have 
    // stored the changes, redirect to the next (or 
    // back to same) page. Cannot be any output 
    // before this (print, echo, blank lines, html)
    
}
```