Data Analytics
27K subscribers
1.16K photos
24 videos
26 files
977 links
Dive into the world of Data Analytics – uncover insights, explore trends, and master data-driven decision making.
Download Telegram
Topic: PHP Basics – Part 5 of 10: Functions in PHP (User-Defined, Built-in, Parameters, Return)

---

1. What is a Function in PHP?

• A function is a block of code that performs a specific task and can be reused.

• PHP has many built-in functions, and you can also create your own user-defined functions.

---

2. Creating User-Defined Functions

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

greet(); // Call the function


Function names are case-insensitive.

---

3. Functions with Parameters

Functions can accept arguments (input values):

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

greetUser("Ali"); // Output: Hello, Ali!


• You can pass multiple parameters:

function add($a, $b) {
return $a + $b;
}

echo add(3, 5); // Output: 8


---

4. Default Parameter Values

• Parameters can have default values if not passed during the call:

function greetLanguage($name, $lang = "English") {
echo "Hello $name, language: $lang";
}

greetLanguage("Sara"); // Output: Hello Sara, language: English


---

5. Returning Values from Functions

function square($num) {
return $num * $num;
}

$result = square(6);
echo $result; // Output: 36


• Use the return statement to send a value back from the function.

---

6. Variable Scope in PHP

Local Scope: Variable declared inside function – only accessible there.

Global Scope: Variable declared outside – accessible inside with global.

$x = 5;

function showX() {
global $x;
echo $x;
}

showX(); // Output: 5


---

7. Anonymous Functions (Closures)

Functions without a name – often used as callbacks.

$square = function($n) {
return $n * $n;
};

echo $square(4); // Output: 16


---

8. Recursive Functions

• A function that calls itself.

function factorial($n) {
if ($n <= 1) return 1;
return $n * factorial($n - 1);
}

echo factorial(5); // Output: 120


---

9. Built-in PHP Functions (Examples)

strlen($str) – Get string length
strtoupper($str) – Convert to uppercase
array_sum($arr) – Sum of array elements
isset($var) – Check if variable is set
empty($var) – Check if variable is empty

---

10. Summary

Functions keep your code organized, reusable, and clean.

• Mastering parameters, return values, and scopes is key to effective programming.

---

Exercise

• Write a function that takes a name and age, and returns a sentence like:
"My name is Ali and I am 30 years old."

• Then, write a recursive function to compute the factorial of a number.

---

#PHP #Functions #PHPTutorial #WebDevelopment #Backend

https://t.iss.one/Ebooks2023
3
Data Analytics
# 📚 Java Programming Language – Part 2/10: Operators & Control Flow #Java #Programming #OOP #ControlFlow #Coding Welcome to Part 2 of our Java series! Today we'll explore operators and control flow structures. --- ## 🔹 Java Operators Overview Java provides…
# 📚 Java Programming Language – Part 3/10: Methods & Functions
#Java #Programming #Methods #Functions #OOP

Welcome to Part 3 of our Java series! Today we'll dive into methods - the building blocks of Java programs.

---

## 🔹 What are Methods in Java?
Methods are blocks of code that:
✔️ Perform specific tasks
✔️ Can be reused multiple times
✔️ Help organize code logically
✔️ Can return a value or perform actions without returning

// Method structure
[access-modifier] [static] return-type methodName(parameters) {
// method body
return value; // if not void
}


---

## 🔹 Method Components
### 1. Simple Method Example
public class Calculator {

// Method without return (void)
public static void greet() {
System.out.println("Welcome to Calculator!");
}

// Method with return
public static int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {
greet(); // Calling void method
int sum = add(5, 3); // Calling return method
System.out.println("Sum: " + sum);
}
}


### 2. Method Parameters
public static void printUserInfo(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}


### 3. Return Values
public static boolean isAdult(int age) {
return age >= 18;
}


---

## 🔹 Method Overloading
Multiple methods with same name but different parameters.

public class MathOperations {

// Version 1: Add two integers
public static int add(int a, int b) {
return a + b;
}

// Version 2: Add three integers
public static int add(int a, int b, int c) {
return a + b + c;
}

// Version 3: Add two doubles
public static double add(double a, double b) {
return a + b;
}

public static void main(String[] args) {
System.out.println(add(2, 3)); // 5
System.out.println(add(2, 3, 4)); // 9
System.out.println(add(2.5, 3.7)); // 6.2
}
}


---

## 🔹 Recursion
A method that calls itself.

### 1. Factorial Example
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}


### 2. Fibonacci Sequence
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}


---

## 🔹 Variable Scope
Variables have different scope depending on where they're declared.

public class ScopeExample {
static int classVar = 10; // Class-level variable

public static void methodExample() {
int methodVar = 20; // Method-level variable
System.out.println(classVar); // Accessible
System.out.println(methodVar); // Accessible
}

public static void main(String[] args) {
int mainVar = 30; // Block-level variable
System.out.println(classVar); // Accessible
// System.out.println(methodVar); // ERROR - not accessible
System.out.println(mainVar); // Accessible
}
}


---

## 🔹 Practical Example: Temperature Converter
public class TemperatureConverter {

public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9/5) + 32;
}

public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5/9;
}

public static void main(String[] args) {
System.out.println("20°C to Fahrenheit: " + celsiusToFahrenheit(20));
System.out.println("68°F to Celsius: " + fahrenheitToCelsius(68));
}
}


---

## 🔹 Best Practices for Methods
1. Single Responsibility Principle - Each method should do one thing
2. Descriptive Names - Use verbs (calculateTotal, validateInput)
3. Limit Parameters - Ideally 3-4 parameters max
4. Proper Indentation - Keep code readable
5. Documentation - Use JavaDoc comments
1