Jump to content

Okay to use $$


ChenXiu
 Share

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
Share on other sites

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 😀

Link to comment
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
Share on other sites

Posted (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 by ChenXiu
Link to comment
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
Share on other sites

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 😀

Link to comment
Share on other sites

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.

 Share

×
×
  • 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.