php3
php3
Faisal Salaheldeen
STEM Obour
PHP Functions
PHP Built-in Functions
PHP has over 1000 built-in functions that can be called
directly, to perform a specific task.
Examples of using PHP functions
is_int() check if the variable data type is integer.
is_float() to check if the type of a variable is float.
is_nan() to check if a value is not a number.
is_numeric() to find whether a variable is numeric.
pi() function returns the value of PI.
min() and max() to find the lowest or highest value in a list
of arguments.
PHP Functions
• Ex. echo(min(0, 150, 30, 20, -8, -200)); // returns -200
round() function rounds a floating-point number to its
nearest integer
Ex. echo(round(0.49)); // returns 0
rand() function generates a random number.
isset() Determine if a variable is declared and is different
than null.
unset(variables_name) to delete variable.
var_dump(variables_name) to information about a
variable.
empty — Determine whether a variable is empty.
strlen — Get string length.
PHP User Defined Functions
• Besides the built-in PHP functions, it is possible to create
your own functions.
• A function is a block of statements that can be used
repeatedly in a program.
• A function will be executed by a call to the function.
• Syntax
• function functionName() {
code to be executed;
}
PHP User Defined Functions
<?php
function writeMsg() {
echo "Hello world!";
}
• familyName("Hege","1975");
• familyName("Stale","1978");
• familyName("Kai Jim","1983");
• ?>
PHP Function Arguments
• Results of previous example.
• Hege Refsnes. Born in 1975
Stale Refsnes. Born in 1978
Kai Jim Refsnes. Born in 1983
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;
}
// Create connection
$conn = mysqli_connect($servername, $username,
$password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>