PHP Total of Var & Constant

JSEB

Newb
Apr 2, 2020
13
14
I am fairly new to PHP, I am trying to get the total for a variable and a constant.
My variable is a random integer, and I need the total of $shirts bought, to match the price point I have set as my constant 'SHIRT_PRICE'
I hope I explained this well, the code I have currently:

PHP:
<?php
    define ('SHIRT_PRICE', '17.99');
    $shirts = rand(1, 10);
    echo $shirts_total = ($shirts += 'SHIRT_PRICE');

The error I am getting: Fatal error: Uncaught TypeError: Unsupported operand types: int + string in C:\xampp\htdocs\clothing_sales.php:32 Stack trace: #0 {main} thrown in C:\xampp\htdocs\clothing_sales.php on line 32
 

Damien

Don't need glasses if you can C#
Feb 26, 2012
426
642
Code:
$shirts_total = ($shirts += 'SHIRT_PRICE')
Should be:
Code:
$shirts_total = ($shirts += SHIRT_PRICE)

By adding the single quotes you're not returning the value from the constant, you're creating a string with the same name.
 

Users who are viewing this thread

Top