When considering whether to rewrite an old PHP script, what factors should be taken into account to determine if it is more efficient to start from scratch or refactor the existing code?

When considering whether to rewrite an old PHP script, factors such as the complexity of the code, the extent of required changes, the availability of resources, and the potential impact on other systems should be taken into account. If the existing codebase is heavily outdated, poorly structured, or not meeting current requirements, it may be more efficient to start from scratch. However, if the codebase is relatively sound and only requires minor improvements, refactoring the existing code may be a more cost-effective option.

// Example PHP code snippet for refactoring an old script
function calculateTotal($items) {
  $total = 0;
  
  foreach ($items as $item) {
    $total += $item['price'] * $item['quantity'];
  }
  
  return $total;
}