What is the purpose of using namespaces in PHP and how does it help in avoiding naming collisions?

When working on large projects or using third-party libraries in PHP, naming collisions can occur when different code elements have the same name. This can lead to conflicts and unexpected behavior in the application. Namespaces in PHP help in organizing and categorizing code elements by providing a way to group related classes, functions, and constants under a particular namespace. This helps in avoiding naming collisions by ensuring that each element is uniquely identified within its namespace.

<?php

namespace MyNamespace;

class MyClass {
    // class implementation
}

function myFunction() {
    // function implementation
}

const MY_CONSTANT = 1;

?>