Composer is a dependency management tool for PHP that allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Here’s a step-by-step guide on how to use Composer with PHP:

1. Install Composer:
– Download the installer from the official website: https://getcomposer.org/
– Run the installer script in your terminal: `php composer-setup.php`
– Move the `composer.phar` file to a directory listed in your system’s PATH to make Composer globally accessible.

2. Create a new PHP project:
– Create a new directory for your project: `mkdir my-project`
– Navigate to the project directory: `cd my-project`
– Initialize a new composer.json file: `composer init`
– It will ask you some questions about your project (name, description, author, etc.). You can also skip this step if you want to create an empty composer.json file and manually add the dependencies.

3. Define your project dependencies:
– Open the `composer.json` file in your favorite text editor.
– Add the required dependencies under the `require` or `require-dev` section.
– For example: `”require”: { “monolog/monolog”: “1.0.*” }`
– The format is `”vendor/package”: “version”`.
– You can also specify version constraints, like `”1.0.*”` means any version `1.0.x`.
– Save the file after adding the dependencies.

4. Install the dependencies:
– Run `composer install` in your terminal.
– Composer will read the `composer.json` file and download/install the required dependencies.
– It will create a `vendor` directory where all the dependencies will be installed.
– It will also create a `composer.lock` file that keeps track of the exact versions of the dependencies installed.

5. Autoloading:
– Composer provides a convenient way to autoload your classes using the `autoload` section in the `composer.json` file.
– By default, Composer generates an autoloader file in the `vendor` directory, but you can change the autoload setting in the `composer.json` file.
– For example: `”autoload”: { “psr-4”: { “MyNamespace\\”: “src” } }`.
– In this example, all classes in the `src` directory with the namespace `MyNamespace` will be automatically loaded.

6. Update the dependencies:
– Run `composer update` whenever you want to update the dependencies to their latest versions.
– Composer will read the `composer.json` file and update/install the dependencies based on the version constraints.
– It will update the `composer.lock` file with the new versions.

That’s it! You can now start using the installed dependencies in your PHP project.