ChenXiu Posted July 24, 2021 Share Posted July 24, 2021 (edited) I have a long list of named input fields like this: <input name="address"... , <input name="email"... , etc. To minify my PHP code, is there anything syntactically or logically wrong with processing their values like this: $fields = array( 'address' , 'email' , 'phone' , 'city' ); foreach ($fields as $val) { $$val = strtoupper($val); // (or whatever processing I need to do) } //yields $address = processedAddress, $email = processedEmail, $phone = processedPhone, etc. By putting all the word-names into an array, and using " $$ " I only need 4 lines of PHP code to name and process my variables how I want them. This seems to work, but there is a "baaaadddd don't go there " feeling about doing this (maybe PTSD flashbacks from the Register Globals Era), as well as vague recollections of seeing code that uses ${$variable} instead of $$variable. Thank you. Edited July 24, 2021 by ChenXiu Quote Link to comment https://forums.phpfreaks.com/topic/313423-okay-to-use/ Share on other sites More sharing options...
ChenXiu Posted July 24, 2021 Author Share Posted July 24, 2021 oops, I did a really terrible typo in my question which changes the whole meaning and desired result and makes my question more stupid than my questions usually are. The 3rd line of code should be $$val = strtoupper($$val); Thanks 😀 Quote Link to comment https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588555 Share on other sites More sharing options...
Barand Posted July 24, 2021 Share Posted July 24, 2021 I'd go for an array instead of separate variables created using $$. $fields = array( 'address' , 'email' , 'phone' , 'city' ); foreach ($fields as $name) { $data[$name] = "whatever"; } then you can subsequently $stmt = $pdo->prepare("INSERT INTO user (address, city, email, phone) VALUES (:address, :city, :email, :phone) "); $stmt->execute($data); Quote Link to comment https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588556 Share on other sites More sharing options...
ChenXiu Posted July 24, 2021 Author Share Posted July 24, 2021 (edited) Thank you. But is it still okay to use the " $$ " style? The array loop with the "$$" thing wasn't for mySQL input (the variables are variables used later on in my code). I was just trying to reduce my 5000 lines of code to 4995 lines 😀 That being said, your answer is really amazing -- I didn't realize that feature of PDO. -- I've always wondered why the heck people sometimes put colons before the value names. Now I know. p.s. I'm starting to think you could take my 5000 lines of code, and reduce it down to just 3 lines of code 😀 Edited July 24, 2021 by ChenXiu Quote Link to comment https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588557 Share on other sites More sharing options...
mac_gyver Posted July 24, 2021 Share Posted July 24, 2021 arrays are for sets of data, where you will operate on each member in the set in the same/similar way. by keeping the input data as a array, you can search through your code and find all references to that data, via the array variable name. using variable-variables, you are 'magically' creating those discrete variables and you must now know what they are named in order to search for them. by keeping the data as an array, you can directly operate on the data using php array functions. for your strtoupper example, where you want to apply that function to each entry in an array - $data = array_map('strtoupper',$data); you can then start doing things like trimming all the data, filtering out empty values, applying user written sort, filter, and validation functions to the data, all at once. also, as Barand has shown, you can build and use an array as a dynamic input to things like a PDO prepared query ->execute(...) call, or a templating system, so that you are not writing out lists of variables yourself. you can use general-purpose code to build those arrays of values based on entries you add to your defining $fields array. Quote Link to comment https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588562 Share on other sites More sharing options...
requinix Posted July 24, 2021 Share Posted July 24, 2021 5 hours ago, ChenXiu said: Thank you. But is it still okay to use the " $$ " style? Given that there are better options for you to use, no, it is not okay. Quote Link to comment https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588568 Share on other sites More sharing options...
ChenXiu Posted July 24, 2021 Author Share Posted July 24, 2021 Thank you all! I've learned a lot about arrays today (I'm going to learn what this "array_map" thing is that mac_gyver just suggested -- it seems too good to be true). I'm now thinking that my 5000 lines of code really could be reduced to 3 lines 😀 Quote Link to comment https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588571 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.