0

I have a function which verify if all necessary parameters are set and then trim these params. Is it possible? How can/should I pass these parameters per reference?

$error = false;
$error = array();

$posts = array('first_name', 'last_name', 'email',
               'telephone', 'address', 'CAP', 'city', 'comments'
                //many others
                );

$post_error = dieOrTrim(&$posts, &$error);

echo '<pre>';
echo '0'.$_POST['first_name'].'0<br />';
var_dump($error);              
var_dump($_POST);
echo '</pre>';
die();

FUNCTION:

function dieOrTrim(&$param) {
    foreach($param as &$p){
        if(!isset($_POST[$p])){
            $error[] = '$_POST['.$p.']';
            $post_error = 'ERROR';
            return $post_error;
        }else{
            $_POST[$p] = trim($_POST[$p]);
        }
    }
}

Entering ' Name ' on first_name field returns:

0 Name 0

array(0) {
}

array(18) {
  ["first_name"]=>
  string(6) " Name "
  ["last_name"]=>
  string(0) ""
  ["email"]=>
  string(0) ""
  ["telephone"]=>
  string(0) ""
  ["address"]=>
  string(3) "+41"
  ["CAP"]=>
  string(3) "(0)"
  ["city"]=>
  string(0) ""
  ["comments"]=>
  string(0) ""
  ["etc"]=>
  string(0) ""
  ["etc"]=>
  string(1) "1"
  ["etc"]=>
  string(0) ""
  ["etc"]=>
  string(0) ""
  ["etc"]=>
  string(0) ""
  ["etc"]=>
  string(0) ""
  ["etc"]=>
  string(0) ""
  ["etc"]=>
  string(0) ""
  ["etc"]=>
  string(1) "1"
  ["etc"]=>
  string(1) "1"
}
Perocat
  • 1,481
  • 7
  • 25
  • 48
  • possible duplicate of [PHP need to trim all the $\_POST variables](http://stackoverflow.com/questions/4710440/php-need-to-trim-all-the-post-variables) – T.Todua Mar 17 '15 at 07:39

2 Answers2

4

Are there any values that should not be trimmed?

Else you could just trim all values using array_map, if $_POST is not empty:

if(!empty($_POST)) {
  $_POST = array_map("trim", $_POST);
}

Hope that helps, not so much code :)

Jonny 5
  • 12,171
  • 2
  • 25
  • 42
  • Thank you, this is the fastest and best solution. I'll only mantain my function to verify that all parameters are setted. – Perocat Jan 05 '14 at 04:43
1

The problem I see in your function is that you return immediately after you check !isset($_POST[$p]). I believe you want to continue on to the next item and operate on that accordingly.

function dieOrTrim($param) {
    $post_error = '';

    foreach($param as $p){
        if(!isset($_POST[$p])){
            $post_error = 'ERROR';
            continue;
        }

        // Trim $_POST[$p] if it's set
        $_POST[$p] = trim($_POST[$p]);
    }

    return $post_error;
}

Also, you don't need to pass these by reference since you are not modifying any of the passed parameters.

vee
  • 38,255
  • 7
  • 74
  • 78
  • Thank you! No, I'd like to stop at the first inoccurence of $_POST[$p], this is my intention. The problem is that the $_POST[$p] parameters are not trimmed when testing them after the function – Perocat Jan 05 '14 at 03:58
  • How does your input look like, i.e. `var_dump($_POST)`? – vee Jan 05 '14 at 04:06
  • I was interested in before just to make sure that it die not `ERROR`ed out early. But please post both before and after in your question. – vee Jan 05 '14 at 04:16
  • Posted, I didn't get error because after the function I post `$post_error` and `die()` if $post_error is not false anymore... I edited my code; – Perocat Jan 05 '14 at 04:20
  • In the `var_dump` you've posted, all inputs are already trimmed, how are you testing this? – vee Jan 05 '14 at 04:24