What are the differences between functions and methods in PHP, and how are they used in classes?

In PHP, functions are standalone blocks of code that can be called independently, while methods are functions that are defined within a class and are called using an instance of that class. To use functions in classes, you can define them outside of the class and call them within the class methods. To use methods in classes, you define them within the class and call them using the object of that class.

<?php
// Defining a function outside of a class
function greet() {
    echo "Hello, World!";
}

class MyClass {
    // Using the function inside a class method
    public function sayHello() {
        greet();
    }
}

// Creating an object of the class
$obj = new MyClass();
// Calling the method that uses the function
$obj->sayHello();
?>