Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

6. Basic Functions in PHP

Uploaded by

imsisia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

6. Basic Functions in PHP

Uploaded by

imsisia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BASIC FUNCTIONS IN PHP

Functions are blocks of reusable code that perform specific tasks. They help reduce redundancy,
enhance readability, and improve the structure of PHP programs.

1. Defining and Calling Functions

Definition

• A function is a block of statements grouped to perform a specific task.


• Functions are defined once and can be called multiple times in the program.

Syntax
function functionName() {
// Code to execute
}

Example
function greet() {
echo "Hello, welcome to PHP!";
}

// Calling the function


greet(); // Outputs: Hello, welcome to PHP!

Real-World Application

• A greeting function displayed to users when they visit a website.

2. Function Parameters and Return Values

Function Parameters

• Parameters are variables passed to a function to influence its behaviour.


• They are declared in parentheses after the function name.

Syntax
function functionName($parameter1, $parameter2) {
// Code to execute
}

Example with Parameters


function calculateArea($length, $width) {
echo "The area is " . ($length * $width);
}

BASIC PHP - FUNCTIONS SOMATECH IT


calculateArea(5, 10); // Outputs: The area is 50

Return Values

• A function can return data to the caller using the return keyword.

Example with Return Values


function calculateArea($length, $width) {
return $length * $width;
}

$area = calculateArea(5, 10);


echo "The area is $area"; // Outputs: The area is 50

Real-World Application

• A calculator function for determining areas, prices, or discounts.

3. Variable Scope

Definition

• Variable scope determines where a variable can be accessed in a script.

A. Local Variables

• Defined inside a function and can only be accessed within that function.
• They are temporary and exist only during the function's execution.

Example
function showLocal() {
$localVar = "I am local";
echo $localVar;
}

showLocal(); // Outputs: I am local


// echo $localVar; // Error: Undefined variable

B. Global Variables

• Declared outside any function and can be accessed globally.


• Use the global keyword inside a function to access them.

BASIC PHP - FUNCTIONS SOMATECH IT


Example
$globalVar = "I am global";

function showGlobal() {
global $globalVar;
echo $globalVar;
}

showGlobal(); // Outputs: I am global

C. Static Variables

• Retain their value between function calls.


• Useful for counting or remembering the state of a function.

Example
function countCalls() {
static $count = 0; // Retains value across calls
$count++;
echo "Function called $count times<br>";
}

countCalls(); // Outputs: Function called 1 times


countCalls(); // Outputs: Function called 2 times

D. Function Parameters as Local Variables

• Parameters act as local variables and are initialized with values passed during function
calls.

Example
function greetUser($name) {
echo "Hello, $name!";
}

greetUser("Alice"); // Outputs: Hello, Alice!


greetUser("Bob"); // Outputs: Hello, Bob!

Real-World Examples of Functions

1. Greeting System
function greet($timeOfDay) {
return "Good $timeOfDay!";
}
BASIC PHP - FUNCTIONS SOMATECH IT
echo greet("Morning"); // Outputs: Good Morning!

2. Discount Calculator
function calculateDiscount($price, $discountRate) {
return $price - ($price * $discountRate / 100);
}

$price = 1000;
$discountRate = 10;
echo "Discounted Price: " . calculateDiscount($price,
$discountRate);
// Outputs: Discounted Price: 900

3. Login Attempts Counter


function trackLoginAttempts() {
static $attempts = 0;
$attempts++;
echo "Login attempts: $attempts<br>";
}

trackLoginAttempts(); // Outputs: Login attempts: 1


trackLoginAttempts(); // Outputs: Login attempts: 2

Key Points to Remember

1. Functions make code reusable and modular.


2. Parameters allow functions to accept input and process different data.
3. The return keyword allows functions to provide results to the caller.
4. Scope defines where a variable is accessible:
o Local for function-specific use.
o Global for variables used throughout the script.
o Static for retaining variable values across multiple calls.

Mastering functions is essential for creating clean, reusable, and efficient PHP programs.

BASIC PHP - FUNCTIONS SOMATECH IT

You might also like