Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
86 views

Function in PHP

Functions allow you to execute code as a single unit in PHP. There are two types of functions - built-in functions included with PHP and user-defined functions created by the programmer. Built-in functions include array, file system, and math functions. User-defined functions are defined using the function keyword and code is placed between curly braces. Functions can accept arguments to customize their behavior, and default values can be set for arguments if they are not provided upon calling the function.

Uploaded by

Sazal Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Function in PHP

Functions allow you to execute code as a single unit in PHP. There are two types of functions - built-in functions included with PHP and user-defined functions created by the programmer. Built-in functions include array, file system, and math functions. User-defined functions are defined using the function keyword and code is placed between curly braces. Functions can accept arguments to customize their behavior, and default values can be set for arguments if they are not provided upon calling the function.

Uploaded by

Sazal Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Function in PHP

Definition
Functions are the group of statements that you can execute as a single unit. Function
definations are the lines of code that make up a function.
The syntax for defining a function is:
<?php

function name function(parameters) { statements;}


?>
There are two types of functions.
1. Built-in functions.
2. 2. User defined functions.
Build in function
PHP has lots of built-in functions that we use all the time. Some of the function are
given below:
•PHP Array Functions
•PHP File System Functions
•Math functions
1.) PHP Array Functions
These functions allow you to interact with and manipulate arrays in various ways.
Arrays are essential for storing, managing, and operating on sets of variables. Some
Array Functions are: Array(), Array push(), Array pop() etc
EXAMPLE
<body>
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
PHP File System Functions

The filesystem functions are used to access and manipulate


the filesystem PHP provides you all the posible functions you
may need to manipulate a file.
List:
Copy(), delete(), file(), filetype() etc
Math functions
The math functions can handle values within the range of integer and float types.
List:
hexdec(), sqrt(), sin() etc
Example:
<?php
echo hexdec("1e") . "<br>";
echo hexdec("a") . "<br>";
echo hexdec("11ff") . "<br>";
echo hexdec("cceeff");
?>
User defined function
A user defined function is a user-defined set of commands that are carried out when the
function is called.
syntax:
Function functionName()
{
code to be executed;
}
NOTE
A function should start with keyword function and all the function code should be put
inside { and } brace.
A function name can start with a letter or underscore not a number.
The function names are case-insensitive
Php function argument
•Information can be passed to functions through arguments.
An argument is just like a variable.
•Arguments are specified after the function name, inside the
parentheses.
•You can add as many arguments as you want, just separate
them with a comma.
Example:
User defined function with argument:

<?php
function say_hello() {
echo “<p>Hello everybody!</p>”;
}
say_hello();
Function writeMSg() {
Echo “How are You?”;
}
writeMsg();
?>
Example:
User defined function with one argument:

<?php
function familyName($fname)
{
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Kai Jim");
familyName("Borge");
?>
Php default argument value
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);
?>

You might also like