Jump to content

Recommended Posts

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 by ChenXiu
Link to comment
https://forums.phpfreaks.com/topic/313423-okay-to-use/
Share on other sites

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);    

 

Link to comment
https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588556
Share on other sites

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 by ChenXiu
Link to comment
https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588557
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/313423-okay-to-use/#findComment-1588562
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.