Avatar ·

How to convert a string to a number in PHP? All methods and pitfalls

Hello everyone! I'm a beginner PHP developer and I've encountered a problem with converting strings to numbers. I have a form on my website where users enter a product price. I get the value from $_POST, and it comes as a string, for example "1500" or "1999.99". I need to perform mathematical operations: multiply by quantity, add VAT, compare with the free shipping threshold. I tried simply adding strings, but I got concatenation instead of sum. For example: ```php $price = $_POST['price']; // "1500" $quantity = "3"; $total = $price + $quantity; // I expect 4500, but something went wrong? ``` Actually, PHP sometimes converts types automatically, but I'm not sure when it works correctly and when it might break. I've heard about the functions intval(), floatval() and about (int) and (float). But which method