Functions With PHP
Functions With PHP
By
Sandun Samaratunga
Functions in PHP
Introduction
Functions are the most important part of any programming language,
PHP included. You can say in a few words that functions are pieces of
code that accept values and produce results.
function is a block of code that is not immediately executed, but can be
called by your scripts when needed. Functions can either be built-in or
defined by the user. PHP has a lot of built-in functions; one of them is
print().
Functions help you create organized and reusable code. They allow you
to abstract out details so your code becomes more flexible and more
readable. Without functions, it is impossible to write easily
maintainable programs because you're constantly updating identical
blocks of code in multiple places and in multiple files.
PHP Code:
<?php
function myFunction() //no semicolon here!
{
print <h1>This is my first function</h1>;
}
myFunction();
?>
Note: Your function name can start with a letter or underscore "_", but
not a number!
To declare a function, use the function keyword, followed by the name
of the function and any parameters in parentheses. To invoke a
function, simply use the function name, specifying argument values for
any parameters to the function. If the function returns a value, you can
assign the result of the function to a variable.
Variable Scope
Up to now you can used declared any variable anywhere in a page.
With functions this is no longer always true. Functions keep their sets
of variables that are distinct from those of the page and of page and of
other functions.
The Variables defined in a function, including its parameters, are not
accessible outside of the function, and by default, variables defined
outside a function are not accessible inside the function.
<?php
$text='Hey Im in a pag';
?>
<P>this is a web page<P>
<?php
echo $text;
?>
<BR>
<?php
$a=3;
function Add()
{
$a+=2;
echo $a.'<BR>';
}
Add();
echo $a;
?>
Global Variable
If you want a variable in the global scope be accessible with in a
function, you can use the global keyword.
<?php
$a=3;
function Add()
{
global $a;
$a+=2;
echo $a;
}
Add();
echo $a;
?>
3
You must include the global keyword in a function before any uses of the
global variable or variables you want to access. Because they are
declared before the body of the function, Function parameters can never
be globale.
Static Variables
Static variable is shared between all calls to the functions and it is
initialized only the first time the function is, called.
Use static keyword to declare a static variable.
<?php
Function counter(){
static $count=0;
return $count++;
}
for($i=0;$i<=5;$i++)
{
echo counter ();
}
?>
Exercise 2
Define a function that takes number as input, incremented by one and
printed on the screen.
Outside the function, declare variable and initialize it to 10. Call to the
function and print the variable (outside the function)
Exercise 3
Define a function that takes 2 parameters (String and character
representing text effect) and prints in that string with the text effect.
Default text effect should be Bold. If user wants, he can pass
character for Italics and Underline too.
Exercise 6
Extend the above example so that time part HOURS get returned and
to display some greeting message based on the time. Eg: if hrs<12,
Good Morning.
10
Predefined functions
Date manipulation function
Get Current date (date() function)
<?php
$today = date("F j, Y");
echo "$today";
$today = date("d / M/ Y");
echo "$today";
?>
Write a program to get the date in 12th December 2008 like?
<?php
// Assuming today is: March 10th, 2001, 5:16:18 pm
$today = date("F j, Y, g:i a");
pm
$today = date("m.d.y");
$today = date("j, n, Y");
$today = date("Ymd");
$today = date('h-i-s, j-m-y, it is w Day z ');
1631 1618 6 Fripm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');
$today = date("D M j G:i:s T Y");
MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');
$today = date("H:i:s");
?>
03.10.01
10, 3, 2001
20010310
05-16-17, 10-03-01,
Note: This can be more reliable than simply adding or subtracting the number of seconds
in a day or month to a timestamp because of daylight saving time.
11