0

What can be a better and easier way of writing this if...elseif...else code in PHP.

Suppose $student_name and $student_age are two variables.

$age1 = array();
$age2 = array();
$age3 = array();

//Similarly arrays for ages 4,5,6,7,8,9 and 10.


if($student_age == 1){
  $age1[] = $student_name;
}


elseif($student_age == 2){

  $age2[] = $student_name;
}



elseif($student_age == 3){

  $age3[] = $student_name;

}

//Elseif loop continues for ages 4,5,6,7,8,9  and 10.


else{
...
}

Arpit Jain
  • 1,599
  • 9
  • 23
Tejas
  • 27
  • 2
  • For your specific example you could use a `switch...case` construct, but frankly, your example is unrealistic. You're more likely to need to select on age ranges in which case a series of `if ($age<$someValue ) then {}` might serve you better. – Tangentially Perpendicular May 28 '23 at 03:52
  • Use a single array, not 10 arrays with different names. – knittl May 28 '23 at 06:27
  • Please don't title your question as what is other way to write if elseif in PHP. There are ways but all of them have nothing to do with your question – Your Common Sense May 28 '23 at 06:30
  • Also see [this question](https://stackoverflow.com/q/9257505/5527985) - You would use [`${"age".$student_age}[] = $student_name;`](https://tio.run/##K8go@P/fxj7AI4CLS6W4pDQlNa8kPjE9VcFWwdBaAS6Sl5gLElLyys/IU3DJT1WyBiqvVgIqVNJD1lYbHQtUhqINqLIssSg@pTS3QEMFqMRQ0/r/fwA) but as mentioned in the answers here, there are better ways to structure your data. – bobble bubble May 28 '23 at 11:05

3 Answers3

2

Use a fitting data-structure for your data, not just think in variables names.

One flexible in PHP is array and here you can map the names to the ages as you can create arrays of arrays (multidimensional arrays):

$age[$student_age][] = $student_name;

Here a map of student age to a list of the student names.

In general the best if/else is the one you don't have.

hakre
  • 193,403
  • 52
  • 435
  • 836
2

Instead of having 10 arrays, this probably works better as 1 2-dimensional array.

$ages = [];
// Then inside the loop
if (!isset($ages[$student_age])) {
  $ages[$student_age] = [];
}
$ages[$student_age][] = $student_name;
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Evert
  • 93,428
  • 18
  • 118
  • 189
  • 1
    +1, came to the [same/a very similar conclusion](https://stackoverflow.com/a/76350127/367456), with a little difference: the isset() check is not necessary in PHP when building such kind of arrays. – hakre May 28 '23 at 08:02
0

The simplest way to sort our the problem to define a multidimensional array for the student age. The array index can increased based on the ages and then assign student name according to the age

<?php
$ages = [2 => [], 3 => [], 4 => []];


$ages[$student_age] = $student_name;
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Sorry, but what value does this answer add? Your "solution" to use a multidimensional array has already been suggested 2 times. Since this is your first answer, you may read: https://stackoverflow.com/help/answering – Ferris May 28 '23 at 09:35