How can public, private, and protected keywords be used in PHP classes and what are their purposes?

In PHP classes, the public, private, and protected keywords are used to control the visibility of properties and methods within the class and its subclasses. - Public properties and methods can be accessed from outside the class. - Private properties and methods can only be accessed within the class itself. - Protected properties and methods can be accessed within the class and its subclasses.

class MyClass {
    public $publicVar;
    private $privateVar;
    protected $protectedVar;

    public function publicMethod() {
        // code here
    }

    private function privateMethod() {
        // code here
    }

    protected function protectedMethod() {
        // code here
    }
}