Preloader

Practice Exercises: PHP Functions

PHP Tutorials
codevigyaan php

PHP functions practice exercises are one of the best of PHP beginner to learn how function in real PHP programs. In this post, PHP functions practice exercises we will teach you step by step with simple exercises with answers. Parameters, return values, default arguments, variable scope and variable length arguments from the PHP functions practice exercises provides a good picture of the topic from beginning itself.

Exercise 1: Simple Function

Create a function named sayHello that prints Hello, PHP.

Task
Call the function once.

Exercise 2: Function with Parameters

Write a function that accepts two numbers and prints their addition.

Task
Pass values 20 and 30.

Exercise 3: Function with Return Value

Create a function that accepts one number and returns its square.

Task
Store the returned value in a variable and print it.

Exercise 4: Function with Default Argument

Write a function that prints a welcome message.
If no name is passed, it should print Welcome Guest.

Exercise 5: Function with Multiple Arguments

Create a function that calculates the total of three marks.

Task
Pass values 70, 80, and 90.

Exercise 6: Function Using Global Variable

Create a global variable $discount with value 10.
Write a function that uses this variable to calculate the final price of a product.

Task
Product price is 1000.

Exercise 7: Function with Static Variable

Create a function that counts how many times it is called.

Task
Call the function three times.

Exercise 8: Variable Arguments Function

Write a function that accepts any number of values and prints their total.

Task
Pass 5, 10, 15, and 20.

Answers with Explanation

1: Simple Function

<?php
function sayHello() {
    echo "Hello, PHP";
}
sayHello();
?>

2: Function with Parameters

<?php
function addNumbers($a, $b) {
    echo $a + $b;
}
addNumbers(20, 30);
?>

3: Function with Return Value

<?php
function square($num) {
    return $num * $num;
}
$result = square(5);
echo $result;
?>

4: Function with Default Argument

<?php
function welcome($name = "Guest") {
    echo "Welcome " . $name;
}
welcome();
welcome("Amit");
?>

5: Function with Multiple Arguments

<?php
function totalMarks($m1, $m2, $m3) {
    echo $m1 + $m2 + $m3;
}
totalMarks(70, 80, 90);
?>

6: Function Using Global Variable

<?php
$discount = 10;
function calculatePrice($price) {
    global $discount;
    return $price - ($price * $discount / 100);
}
echo calculatePrice(1000);
?>

7: Function with Static Variable

<?php
function callCounter() {
    static $count = 0;
    $count++;
    echo $count . "<br>";
}
callCounter();
callCounter();
callCounter();
?>

Answer 8: Variable Arguments Function

<?php
function totalSum(...$values) {
    $sum = 0;
    foreach ($values as $v) {
        $sum += $v;
    }
    echo $sum;
}
totalSum(5, 10, 15, 20);
?>

Final Tip

Practise writing functions everday. Begin with very simple example and gradually incorporate parameters, return values and scope. Good knowledge of function will make PHP coding much easier and clutter free.

For free projects, check our Bootstrap Templates section.
Visit → https://codevigyaan.com/bootstrap-projects

Want E-Books?
Open →https://codevigyaan.com/free-e-books/

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *