PHP program to print hello world using function

A simple php program to print hello world using function. This program will help you to understand how the function works in php programming language. PHP function is a chunk of code that can be reused numerous times. It can take input as argument list and return value. PHP comes with thousands of built-in functions. Conditional functions, Function within Functions, and Recursive functions can all be defined in PHP.

<?php 
function hello($name)
{
echo "Hello $name<br/>";
}
hello("inlarn");
hello("dotcom");
?>

 

Output:

Hello inlarn

Hello dotcom

What is functions in php and how it works

Other programming languages‘ functions are similar to PHP’s. A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. Many functions, such as fopen() and fread(), have already been encountered. A feature that can be used by end users and is built into an application. Most spreadsheet programs, for example, provide a built-in SUM function that sums up all cells in a row or column.

Why do we need functions in php

Functions allow you to run the same code numerous times. 2 Functions are used to break down large programs into smaller parts. 3 Other programmers can share and utilise the functions.