Day 9 PHP Function: Numeric, Built in, String, Date, User Defined

Day 9 PHP Function: Numeric, Built in, String, Date, User Defined
Day 9 PHP Function: Numeric, Built in, String, Date, User Defined

What is a Function? Why Use Functions?

A function in programming is a reusable block of code designed to perform a specific task. Functions are fundamental because they promote reusability, modularity, and cleaner code. In PHP, functions can accept inputs (arguments) and return outputs.

Why Use Functions?

  1. Code Reusability: Write once and reuse across scripts.
  2. Better Organization: Group logically related tasks together.
  3. Dynamic Behavior: Perform operations on dynamic data.
  4. Maintenance Ease: Update logic in one place instead of multiple locations.

Types of PHP Functions

1. Built-in Functions

These are pre-defined functions in PHP, categorized as:

  • String Functions: Manipulate text (e.g., strlen(), str_replace()).
  • Numeric Functions: Perform numeric operations (e.g., abs(), round()).
  • Date Functions: Handle date/time (e.g., date(), strtotime()).
  • File Functions: Manage file handling (e.g., fopen(), file_get_contents()).
  • Database Functions: Work with databases (e.g., mysqli_connect(), PDO).

Example of a built-in String Function:

php

<?php echo strlen("Hello World!"); // Outputs 12 ?>

2. User-Defined Functions

These are custom functions created by developers to meet specific needs.

Key Rules for User-Defined Functions
  • Must start with a letter or underscore (_), not a number.
  • Should have unique names.
  • Avoid spaces in names.
  • Use descriptive names for clarity.
Examples
  1. Basic Function:
php

<?php function greet() { echo "Hello, World!"; } greet(); // Outputs: Hello, World! ?>
  1. Function with Parameters:
php

<?php function add($num1, $num2) { return $num1 + $num2; } echo add(5, 10); // Outputs: 15 ?>
  1. Function with Default Parameters:
php

<?php function welcome($name = "Guest") { echo "Welcome, " . $name; } welcome(); // Outputs: Welcome, Guest welcome("Alice"); // Outputs: Welcome, Alice ?>
  1. Function Returning a Value:
php

<?php function multiply($a, $b) { return $a * $b; } $result = multiply(4, 5); echo $result; // Outputs: 20 ?>

Built-in Date Functions in PHP

PHP provides a rich set of date and time functions. Here are some commonly used ones:

  1. date(): Formats a local date/time.
php

<?php echo date("Y-m-d H:i:s"); // Outputs: 2024-12-04 12:00:00 ?>
  1. strtotime(): Converts a textual date into a Unix timestamp.
php
<?php $timestamp = strtotime("next Monday"); echo date("Y-m-d", $timestamp); // Outputs the next Monday's date ?>
  1. date_diff(): Calculates the difference between two dates.
php

<?php $date1 = date_create("2024-01-01"); $date2 = date_create("2024-12-31"); $diff = date_diff($date1, $date2); echo $diff->format("%R%a days"); // Outputs: +365 days ?>

Advantages of User-Defined Functions

  1. Custom Logic: Address specific application requirements.
  2. Reusable Code: Call the function across multiple scripts/pages.
  3. Simplified Debugging: Isolate and test individual blocks of code.
  4. Improved Readability: Enhance code structure with meaningful function names.

Example of Reusability in User-Defined Functions

php
<?php function calculateDiscount($price, $discountPercent) { return $price - ($price * $discountPercent / 100); } echo calculateDiscount(1000, 10); // Outputs: 900 echo calculateDiscount(500, 20); // Outputs: 400 ?>

Summary

  • Functions simplify and optimize PHP coding.
  • Built-in functions offer ready-to-use solutions for common tasks.
  • User-defined functions enable flexibility for unique needs.
  • Functions improve code organization, maintenance, and efficiency.

Blog categories