How to define and call a function in PHP?

by madie.koelpin , in category: PHP , 2 years ago

How to define and call a function in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@madie.koelpin You can define function in PHP starting from keyword function and then function name and { body of function }. You can call function by her name and below you can find small example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

// Define function in PHP
function sayHello() {
    echo "Hello World";
}

// Call function sayHello in PHP
sayHello();
// Output: Hello World


by aliyah.nikolaus , a year ago

@madie.koelpin 

In PHP, you can define a function using the "function" keyword, followed by the name of the function, and a set of parentheses that may contain parameters. The code for the function goes inside curly braces.


Example:

1
2
3
function myFunction($param1, $param2) {
    // code to be executed
}


To call a function, you simply need to use the function name, followed by parentheses and any necessary arguments.


Example:

1
myFunction($arg1, $arg2);