Using Functions
PHP
The best way to develop and maintain a large
program is to construct it from smaller pieces
(functions) each of which is more manageable
than the original program.
Testing variable status
PHP supports a series of variable testing
functions that return true or false.
is_numeric() tests if a variable is a valid
number or a numeric string.
is_set() tests if a variable exists.
empty() checks if a variable is empty
3
is_numeric()
<?php
echo '2 ', (is_numeric('2') ? "true" : "false") . "<br>";
echo '2.4 ', (is_numeric('2.4') ? "true" : "false") . "<br>";
echo '2.2b ', (is_numeric('2.2b') ? "true" : "false") . "<br>";
echo '.5 ', (is_numeric('.5') ? "true" : "false") . "<br>";
echo '1.2.5 ', (is_numeric('1.2.5') ? "true" : "false") . "<br>";
echo 'A.5 ', (is_numeric('A.5') ? "true" : "false") . "<br>";
?>
4
is_set()
Checks if variable exists.
Most useful for checking array elements:
if (!is_set($_POST[name] && !is_set($_POST[score] )
{
print (Error you must specify name and score);
}
Note: from php.net contrib notes:
<?php
$a = 0; print isset($a) ? "TRUE<br>" : "FALSE<br>"; //TRUE
$b = "0"; print isset($b) ? "TRUE<br>" : "FALSE<br>"; //TRUE
$c = ""; print isset($c) ? "TRUE<br>" : "FALSE<br>"; //TRUE
$d = 1; print isset($d) ? "TRUE<br>" : "FALSE<br>"; //TRUE
print isset($e) ? "TRUE<br>" : "FALSE<br>"; //FALSE
$f= TRUE; print isset($f) ? "TRUE <br>" : "FALSE<br>"; //TRUE
$g= FALSE; print isset($g) ? "TRUE<br>" : "FALSE<br>"; //TRUE
$h=array();print isset($h) ? "TRUE<br>" : "FALSE<br>"; //TRUE 5
?>
empty()
Check if a variable is empty. Am empty string is:
"" (an empty string)
0 (integert) or "0" (string)
NULL, or FALSE
array() (an empt)
var $var; (a variable declared, but without a value in a class
if (empty($_POST[name] && empty($_POST[score] ) {
print (Error you must specify name and score);
}
Note: from php.net contrib notes:
$a =
0 ; print empty($a) ? "TRUE" : "FALSE"; //TRUE
$b =
"0" ; print empty($b) ? "TRUE" : "FALSE"; //TRUE
$c =
"" ; print empty($c) ? "TRUE" : "FALSE"; //TRUE
$d =
1 ; print empty($d) ? "TRUE" : "FALSE"; //FALSE
print empty($e) ? "TRUE" : "FALSE"; //TRUE
$f= TRUE ; print empty($f) ? "TRUE" : "FALSE"; //FALSE
$g= FALSE; print empty($g) ? "TRUE" : "FALSE"; //TRUE 6
$h=array();print empty($h) ? "TRUE" : "FALSE"; //TRUE
A function
Is just a name we give to a block of code that
can be executed whenever we need it.
When you create a function, you first need to
give it a name, like Motto. It's with this
function name that you will be able to call
upon your function, so make it easy to type
and understand.
User-defined Functions
All functions and classes in PHP have the
global scope.
Functions need not be defined before they
are referenced
A function may be defined using
syntax such as the following:
<?php
function Motto()
{
}
?>
Note: Your function name can start with a letter
or underscore "_", but not a number!
An example
<?php
function Motto()
{
echo "We deliver quantity, not quality!<br />";
}
?>
That's it! You have written your first PHP function from scratch!
Notice that the code that appears within a function is just the same as
any other PHP code.
Using Your PHP Function
A simple PHP script. Let's do two things: PHP Code with Function
add the function code to it and use the
function twice. <?php
function Motto(){
PHP Code echo "We deliver quantity, not quality!<br
/>";
<?php
}
echo "Welcome to Tizag.com <br />";
echo "Welcome to Tizag.com <br />";
echo "Well, thanks for stopping by! <br />"; Motto();
echo "and remember... <br />"; echo "Well, thanks for stopping by! <br />";
?> echo "and remember... <br />";
Motto();
?>
.
Display
Welcome to Tizag.com
We deliver quantity, not quality!
Well, thanks for stopping by!
and remember...
We deliver quantity, not quality!
When you are creating a function,
follow these simple guidelines:
Always start your function with the keyword
function
Remember that your function's code must be
between the "{" and the "}"
When you are using your function, be sure
you spell the function name correctly
Don't give up!
PHP Functions - Parameters
Another useful thing about functions is that
you can send them information that the
function can then use.
However, if we were to use parameters, then
we would be able to add some extra
functionality! A parameter appears with the
parentheses "( )" and looks just like a normal
PHP variable.
Let's create a new function that creates a
custom greeting based off of a person's
name.
PHP Code with Function:
It is also possible to have multiple parameters in a function.
To separate multiple parameters PHP uses a comma ",". Let's
modify our function to also include last names.
PHP Functions - Returning Values
Besides being able to pass functions information, you can also
have them return a value. However, a function can only return
one thing, although that thing can be any
integer, float, array, string, etc.
To capture this value you can set a variable equal to the
function. Something like:
$myVar = somefunction();
Let's demonstrate this returning of a value by
using a simple function that returns the sum
of two integers.
Any question