Using Functions
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
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.
<?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.
$myVar = somefunction();
Let's demonstrate this returning of a value by
using a simple function that returns the sum
of two integers.
Any question