whityiu Posted June 20, 2012 Share Posted June 20, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/264468-need-help-with-syntax/ Share on other sites More sharing options...
Bradley99 Posted June 20, 2012 Share Posted June 20, 2012 I would imagine it's what's written inside of a Field that's inside of a Form. Quote Link to comment https://forums.phpfreaks.com/topic/264468-need-help-with-syntax/#findComment-1355316 Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264468-need-help-with-syntax/#findComment-1355318 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2012 Share Posted June 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264468-need-help-with-syntax/#findComment-1355320 Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264468-need-help-with-syntax/#findComment-1355326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.