PHP Inbuilt Functions

Posted on Updated on

PHP Functions

Php have many inbuilt function which increase the power of php. It has more than 1000 built-in functions.

PHP User Defined Functions

  • We can make own function beside inbuilt function.
  • A function is repeatedly in a program it is set of statements.
  • A function will not loads automatic when page load.
  • It will execute only when we call.
  • Create a User Defined Function in PHP

A function is declare with help of thekeyword “function”:

Syntax

function functionName() {
    code to be executed;
}

Note: function can start with alphabet and underscore not with a number.

In the example below, we create a function named “writeMsg()”. The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs “Hello world!”. To call the function, just write function name:

Example

<?php
function writeMsg() {
echo “Hello world!”;
}

writeMsg(); // call the function
?>

PHP Function Arguments

Information can be passed to functions through variables are known as arguments.

Arguments are declare in brackets with function name.we can add more arguments with help of comas.

The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:

Example

<?php
function familyName($fname) {
echo “$fnameRefsnes.<br>”;
}

familyName(“Jani”);
familyName(“Hege”);
familyName(“Stale”);
familyName(“Kai Jim”);
familyName(“Borge”);
?>

The following example has a function with two arguments ($fname and $year):

Example

<?php
function familyName($fname, $year) {
echo “$fnameRefsnes. Born in $year <br>”;
}

familyName(“Hege”, “1975”);
familyName(“Stale”, “1978”);
familyName(“Kai Jim”, “1983”);
?>

PHP Default Argument Value

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:

Example

<?php
function setHeight($minheight = 50) {
echo “The height is : $minheight<br>”;
}

setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

PHP Functions – Returning values

To let a function return a value, use the return statement:

Example

<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}

echo “5 + 10 = ” . sum(5, 10) . “<br>”;
echo “7 + 13 = ” . sum(7, 13) . “<br>”;
echo “2 + 4 = ” . sum(2, 4);
?>

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s