-1

Please how can i achieve something like this:

A-FIRSTNAME
B-LASTNAME
C-MIDDLENAME
D-PHONE

her is my code

<php 

   $alphabet = range('A','D');
   for ($i = 1; $i<4; $i++){
      $i2 = $i;
      echo $alphabet[$i2]."-FIRSTNAME";
      echo $alphabet[$i2]."-LASTNAME";
      echo $alphabet[$i2]."-MIDDLENAME";
      echo $alphabet[$i2]."-PHONE";
      $i2++;
  }

?>

Michael Codes
  • 135
  • 1
  • 10

3 Answers3

0

You can either do:

$arr = array('FIRSTNAME','LASTNAME','MIDDLENAME','PHONE');

$i = 0;
foreach (range('A', 'D') as $alphabet) {
    echo $alphabet .'-'. $arr[$i] . '\n';
    $i++;
}
Özgür Can Karagöz
  • 1,039
  • 1
  • 13
  • 32
0

PHP > 7 :

<?php
$phrases = [
    'FIRSTNAME', 
    'LASTNAME', 
    'MIDDLENAME',
    'PHONE'
];
$chars  = range('A','Z');
foreach ($phrases as $index => $phrase) {
    echo ($chars[$index] ?? '?') . ' => ' . $phrase;
}

PHP < 7 :

<?php
$phrases = [
    'FIRSTNAME', 
    'LASTNAME', 
    'MIDDLENAME',
    'PHONE'
];
$chars  = range('A','Z');
foreach ($phrases as $index => $phrase) {
    echo (isset($chars[$index]) ? $chars[$index] : '?') . ' => ' . $phrase;
}
azjezz
  • 3,827
  • 1
  • 14
  • 35
0
<?php 

   $alphabet = range('A','D');


   for ($i = 0; $i<4; $i++){

      echo nl2br($alphabet[$i]."-FIRSTNAME\n");
      echo nl2br($alphabet[$i]."-LASTNAME\n");
      echo nl2br($alphabet[$i]."-MIDDLENAME\n");
      echo nl2br($alphabet[$i]."-PHONE\n");
      echo nl2br("\n");
      echo nl2br("\n");
  }

?>
calmchess
  • 118
  • 8