PHP functions are blocks of reusable code that perform specific tasks. They help in modularizing your code, making it more organized, maintainable, and easier to understand.
PHP Functions
Here's a list of some commonly used PHP functions
- echo: Outputs one or more strings.
- print: Outputs a string.
- strlen: Returns the length of a string.
- strpos: Finds the position of the first occurrence of a substring in a string.
- substr: Returns a part of a string.
- strtolower: Converts a string to lowercase.
- strtoupper: Converts a string to uppercase.
- trim: Strips whitespace (or other characters) from the beginning and end of a string.
- explode: Splits a string by a specified delimiter and returns an array.
- implode: Joins array elements with a string.
- array_push: Pushes one or more elements onto the end of an array.
- array_pop: Pops the element off the end of an array.
- count: Counts all elements in an array or something in an object.
- isset: Determines if a variable is set and is not null.
- empty: Checks if a variable is empty.
- array_key_exists: Checks if a specified key exists in an array.
- array_merge: Merges one or more arrays into one array.
- array_unique: Removes duplicate values from an array.
- file_get_contents: Reads entire file into a string.
- file_put_contents: Writes a string to a file.
- mail: Sends an email message.
- header: Sends a raw HTTP header.
- session_start: Starts new or resumes existing session.
- session_destroy: Destroys all data registered to a session.
- function_exists: Checks whether a function has been defined.
- date: Returns the current date/time formatted according to given format.
PHP has a vast standard library with many functions available for various tasks - check php documentation -.
How can I use this functions ?
In the following section, we'll take a break and explore some examples to learn how to use the previously mentioned PHP functions.
1- Echo:
<?php
echo "Hello, World!";
?>
2- print:
<?php
print "Hello, World!";
?>
3- strlen:
<?php
$str = "Hello, World!";
echo strlen($str); // Output: 13
?>
4- strpos:
<?php
$str = "Hello, World!";
echo strpos($str, "World"); // Output: 7
?>
5- substr:
<?php
$str = "Hello, World!";
echo substr($str, 7); // Output: World!
?>
6- strtolower:
<?php
$str = "Hello, World!";
echo strtolower($str); // Output: hello, world!
?>
7- strtoupper:
<?php
$str = "Hello, World!";
echo strtoupper($str); // Output: HELLO, WORLD!
?>
8- trim:
<?php
$str = " Hello, World! ";
echo trim($str); // Output: Hello, World!
?>
9- explode:
<?php
$str = "Hello, World!";
$arr = explode(", ", $str);
print_r($arr); // Output: Array ( [0] => Hello [1] => World! )
?>
10- implode:
<?php
$arr = array("Hello", "World!");
echo implode(", ", $arr); // Output: Hello, World!
?>
×