Common Functions in PHP - Part 1

String manipulation functions

strlen(): Returns the length of a string.

$str = "hello";
echo strtoupper($str); // Output: HELLO

strtoupper(): Converts a string to uppercase.

$str = "hello";
echo strtoupper($str); // Output: HELLO

strtolower(): Converts a string to lowercase.

$str = "WORLD";
echo strtolower($str); // Output: world

substr(): Extracts a portion of a string based on a starting position and length.

$str = "Hello, world!";
echo substr($str, 7, 5); // Output: world

 

Number manipulation functions

intval(): Converts a value to an integer.

$num = 10.5;
echo intval($num); // Output: 10

loatval(): Converts a value to a float.

$num = "3.14";
echo floatval($num); // Output: 3.14

number_format(): Formats a number with thousands separators.

$num = 1000;
echo number_format($num); // Output: 1,000

 

Array manipulation functions

count(): Counts the number of elements in an array.

$arr = [1, 2, 3, 4, 5];
echo count($arr); // Output: 5

array_push(): Adds an element to the end of an array.

$arr = [1, 2, 3];
array_push($arr, 4);
print_r($arr); // Output: [1, 2, 3, 4]

array_pop(): Removes and returns the last element of an array.

$arr = [1, 2, 3, 4];
$lastElement = array_pop($arr);
echo $lastElement; // Output: 4

 

These are just a few examples of commonly used functions in PHP. There are many more functions available for various tasks. You can explore the PHP documentation for more details on different functions and their usage.