UNIT II (Functions)
UNIT II (Functions)
Built-In functions:
These are pre-defined functions provided by PHP.
They cover a wide range of tasks, including string manipulation, mathematical operations,
and file handling.
Examples include:
String Functions
Functions like strlen(), strpos(), and substr() manipulate strings.
<?php
echo substr("Hello world",6);
?>
Math Functions
Functions such as abs(), round(), and rand() perform math.
Array Functions
Functions like array_push(), array_pop(), and count() manage arrays.
User-defined function
These are custom functions created by developers to perform specific tasks.
They allow for tailored functionality that can be reused throughout the application.
<?php
function greet() {
echo "Hello, World!";
}
greet();
?>
Defining a function
// Function call
greet();
?>
Variable Scope
myFunction();
echo $localVar; // Error: Undefined variable $localVar
?>
Global Scope
Variables declared outside of any function have a global scope.
These variables are accessible from any part of the script except inside
functions, unless explicitly specified.
<?php
$globalVar = "I'm global";
function myFunction() {
echo $globalVar; // Error: Undefined variable $globalVar
}
myFunction();
echo $globalVar; // Outputs: I'm global
?>
To access a global variable within a function, use the global keyword or
the $GLOBALS array.
<?php
$globalVar = "I'm global";
function myFunction() {
global $globalVar;
echo $globalVar; // Outputs: I'm global
}
myFunction();
?>
Static Variables
Variables declared as static inside a function retain their value between function calls.
This is useful for keeping track of information across multiple invocations of a function
without using global variables.
<?php
function myCounter() {
static $count = 0;
$count++;
echo $count;
}
myCounter(); // Outputs: 1
myCounter(); // Outputs: 2
myCounter(); // Outputs: 3
?>
Function Parameters
Function parameters are variables specified in a function's declaration
that accept values, known as arguments, when the function is called.
These parameters enable functions to process dynamic inputs,
enhancing code flexibility and reusability.
Defining Function Parameters:
Parameters are declared within the parentheses following the function name.
Multiple parameters are separated by commas.
<?php
function multiply($a, $b)
{
echo “$a * $b”;
}
?>
Calling Functions with Arguments:
When invoking a function, provide arguments corresponding to the defined
parameters.
<?php
multiply(5, 10);
?>
Default Parameter Values:
PHP allows default values for parameters, making them optional during function calls.
<?php
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet(); // Outputs: Hello, Guest!
greet("Alice"); // Outputs: Hello, Alice!
?>
If no argument is provided, $name defaults to "Guest".
Named Arguments (PHP 8.0+):
PHP 8.0 introduced named arguments, allowing you to specify arguments by
parameter name, enhancing readability and flexibility.
function configure($width = 100, $height = 200, $color = 'blue') {
}
configure($height: 300, $color: 'red');
Type Declarations:
PHP supports type declarations for parameters, enforcing the type of arguments
passed to functions.
Output: 8
The sum is:
Variable Functions
Here, $func = "greet"; stores the function name in a variable, and $func();
calls that function dynamically.
Example with parameters
<?php
function greet($name) {
echo "Hello, $name!";
}
$func = "greet";
$func("John"); // Calls greet("John")
?>
Here, $func("John"); dynamically calls greet("John").
Selecting a Function Dynamically
Imagine a simple calculator where you choose an operation at
runtime.
<?php
function add($a, $b) {
return $a + $b;
}
function multiply($a, $b) {
return $a * $b;
}
$operation = "add";
echo $operation(5, 3); // Calls add(5, 3) and outputs 8
$operation = "multiply";
echo "<br>" . $operation(5, 3); // Calls multiply(5, 3) and outputs 15
?>
When to Use Variable Functions?
Dynamic Function Calls – When the function name needs to be decided at
runtime.
Switching Between Functions – As seen in the calculator example.
Event Handling – Deciding what function to execute based on user input.
Anonymous Functions
<?php
$greet = function($name) {
return "Hello, $name!";
};
echo $greet("John");
?>
Here, $greet acts like a function name, but it’s actually a variable storing a
function.
We call it using $greet("John"); like a normal function.
Using use() to Access Outer Variables
Since an anonymous function doesn’t have access to outside variables by
default, we use use() to pass them.
<?php
$multiplier = 3;
echo $multiply(5);
?>
The use ($multiplier) allows the function to access $multiplier from outside
its scope.
Date and Time Functions
PHP provides built-in functions to work with date and time, allowing
developers to format, manipulate, and display date and time information
dynamically.
The date() function is used to format and display the current date and time.
<?php
echo date("Y-m-d"); // Output: 2025-02-05 (Year-Month-Day)
?>
<?php
echo date("H:i:s"); // Output: 14:30:45 (Hour:Minute:Second)
?>