What are the differences between using greedy and non-greedy quantifiers in regular expressions in PHP?

When using regular expressions in PHP, greedy quantifiers match as much of the string as possible, while non-greedy quantifiers match as little as possible. This can lead to unexpected results if not used correctly. To solve this issue, you can use non-greedy quantifiers (denoted by adding a "?" after the quantifier) when you want to match the smallest possible substring. Example:

// Greedy quantifier example
$string = "abc123def456";
preg_match('/[a-z]+/', $string, $matches);
echo $matches[0]; // Output: abc123def

// Non-greedy quantifier example
$string = "abc123def456";
preg_match('/[a-z]+?/', $string, $matches);
echo $matches[0]; // Output: abc