PHP has a vast set of built-in functions that every developer should know to write efficient and secure code. Here are some essential ones:
strlen($string): Returns the length of a string.strpos($haystack, $needle): Finds the position of the first occurrence of a substring.str_replace($search, $replace, $subject): Replaces occurrences of a string within another string.substr($string, $start, $length): Extracts a portion of a string.trim($string): Removes whitespace (or other characters) from the beginning and end of a string.count($array): Returns the number of elements in an array.array_merge($array1, $array2): Merges two or more arrays.array_push($array, $value): Adds an element to the end of an array.array_pop($array): Removes and returns the last element of an array.in_array($value, $array): Checks if a value exists in an array.array_keys($array): Returns all the keys from an array.array_values($array): Returns all the values from an array.file_get_contents($filename): Reads the entire file into a string.file_put_contents($filename, $data): Writes data to a file.fopen($filename, $mode): Opens a file for reading or writing.fclose($handle): Closes an open file handle.fwrite($handle, $data): Writes data to an open file handle.date($format): Formats a date/time string.strtotime($string): Converts a string to a Unix timestamp.time(): Returns the current Unix timestamp.mktime($hour, $minute, $second, $month, $day, $year): Creates a timestamp from given date and time.abs($number): Returns the absolute value of a number.ceil($number): Rounds a number up to the nearest integer.floor($number): Rounds a number down to the nearest integer.round($number, $precision): Rounds a number to a specified precision.rand($min, $max): Generates a random number.htmlspecialchars($string): Converts special characters to HTML entities to prevent XSS.md5($string): Generates an MD5 hash of a string (not recommended for security).password_hash($password, PASSWORD_DEFAULT): Hashes a password securely.password_verify($password, $hash): Verifies a hashed password.filter_var($data, FILTER_VALIDATE_EMAIL): Validates an email address.mysqli_connect($host, $user, $password, $dbname): Connects to a MySQL database.mysqli_query($conn, $query): Executes a query on the database.mysqli_fetch_assoc($result): Fetches a result row as an associative array.PDO::prepare($query): Prepares a SQL query for execution with parameter binding.PDO::execute(): Executes a prepared statement.json_encode($data): Converts an array or object into a JSON string.json_decode($json, true): Converts a JSON string into an array or object.error_log($message): Logs an error message.set_error_handler($callback): Defines a custom error handler.try { } catch (Exception $e) { }: Catches exceptions in a try block.die($message): Stops script execution and prints a message.exit($message): Stops script execution (similar to die()).isset($variable): Checks if a variable is set.empty($variable): Checks if a variable is empty.define('CONSTANT', 'value'): Defines a constant.