Internet

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:


1. String Functions

  • 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.

2. Array Functions

  • 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.

3. File Handling Functions

  • 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.

4. Date & Time Functions

  • 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.

5. Math Functions

  • 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.

6. Security & Validation Functions

  • 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.

7. Database Functions (MySQLi & PDO)

  • 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.

8. JSON Handling

  • 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.

9. Error Handling

  • 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.

10. Miscellaneous

  • 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.

Leave A Comment


Last Visited Articles


Info Board

Visitor Counter
0
 

Todays visit

47 Articles 6709 RSS ARTS 15 Photos

Popular News

🚀 Welcome to our website! Stay updated with the latest news. 🎉

United States

216.73.216.234 :: Total visit:


Welcome 336.73.336.334 Click here to Register or login
Oslo time:2025-07-09 Whos is online (last 1 min): 
1 - United States - 996.73.996.934
2 - United States - 34.205.863.803
3 - United States - 38.334.334.6
4 - United States - 44.204.487.44
5 - United States - 44.296.228.6
6 - United States - 54.852.863.42
7 - United States - 52.0.705.244
8 - United States - 34.225.24.989
9 - United States - 91.12.214.73
10 - United States - 207.96.93.902
11 - United States - 52.7.53.543
12 - United States - 34.774.737.777
13 - Singapore - 46.668.660.666
14 - United States - 58.207.79.544
15 - United States - 50.11.193.48
16 - United States - 34.295.242.30


Farsi English Norsk RSS