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

42 Articles 7383 RSS ARTS 13 Photos

Popular News

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

United States

3.141.24.158 :: Total visit:


Welcome 3.848.84.858 Click here to Register or login
Oslo time:2025-05-01 Whos is online (last 10 min): 
1 - United States - 3.646.24.668
2 - Singapore - 47.888.44.58
3 - United States - 20.444.204.483
4 - Singapore - 42.228.26.245
5 - United States - 4.444.29.244
6 - Singapore - 47.79.005.075
7 - Singapore - 40.028.90.022
8 - United States - 48.447.497.488
9 - Singapore - 47.008.07.06
10 - United States - 3.23.33.329
11 - Singapore - 57.528.26.63
12 - United States - 55.45.55.533
13 - United States - 34.234.200.207
14 - United States - 3.255.35.239
15 - Singapore - 41.19.191.43
16 - United States - 98.999.959.96
17 - Singapore - 46.628.666.69
18 - United States - 41.110.102.221
19 - United States - 78.278.24.244
20 - Singapore - 44.49.243.244
21 - Singapore - 47.228.27.222
22 - United States - 08.007.070.00
23 - Japan - 43.467.457.80
24 - United States - 757.55.37.6
25 - Singapore - 49.929.34.950
26 - United States - 3.148.171.222
27 - United States - 68.688.66.266
28 - Singapore - 47.020.40.234
29 - United States - 66.449.77.444
30 - Singapore - 47.79.288.249
31 - United States - 58.556.53.558
32 - Singapore - 47.424.22.494
33 - Singapore - 114.119.133.14
34 - United States - 43.166.134.41
35 - Singapore - 47.728.22.787
36 - Singapore - 47.79.598.555


Farsi English Norsk RSS