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?
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?
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
.
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 />";
}