start learning
Image 1
5PHPFunctions

PHP Functions

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.

Here's a list of some commonly used PHP functions

  1. echo: Outputs one or more strings.
  2. print: Outputs a string.
  3. strlen: Returns the length of a string.
  4. strpos: Finds the position of the first occurrence of a substring in a string.
  5. substr: Returns a part of a string.
  6. strtolower: Converts a string to lowercase.
  7. strtoupper: Converts a string to uppercase.
  8. trim: Strips whitespace (or other characters) from the beginning and end of a string.
  9. explode: Splits a string by a specified delimiter and returns an array.
  10. implode: Joins array elements with a string.
  11. array_push: Pushes one or more elements onto the end of an array.
  12. array_pop: Pops the element off the end of an array.
  13. count: Counts all elements in an array or something in an object.
  14. isset: Determines if a variable is set and is not null.
  15. empty: Checks if a variable is empty.
  16. array_key_exists: Checks if a specified key exists in an array.
  17. array_merge: Merges one or more arrays into one array.
  18. array_unique: Removes duplicate values from an array.
  19. file_get_contents: Reads entire file into a string.
  20. file_put_contents: Writes a string to a file.
  21. mail: Sends an email message.
  22. header: Sends a raw HTTP header.
  23. session_start: Starts new or resumes existing session.
  24. session_destroy: Destroys all data registered to a session.
  25. function_exists: Checks whether a function has been defined.
  26. 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!
?>

×