1

I am making for myself a script that checks how many mH/hash of my miners on mining pool websites are. Therefor I have a bunch of variables with $user and $pass for each site. Example:

   $user1 = 'A';
   $pass1 = 'B'

   $user2 = 'C';
   $pass2 = 'D';

So I made a PHP curl that login the form and checks the currently mH/hash. Also I've made a counter, to rise the variable user1 and pass1 up by +1.

   $count = 0;
   $count = $count + 1;
   $user = "$user$count";   
   echo $user;

But the problem is my output at the login form:

  $user2

Should be:

  C

I hope it is clear what I am looking for and thanks in advance.

1
  • 2
    A) Read the manual for "variable variables", b) use arrays instead. Commented Dec 17, 2013 at 14:27

4 Answers 4

3

Use an array. Start with something like:

$users = array(
    array('A', 'B'),
    array('C', 'D')
);

foreach ($users as $user) {
    echo "Username: ".$user[0]. "\n";
    echo "Password: ".$user[1]. "\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks gonna change my code into an array, sounds to be better indeed.
3

Var concatenation is not very "recommended", but...

$user = ${'user'.$count}

You should consider using an array instead.

Comments

0

Use an Array:

$user = [];
$pass = [];

$user[1] = 'A';
$pass[1] = 'B'

$user[2] = 'C';
$pass[2] = 'D';

$count = 0;
$count = $count + 1;
echo $user[$count];

Comments

0

I think it would be a better approach to specify your user and password pairs in an array:

$login_info = array();
$login_info[] = array(
  'name' => 'A',
  'pass' => 'B',
);
$login_info[] = array(
  'name' => 'C',
  'pass' => 'D',
);

And you can travers the array by foreach:

foreach ($login_info as $info) {
  $name = $info['name'];
  $pass = $info['pass'];
  ...
}

You can easily add new elements to the array without creating new variables.

Comments

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.