Skip to content
Advertisement

How To Reference a Variable Inside The Same Array

$price = $product->price;
$priceData = [
    'subtotal' => $price,
    'tax' => $price * 0.13,
    'total' => $price + >>>> $tax <<<<
];

How can I get the value of 'tax' and use it inside 'total'?

Advertisement

Answer

Not the way you are trying to do it. You have about three options:

$priceData['subtotal'] = $price;
$priceData['tax'] = $price * 1.13;
$priceData['total'] = $price + $priceData['tax'];

Or:

$priceData = [
    'subtotal' => $price,
    'tax' => $price * 1.13
];
$priceData['total'] = $price + $priceData['tax'];

Or:

$tax = $price * 1.13;
$priceData = [
    'subtotal' => $price,
    'tax' => $tax,
    'total' => $price + $tax
];
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement