how to call function in php?

Today, We want to share with you how to call function in php?.In this post we will show you what is function in php?, hear for php function use we will give you demo and example for implement.In this post, we will learn about calling javascript function from php code with an example.

How to Define and Call a Function in PHP?

Example 1: call function using php code

<?php

function onlineFreeGetAppoiments(){
    echo "Today is " . date('l', mktime());
}

onlineFreeGetAppoiments();
?>

Functions with Parameters

Example 2: Defining function &

<?php

function fetchTotal($ankno, $ankno2){
  $sum = $ankno + $ankno2;
  echo "the total Sum of the two numbers $ankno and $ankno2 is : $sum";
}
 
fetchTotal(10, 20);
?>

Functions with Optional Parameters and Default Values

<?php

function genratedName($font, $size=1.5){
    echo "<p style=\"font-family: $font; font-size: {$size}em;\">Welcome To Pakainfo.com!</p>";
}
 
genratedName("Arial", 2);
genratedName("Times", 3);
genratedName("Courier");
?>

Returning Values from a Function

<?php

function fetchTotal($ankno, $ankno2){
    $total = $ankno + $ankno2;
    return $total;
}
 

echo fetchTotal(5, 10); // Results: 15
?>

<?php
// Defining function
function divideNumbers($dividend, $divisor){
    $quotient = $dividend / $divisor;
    $array = array($dividend, $divisor, $quotient);
    return $array;
}
 
list($dividend, $divisor, $quotient) = divideNumbers(10, 2);
echo $dividend;  // Results: 10
echo $divisor;   // Results: 2
echo $quotient;  // Results: 5
?>

Passing Arguments to a Function by Reference

<?php

function selfMultiply(&$anknober){
    $anknober *= $anknober;
    return $anknober;
}
 
$myankno = 5;
echo $myankno; // Results: 5
 
selfMultiply($myankno);
echo $myankno; // Results: 25
?>

Understanding the Variable Scope

<?php

function example(){
    $welcomemsg = "Welcome Pakainfo!";
    echo $welcomemsg;
}
 
example(); // Results: Welcome Pakainfo!
 
echo $welcomemsg; // Generate undefined variable error
?>

<?php
$welcomemsg = "Welcome Pakainfo!";
 
// Defining function
function example(){
    echo $welcomemsg;
}
 
example();  // Generate undefined variable error
 
echo $welcomemsg; // Results: Welcome Pakainfo!
?>

The global Keyword

<?php
$welcomemsg = "Welcome Pakainfo!";
 
function example(){
    global $welcomemsg;
    echo $welcomemsg;
}
 
example(); // Outpus: Welcome Pakainfo!
echo $welcomemsg; // Outpus: Welcome Pakainfo!
 
// Assign a new value to variable
$welcomemsg = "Thanks a lot";
 
example(); // Results: Thanks a lot
echo $welcomemsg; // Results: Thanks a lot
?>

Creating Recursive Functions

<?php

function printValues($arr) {
    global $count;
    global $products;
    
    if(!is_array($arr)){
        die("ERROR: Input is not an array");
    }
    
    foreach($arr as $a){
        if(is_array($a)){
            printValues($a);
        } else{
            $products[] = $a;
            $count++;
        }
    }
    
    return array('total' => $count, 'values' => $products);
}
 
$species = array(
    "mobiles" => array(
        "lenova",
        "oppo",
        "vivo"
    ),
    "products" => array(
        "Eles",
        "tamil" => array(
            "bolly4u",
            "new",
            "link"
        ),
        "2021 nmew",
        "z570 v"
    ),
    "languages" => array(
        "programme" => array(
            "server" => array(
                "PHP",
                "Laravel"
            ),
            "Nodejs",
            "Angularjs"
        ),
        "English",
        "Hindi" => array(
            "tamilrokers",
            "Jiorokers"
        )
    )
);
 
$result = printValues($species);
echo $result['total'] . ' value(s) found: ';
echo implode(', ', $result['values']);
?>

I hope you get an idea about php functions list.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.