Jump to content

Need help with syntax


whityiu

Recommended Posts

Hello,

 

I am reading a book and came across the following code:

 

foreach ( $form_fields as $field ) {
        if ( !isset( $_POST[$field] ) || trim( $_POST[$field] ) == "" ) {
            $completed_form = false;
            break;
        }else{
            ${$field} = $_POST[$field];
        }
}

 

I was wondering if someone could explain the line of code in the else-clause. I am not sure what the ${$field} syntax is. Any help is much appreciated.

 

Thanks!

Link to comment
Share on other sites

It's called variable variables.

http://php.net/manual/en/language.variables.variable.php

 

I generally avoid using them because you're automating changes to your function's or application's global data. I'd much rather use this method

 

$data = array();
foreach ( $form_fields as $field ) {
        if ( !isset( $_POST[$field] ) || trim( $_POST[$field] ) == "" ) {
            $completed_form = false;
            break;
        }else{
            $data[$field] = $_POST[$field];
        }
}

 

Any changes you make are limited to the $data variable, and cannot change other variables that may exist in the current scope.

 

Here's an example of a conflict

<?php

$foobar = 'hello world';

$form_fields = array('foobar');
$_POST['foobar'] = 'goodbye';


foreach ( $form_fields as $field ) {
        if ( !isset( $_POST[$field] ) || trim( $_POST[$field] ) == "" ) {
            $completed_form = false;
            break;
        }else{
            ${$field} = $_POST[$field];
        }
}

echo $foobar;
// Outputs 'goodbye'

?>

 

I know it's easy to spot the mistake here, but in large applications, behaviour like this can be an absolute BITCH to debug.

Link to comment
Share on other sites

The book has code in it but no explanation of what it does? That's odd.

 

There's an array of field names somewhere that go with that code. Using variable variables, the loop is checking to see if all the form fields have at least one non-whitespace character in them, and if so, assigning the value to a variable of the same name as the array key in the $_POST array. Probably not really something I'd use.

Link to comment
Share on other sites

The book has code in it but no explanation of what it does? That's odd.

 

There's an array of field names somewhere that go with that code. Using variable variables, the loop is checking to see if all the form fields have at least one non-whitespace character in them, and if so, assigning the value to a variable of the same name as the array key in the $_POST array. Probably not really something I'd use.

 

I think he's talking about the specific variable-variable with curly-brace syntax. The rest he claims to understand. I think I summed it up in my post, lemme know if I've missed anything.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.