1

I'm new to PHP and trying to learn the proper way to do things, I'm currently working on a PHP / HTML form so here is my code for my globa variable:

// General Function
global $variables;
$variables = array(
    'login' => array(
        'visible' => 'visible',
        'required' => 'required',
        'position' => get_option('order_username'),
        'value' => $_POST['username'],
        'error_req' => __('A username is required to register.'),
        'error_misspelled' => __('Your username is not properly formatted.'),
        'error_unavailable' => __('This username has already been registered.')
),

    'province' => array(
        'visible' => get_option('show_province'),
        'required' => get_option('required_province'),
        'position' => get_option('order_province'),
        'value' => $_POST['easyreg_province'],
        'error_req' => __('You need to fill your province to register.'),
        'error_misspelled' => '',
        'error_unavailable' => ''
    )

);

This works fine and I can access those Variables from every other functions; first I know many people says not to use GLOBAL variables, I don't really understand why because in my case those data will be used and reused inside my form, I would prefer not to redeclare new variables in every function related because those data won't change during the processing.

If it's really not the best way... What could be a good way to do that? As you can see I have about 7 "values" by form "input".

Also, for a GLOBAL variable like that can you "declare" the values from inside a function? What I mean is that if I add a new fields to my form, I would like to have those declaration beside my field, instead of having it in the main PHP (which would also make my code look cleaner).

I would have thought something like that, but it seems to be only declaring it inside the function itself:

function form_display(){
<!-- Email field -->
$GLOBALS['variables']['email'] = array(
    'visible' => 'visible',
    'required' => 'required',
    'position' => get_option('order_email'),
    'value' => $_POST['email'],
    'error_req' => __('An email is required to register.'),
    'error_misspelled' => __('This is not an appropriate email format.'),
    'error_unavailable' => __('This email has already been registered.')
);
?>
<div class="regpage_email required"
     id="order_<?php echo($GLOBALS['variables']['email']['position']) ?>">
    <td id="field_email"><label
        for="email"><?php _e('E-mail Address: *') ?></label><br><input
            id="email"
                type="email"
                name="email"
                value=""
                placeholder=""
           required>
    </td>
}
1
  • Is function form_display(){ really outside <?php ?>?
    – bcesars
    Feb 13, 2015 at 11:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.