+91 9952732367

PHP functions

PHP functions

A function is just a name we give to a block of code that can be executed whenever we need it. The main idea behind the function is reusability.

Syntax:


<?php

function functionname()
{
statement
}

?>

Example:


<?php

function myfun()

{

echo "s3learn.com";

}

myfun();

function myfunwitharg($arg1){

echo $arg1." s3learn.com"; // output is Hai s3learn.com.

}

myfunwitharg("Hai");

?>

Returning Values:


<?php
function square($num)
{
return $num * $num;
}
echo square(4); // outputs 16.

echo square(5); // outputs 25.

?>

Copyright © s3learn.com