php

Exploring the Most Useful and Popular Predefined Functions in PHP

Built in functions are predefined functions in PHP.
These PHP inbuilt functions are what make PHP a very efficient and productive scripting language.
The built in functions of PHP can be classified and here are the top PHP Functions you must know before you start working as a PHP below.

The following functions are the PHP functions that you will be using day-to-day and it’s generated by my experience of developing with PHP.

PHP is a powerful server-side scripting language widely used for web development. One of its strengths lies in its extensive collection of predefined functions that simplify coding tasks and enhance functionality. In this blog post, we’ll explore some of the most useful and popular predefined functions in PHP, categorizing them based on their common use cases.

1. String Functions

String manipulation is a crucial aspect of web development, and PHP provides numerous built-in functions for handling strings.

  • strlen($string)
    • Description: Returns the length of a string.
    • Example:
$str = "Hello, World!";
echo strlen($str); // Output: 13

strpos($haystack, $needle)

  • Description: Finds the position of the first occurrence of a substring in a string.
  • Example:
$str = "Hello, World!";
echo strpos($str, "World"); // Output: 7

str_replace($search, $replace, $subject)

  • Description: Replaces all occurrences of the search string with the replacement string.
  • Example:
$str = "Hello, World!";
echo str_replace("World", "PHP", $str); // Output: Hello, PHP!

Array Functions

PHP arrays are versatile, and the language provides a wide range of functions for array manipulation.

  • array_push(&$array, $value1, $value2, ...)
    • Description: Adds one or more elements to the end of an array.
    • Example:
$array = [1, 2, 3];
array_push($array, 4);
print_r($array); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

array_merge($array1, $array2)

  • Description: Merges one or more arrays into a single array.
  • Example:
$array1 = ["a", "b"];
$array2 = ["c", "d"];
$merged = array_merge($array1, $array2);
print_r($merged); // Output: Array ( [0] => a [1] => b [2] => c [3] => d )

in_array($needle, $haystack)

  • Description: Checks if a value exists in an array.
  • Example:
$array = [1, 2, 3];
echo in_array(2, $array) ? 'Found' : 'Not Found'; // Output: Found

Date and Time Functions

Handling dates and times is often required in web applications, and PHP provides numerous functions for this purpose.

  • date($format, $timestamp)
    • Description: Formats a local date and time.
    • Example:
echo date('Y-m-d H:i:s'); // Output: Current date and time, e.g., 2024-10-24 15:23:50

<?php
echo() – Output one or more strings variable. 
print() or print_r() – Output a string and Prints human-readable information about a variable
exit() – Output a message and terminate the current script
phpinfo() – Outputs information about PHP’s configuration
require() or require_once() – require() includes and evaluates a specific file, while require_once() does that only if it has not been included before
isset() – Determine if a variable is declared and is different than NULL
count() – Count all elements in an array, or something in an object
explode() – Split a string by a string
implode() – Join array elements with a string
rand() – Generate a random integer
is_array() – Finds whether a variable is an array
array_push() – Push one or more elements onto the end of array
round() – Rounds a float
trim() – Strip whitespace (or other characters) from the beginning and end of a string
substr() – Return part of a string
str_replace() – Replace all occurrences of the search string with the replacement string
stripos() – Find the position of the first occurrence of a case-insensitive substring in a string
strlen() – Get string length
strtolower() or strtoupper() – Make a string lowercase and make a string uppercase
date() – Format a local time/date
strtotime() – Parse about any English textual DateTime description into a Unix timestamp
error_reporting() – Sets which PHP errors are reported
htmlspecialchars() – Convert special characters to HTML entities
json_decode() – Decodes a JSON string
json_encode() – Returns the JSON representation of a value
?>

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button