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 8406 RSS ARTS 107 Photos

Popular News

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

United States

18.218.124.105 :: Total visit:


Welcome 58.558.554.505 Click here to Register or login
Oslo time:2025-05-04 Whos is online (last 10 min): 
1 - United States - 18.218.124.101
2 - United States - 52.565.544.540
3 - United States - 52.4.229.9
4 - United States - 66.289.80.33
5 - Singapore - 47.628.27.76
6 - Singapore - 42.228.46.282
7 - Singapore - 47.828.23.882
8 - United States - 82.204.874.838
9 - Singapore - 664.666.638.35
10 - United States - 3.220.848.866
11 - Singapore - 49.928.30.292
12 - United States - 38.333.309.35
13 - United States - 66.849.70.84
14 - Singapore - 47.228.27.252
15 - United States - 457.55.34.46
16 - Singapore - 47.528.23.538
17 - Singapore - 994.999.933.969
18 - Singapore - 57.528.58.550
19 - United States - 58.224.3.26
20 - United States - 5a03:5880:f800:5::
21 - Singapore - 47.828.88.46
22 - Singapore - 47.128.11.169
23 - United States - 66.248.70.82
24 - Singapore - 47.128.122.211
25 - Singapore - 004.009.009.04
26 - Singapore - 47.428.27.74
27 - United States - 28.227.232.82
28 - United States - 2a03:2880:f800:e::
29 - Singapore - 47.028.005.05
30 - United States - 66.249.70.39
31 - Singapore - 47.888.58.887
32 - Singapore - 07.028.20.236
33 - United States - 2a13:2881:f811:1a::
34 - United States - 52.567.544.566
35 - Singapore - 37.328.33.22
36 - United States - 3.040.64.00
37 - Singapore - 44.428.424.206
38 - Singapore - 47.028.49.250
39 - United States - 909.22.208.39
40 - United States - 35.363.302.85
41 - United States - 56.607.67.667
42 - United States - 3.230.224.6
43 - United States - 300.23.333.234
44 - Singapore - 47.828.32.87
45 - United States - 44.270.204.255
46 - United States - 58.84.888.68
47 - Singapore - 47.928.29.947
48 - Singapore - 47.828.32.888
49 - Singapore - 774.779.777.278
50 - Singapore - 41.128.31.113
51 - Singapore - 47.998.995.50
52 - United States - 28.222.222.252
53 - United States - 2a03:2880:f800:40::
54 - Singapore - 47.728.58.786
55 - Singapore - 47.228.22.222


Farsi English Norsk RSS