1

I am trying to write a PHP function to write error messages to an array. Not sure what I'm doing wrong, still trying to get to grips with functions.

I can make it work without functions, so I guess its the way I'm writing the function that is wrong.

function writeerrors($arr_key, $arr_val){

    $errors[$arr_key] = $arr_val;

    return;

}

Then I call it here when I check if the form field is empty. If it is empty I want it to write to the $errors array.

 //check if empty
                if(empty($fname)){

                    //write to error array
                  writeerrors('fname', 'Empty field - error');

                    //Flag
                    $errors_detected = true;

                }else {

Do something else ..}

This is the form... (ONLY TRYING TO VALIDATE FIRST NAME FIELD FOR NOW): http://titan.dcs.bbk.ac.uk/~mgreen21/p1_prac/PHP_BBK/P1/hoe9/index.php

  • You're not returning the array, or doing something else with it, and you're not making the array available globally either, so the array is only within the function, not sure what you are expecting? – Epodax Feb 17 '16 at 13:28
  • 2
    Where you have yours `$errors` array? It's possible that your function `writeerrors` doesn't see it... – tomas.lang Feb 17 '16 at 13:29
  • 1
    What exactly you are trying to achieve? – Ashwani Agarwal Feb 17 '16 at 13:29
  • Also why an unnecessary function call each time when error occur, directly define an array and just assign key value inside the if else loop. – Alive to die - Anant Feb 17 '16 at 13:30
  • 1
    @tomas.lang Since it's not defined in the function, the function definitely can't see it – Machavity Feb 17 '16 at 13:30
  • Like I said, I can do it without functions. But I am trying to practice more efficient coding using functions instead of repeating code all the time. What I want is to validate the fname field in a form and if it is empty then write an error message to the $errors array and later in the code I display the error messages. This is the form (ONLY VALIDATING FIRST NAME) -http://titan.dcs.bbk.ac.uk/~mgreen21/p1_prac/PHP_BBK/P1/hoe9/index.php – Maurice Greenland Feb 17 '16 at 13:32
  • @Machavity you are right, i forget an scope... – tomas.lang Feb 17 '16 at 13:33
  • 2
    @Maurice Greenland instead of one line (`$errors[...] = ...`) you will write one line (`writeerrors(..., ...)`) - it same repeating and as @James answered, you will need also global variable, and that is ugly... – tomas.lang Feb 17 '16 at 13:36
  • 1
    Welcome to functions. You must now learn about scope. See the duplicate. Then go one step further and learn about objects and classes which solve some of the issues programming with scope introduces better than you could with functions alone. – deceze Feb 17 '16 at 13:37

1 Answers1

1

You just need to specify the global $errors variable, which you have created outside of your function.

function writeerrors($arr_key, $arr_val){
    global $errors;

    $errors[$arr_key] = $arr_val;

    return;
}
James
  • 3,765
  • 4
  • 48
  • 79
  • This is *probably* __a__ solution, but probably a bad one. You should not get overly attached to `global`. – deceze Feb 17 '16 at 13:35
  • 1
    @deceze Maybe, but in the OP's case, this is the issue. They've already stated that they have access to it outside of the function in the `if/else` statement, but just not inside of the function. Of course, there are better ways to go about it, and error handling in general, but they are just practicing different ways of handling things and wanted an answer only to this question. – James Feb 17 '16 at 13:37
  • 2
    Since OP obviously doesn't know about "scope", just handing them `global` as the only solution is setting them on a bad path. At least *some* explanation and cautioning is in order. – deceze Feb 17 '16 at 13:39
  • 1
    Thank you, So the problem is the scope. I have tested by making $errors global and it works. I will continue to loko for better solutions not relying on global – Maurice Greenland Feb 17 '16 at 13:39
  • @deceze In the comments above, they have already been given those though. I didn't feel a need to repeat is all. – James Feb 17 '16 at 13:40