Commonly used php built-in functions list with examples

PHP is a widely used server-side scripting language that is especially popular for creating dynamic web pages and web applications. One of the reasons for its popularity is its vast collection of built-in functions that can help with a wide range of tasks. In this article, we’ll explore some of the commonly used PHP built-in functions, organized into different types, with examples.

Commonly Used PHP Built-in Functions Types List with Popular Functions and Examples:

String Functions:

  • strlen() – Returns the length of a string
  • substr() – Returns a portion of a string
  • str_replace() – Replaces all occurrences of a search string with a replacement string
  • strtolower() – Converts a string to lowercase
  • strtoupper() – Converts a string to uppercase

Example:

$text = "Hello World!";echo strlen($text); // Output: 12echo substr($text, 0, 5); // Output: Helloecho str_replace("World", "Universe", $text); // Output: Hello Universe!echo strtolower($text); // Output: hello world!echo strtoupper($text); // Output: HELLO WORLD!

Read More :- Top 10 most used php string functions with examples and syntax

Array Functions:

  • count() – Returns the number of elements in an array
  • array_push() – Adds one or more elements to the end of an array
  • array_pop() – Removes the last element from an array
  • array_merge() – Merges two or more arrays into a single array
  • array_search() – Searches an array for a given value and returns the corresponding key if successful

Example:

$fruits = array("apple", "banana", "cherry");echo count($fruits); // Output: 3array_push($fruits, "orange");print_r($fruits); // Output: Array ( [0] => apple [1] => banana [2] => cherry [3] => orange )echo array_pop($fruits); // Output: orangeprint_r($fruits); // Output: Array ( [0] => apple [1] => banana [2] => cherry )$colors1 = array("red", "green");$colors2 = array("blue", "yellow");print_r(array_merge($colors1, $colors2)); // Output: Array ( [0] => red [1] => green [2] => blue [3] => yellow )print_r(array_slice($fruits, 0, 2)); // Output: Array ( [0] => apple [1] => banana )

Read More : Top 10 important php array functions with examples and syntax

Date and Time Functions:

  • time() – Returns the current Unix timestamp
  • date() – Formats a local time/date
  • strtotime() – Parses an English textual datetime description into a Unix timestamp
  • gmdate() – Formats a GMT/UTC date/time
  • strftime() – Formats a date/time according to local conventions

Example:

echo date("Y-m-d"); // Output: 2023-03-01echo time(); // Output: Current Unix timestampecho strtotime("next Friday"); // Output: Unix timestamp for next Fridayecho mktime(0, 0, 0, 1, 1, 2023); // Output: Unix timestamp for January 1, 2023echo strftime

File System Functions:

  • file_get_contents() – Reads entire file into a string
  • file_put_contents() – Write a string to a file
  • is_file() – Returns true if the file exists and is a regular file
  • is_dir() – Returns true if the file exists and is a directory
  • unlink() – Deletes a file

Example:

$text = file_get_contents("file.txt");echo $text; // Output: Contents of file.txtfile_put_contents("newfile.txt", "This is some text");echo is_file("file.txt"); // Output: trueecho is_dir("mydir"); // Output: trueunlink("file.txt"); // Deletes file.txt

Database Functions:

  • mysqli_connect() – Opens a new connection to the MySQL server
  • mysqli_query() – Performs a query on the database
  • mysqli_fetch_array() – Fetches a result row as an associative array, a numeric array, or both
  • mysqli_num_rows() – Returns the number of rows in a result set
  • mysqli_close() – Closes a previously opened database connection

Example:

// Establish connection to MySQL server$conn = mysqli_connect("localhost", "username", "password", "database_name");// Perform a query on the database$result = mysqli_query($conn, "SELECT * FROM users");// Fetch result rows as an associative arraywhile ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {    echo "Name: " . $row["name"] . " Email: " . $row["email"];}// Get number of rows in the result setecho "Total rows: " . mysqli_num_rows($result);// Close database connectionmysqli_close($conn);

Math Functions:

  • pow() – Returns the value of a number raised to a power
  • round() – Rounds a floating-point number to a specified number of decimal places
  • max() – Returns the highest value in an array
  • min() – Returns the lowest value in an array
  • rand() – Generates a random integer

Example:

echo rand(1, 10); // Output: Random number between 1 and 10echo abs(-5); // Output: 5echo floor(2.8); // Output: 2echo ceil(2.1); // Output: 3echo sqrt(16); // Output: 4

Other Functions:

  • isset() – Determines if a variable is set and is not null
  • empty() – Determines if a variable is empty
  • print_r() – Prints human-readable information about a variable
  • var_dump() – Dumps information about a variable
  • die() – Prints a message and terminates the current script

Example:

$var1 = "hello";$var2 = "";if (isset($var1)) {    echo "Variable var1 is set";}if (empty($var2)) {    echo "Variable var2 is empty";}$fruits = array("apple", "banana", "cherry");print_r($fruits); // Output: Array ( [0] => apple [1] => banana [2] => cherry )var_dump($fruits); // Output: array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(6) "cherry" }echo "Script terminated";die(); // Terminates the script

Conclusion:

In conclusion, PHP has a vast collection of built-in functions that can help with a wide range of tasks. In this article, we’ve explored some of the commonly used PHP built-in functions, organized into different types, with examples. Whether you’re working with strings and arrays, handling dates and times, working with files and directories, connecting to databases, performing math operations, or working with variables, there’s a PHP built-in function that can help. The best way to learn more about PHP built-in functions is to explore the PHP documentation and try out different functions in your own code.

Comments