• 0

Quick PHP Question


Question

Hi,

I know the code below is wrong but you get the idea of what im trying to do, the question is: how do i do it?

<?

$total = 6;
$encr1 = hello;
$encr2 = howdo;
$encr3 = hi;
$encr4 = shamone;
$encr5 = hey;
$encr6 = lol;

for ($i = 1; $i <= $total; $i++)
	{
  echo $encr$i;
	}

?>

If I do this:

<?

$total = 6;
$encr1 = hello;
$encr2 = howdo;
$encr3 = hi;
$encr4 = shamone;
$encr5 = hey;
$encr6 = lol;

for ($i = 1; $i <= $total; $i++)
	{
  echo '$encr' .$i. '<br>';
	}

?>

then it just outputs:

  Quote
$encr1

$encr2

$encr3

$encr4

$encr5

$encr6

Thanks,

Danny

Edited by dannybeckett.co.uk
Link to comment
https://www.neowin.net/forum/topic/336949-quick-php-question/
Share on other sites

6 answers to this question

Recommended Posts

  • 0
  dannybeckett.co.uk said:
Hi,

I know the code below is wrong but you get the idea of what im trying to do, the question is: how do i do it?

586122921[/snapback]

Dynamic vars:

<?

$total = 6;
$encr1 = "hello";
$encr2 = "howdo";
$encr3 = "hi";
$encr4 = "shamone";
$encr5 = "hey";
$encr6 = "lol";

for( $i = 1; $i <= $total; $i++ )
   echo ${ 'encr'.$i };
?>

  • 0

It might be better to consider using an array in these sort of situations. For example:

<?php
$encr[] = "hello";
$encr[] = "howdo";
$encr[] = "hi";
$encr[] = "shamone";
$encr[] = "hey";
$encr[] = "lol";

foreach ($encr as $i)
{
 echo($i);
}
?>

$encr[] = 'some value' will assign that value to the end of array $encr with each assignment.

  • 0

Ok. What is wrong with this code?

<?

session_start();

$user1 = '----';
$pass1 = '-----';
$encr1 = 'ae4c32e1985c8c096c703e3d0721b77a';

$user2 = 'gggg';
$pass2 = 'gggg';
$encr2 = 'f5f1393737c857d968418eeefbf64c88';

$user3 = 'jjjj';
$pass3 = 'jjj';
$encr3 = 'd637facbb013e6bdb8d3a32f1324294b';

$user4 = 'qwerty';
$pass4 = 'zzzz';
$encr4 = '7098c1135f987cd83b17e5acbcaecec3';

$user5 = 'yyyy';
$pass5 = 'zzz';
$encr5 = 'd5ac70e307c46da82a25a8b9c22601fc';

$user6 = 'ghj';
$pass6 = 'ghjjkk';
$encr6 = '8dfc836a65981a6f9e682c0a20c0d458';

$total = 6;

for ($i = 1; $i <= $total; $i++)
	{
  if (session_is_registered("${ 'pass'.$i }"))
  {
  	echo(${ 'user'.$i });
  }
	}

?>

nv/m, i fixed what i needed. :p

Edited by dannybeckett.co.uk
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.