Transaction management
📖 Scenario: You are building a simple PHP script to manage a bank account transaction. You want to make sure that money is only withdrawn if there is enough balance, and if something goes wrong, the transaction should be canceled.
🎯 Goal: Create a PHP script that uses transaction management to safely withdraw money from a bank account balance stored in a variable.
📋 What You'll Learn
Create a variable
$balance with the initial amount of 1000Create a variable
$withdraw with the amount to withdraw, 300Use transaction management with
try and catch blocksCheck if
$balance is enough for the withdrawalIf enough, subtract
$withdraw from $balanceIf not enough, throw an exception with message 'Insufficient funds'
Print the final balance or the error message
💡 Why This Matters
🌍 Real World
Transaction management is important in banking and shopping websites to keep data safe and consistent.
💼 Career
Understanding how to use transactions and error handling is a key skill for backend developers working with databases.
Progress0 / 4 steps