0

I have a list of variables which I am using for some PHP functions. I know I can simplify this code but I am not really sure how. I have tried for(){} loops but haven't figured out how to make the result usable.

HERE ARE MY VARIABLES

    $saveData_1  = post_data($url_1);
    $saveData_2  = post_data($url_2);
    $saveData_3  = post_data($url_3);
    $saveData_4  = post_data($url_4);
    $saveData_5  = post_data($url_5);
    $saveData_6  = post_data($url_6);
    $saveData_7  = post_data($url_7);
    $saveData_8  = post_data($url_8);
    $saveData_9  = post_data($url_9);
    $saveData_10 = post_data($url_10);
    $saveData_11 = post_data($url_11);
    $saveData_12 = post_data($url_12);
    $saveData_13 = post_data($url_13);
    $saveData_14 = post_data($url_14);
    $saveData_15 = post_data($url_15);
    $saveData_16 = post_data($url_16);
    $saveData_17 = post_data($url_17);
    $saveData_18 = post_data($url_18);
    $saveData_19 = post_data($url_19);
    $saveData_20 = post_data($url_20);

HERE IS WHAT I HAVE TRIED

for($i = 0; $i<20; $i++) {
        $saveData = '$saveData_'.$i;
        $postData = 'post_data($url_'.$i.')';   
        print($saveData. '=' .$postData. ';');
    }

This works for the most part, it prints out my list of variables. But what I don't know, is how to make this usable so instead of printing them to the page being displayed in the browser they will be instead be "printed" to my php script.

Basically my goal is to write the Variables of above without having to actually write a new variable each time.

4
  • 4
    Please review PHP arrays and their accompanying standard functions. Commented Jan 7, 2013 at 19:31
  • why you aren't using php arrays? do you have a problem with them?? Commented Jan 7, 2013 at 19:33
  • I am not using them because I am learning PHP and I am not aware of them or certain how to use them. Commented Jan 7, 2013 at 19:35
  • Review the entire contents of my first link from top to bottom, then skim over the contents of the second. Then you'll know about PHP arrays and be certain about how to use them. Commented Jan 7, 2013 at 19:40

2 Answers 2

3

If I understand what you're asking correctly, you want to have actual "variables" that you can use, such as $saveData_16, but don't want to type each one out?

If that's the case, you can what's called a variable-variable:

$varName = 'saveData_16';
$postName = 'url_16';
$$varName = post_data($$postName);

Note the double $$ prefixing the variable's name.

This can be dropped into a loop with:

for ($i = 0; $i < 20; $i++) {
    $varName = 'saveData_' . $i;
    $postName = 'url_' . $i;
    $$varName = post_data($$postName);
}

Variable-variables can be tricky to remember and a nuisance to keep up with, especially if they contain similar names/values as existing variables. If they do, they can easily overwrite an important variable in your code. If this is a potential issue, you should probably avoid them.

An alternative to variable-variables, but on the same topic, if your values really do come from $_POST (based on the function-name post_data()), you can look into PHP's extract() function which performs the same "extraction" for you:

extract($_POST);

I would recommend against using extract() unless you're very clear on what's needed though. It can easily overwrite other variables that use the same name as a posted field. It does provide an optional second-parameter named $extract_type that is a flag specifying the behavior of the extract and, through this, you can tell it to not overwrite existing variables:

extract($_POST, EXTR_SKIP);

Again though, this can cause headaches by simply forgetting the optional flag - so I would avoid this function if this is a risk.

As an additional alternative, you could store the results into an array and use the array as-is. I personally support this method and, if you use strings as your array indexes, it can be just-as-readable as using a variable (and more-readable than a variable-variable).

If you're currently using $_POST, well, you should be all set - it's already in array form. However, if you're obtaining your data in a different method you can always build your own array with:

// build your array
$saveData = array();
for ($i = 0; $i < 20; $i++) {
    $urlName = 'url_' . $i;
    $saveData['url_' . $i] = post_data($$urlName);
}

// access a value
echo $saveData['url_4'];

I'm not 100% sure what your $url_# variables are, so I've kept that as a variable-variable in the array example; if it's just a string, you could replace it with the string instead.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick response. Give me a moment to test this out. Thanks.
using extract() on $_POST is risky
@lupatus Risky indeed; I recommend to use it (if at all) with the appropriate extract_type flags as defined in the documentation that will not overwrite existing variables.
Spiritual -1, but I'm out of votes today: We should not be introducing users new to PHP to variable variables, nor to extracting the GPC superglobals. These are both horrible practices and should be discouraged, not taught.
@Charles I agree with the repercussions of variable-variables and extract(); however, they were the most-direct answer to the OP's specific question. I also included a method of building an array as an alternative in my original answer too though to "cover all bases". However, because you're very right - I've updated my answer to put further emphasis on "why" variable-variables and extract should be avoided and to further support the array-method.
2

You need to use arrays. I will type up an example and add it to this comment. But what you want to use is an array variable. One array can hold all this and you can iterate (loop) through it easily.

Here ya go:

<?php
/* what the heck?  You need arrays ;-)
$saveData_1  = post_data($url_1);
$saveData_2  = post_data($url_2);
$saveData_3  = post_data($url_3);
$saveData_4  = post_data($url_4);
$saveData_5  = post_data($url_5);
$saveData_6  = post_data($url_6);
$saveData_7  = post_data($url_7);
$saveData_8  = post_data($url_8);
$saveData_9  = post_data($url_9);
$saveData_10 = post_data($url_10);
$saveData_11 = post_data($url_11);
$saveData_12 = post_data($url_12);
$saveData_13 = post_data($url_13);
$saveData_14 = post_data($url_14);
$saveData_15 = post_data($url_15);
$saveData_16 = post_data($url_16);
$saveData_17 = post_data($url_17);
$saveData_18 = post_data($url_18);
$saveData_19 = post_data($url_19);
$saveData_20 = post_data($url_20);
*/



$saveData = array();  // just a formality. You don't need to delcare variable types in PHP.
for ($i = 1; $i < 21; $i++) {
    $var = 'url_'.$i;
    $saveData[$i] = post_data($$var);
}

// now all your stuff is in $saveData
// what's in savedata 20?  this is:
echo $saveData[20];

// you could skip all of this and just use the $_POST superglobal
$_POST[$url_20]

// I'm curious to see what you are doing, because this process should be re-thought.

1 Comment

This worked best with what I was trying to do. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.