-2

I am doing this

foreach(['A', 'B', 'C', 'D'] as $x) {
    $x = $s->where('status', $x)->count() / $h * 100;
}

Now I'm trying to echo it like this:

echo $A; echo $B;

etc.

But it says $A is undefined, how do I do this?

danielmz02
  • 45
  • 1
  • 5

2 Answers2

1

Once this question lacks of explanation, I'm assuming that you want to assign a value for each letter of your array, as variable variables, as @nigel-ren has suggested:

foreach(['A', 'B', 'C', 'D'] as $x) {
    $$x = $s->where('status', $x)->count() / $h * 100;
}

For each iteration, $$x will create the variables $A, $B, $C, and $D, and assign the values returned by $s->where('status', $x)->count() / $h * 100;.

After that, you can echo the values of $A, $B, $C, and $D.

0

I'm reaching here, as it's not totally clear, but you are overwriting the value $x from the loop with the calculation. You can get the status and the calculation like so:

foreach(['A', 'B', 'C', 'D'] as $status) {
    $x = $s->where('status', $status)->count() / $h * 100;
    echo "The result for $status is $x <br />";
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87