Function is a reusable piece of code. You will write it once and you can use it many times. For example, We can make a function to perform addition or subtraction or calculation of VAT, And we can use it as many times as we want. Functions have a body structure{} In PHP the keywords for functions are: function, return
Functions can be defined in PHP using the following syntax:
function name($arg1, $arg2, ...) { // Function body goes here return $returnValue; }Functions do not have to return anything, so the return statement can be left out. Variables used within the function are local to that function and do not effect the global scope.
<?
//declearation of function
function nam()
{
echo "my name is Shahid";
}
//function call
echo nam();
?>
<?
//declearation of function
function nampara($f, $l)
{
echo "my first name is " . $f;
echo " AND my last name is " . $l;
}
//function call
echo nampara(shahid, khan);
?>
<?php
//function decleration
function add($a, $b)
{
$result;
$result = $a + $b;
return $result;
}
//function call
echo add(1, 2);
echo "<BR>";
//another function call
echo add(10, 20);
echo "<BR>";
//function decleration
function subtract($a, $b)
{
$result;
$result = $a - $b;
return $result;
}
//function call
echo subtract(5, 2);
echo "<BR>";
//function decleration
function vat($price)
{
return $total = $price * 17.5/100;
}
//function call
echo vat(100);
echo "<BR>";
echo vat(1000);
?>
3
30
3
17.5
175
PHP comes with many builtin functions. We don't have to define those functions. We just need to use them, by calling their names and passing them values. A list of all PHP builtin functions is given here http://www.php.net/quickref.php
PHP has nearly 3,000 built in functions covering pretty much anything you could want to do with a web application. Luckily, it also has a fantastic manual. The manual not only includes full documentation and example code for almost every function but also provides a user comments feature which is full of valuable tips. If you are having a problem with a PHP function check the manual page - the chances are someone else will have had the same problem and documented a solution in the comments section.
A useful shortcut for looking up the manual page for a function is to type www.php.net/name-of-the-function straight in to your browqser - the PHP site will redirect you to the manual page for that function. For example, www.php.net/date takes you straight to the manual page for the date() function.
Wed
Aug
<?php
/*
Date() is a builtin php function for showing datas.
we can pass it many different values/arguments.
For instance, if we pass it "D" it will show us Name of day in three characters
*/
echo date("D");
echo "<BR>";
echo date("M");
echo "<BR>";
?>
<?php echo date('l, jS F Y'); ?>
Using program value:
is_int($variable) // Returns true or false
is_float($variable) // Returns true or false
is_string($variable) // Returns true or false
addslashes($variable) // Escape single quote with a back slash
stripslashes($variable) // Escape back slash from string
Using form values:
strval(intval($variable))
strval(floatval($variable))
Array()EXAMPLE
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r($a);
?>
The output of the code above will be:
Array ( [a] => Dog [b] => Cat [c] => Horse )
<?php
//to send mail
mail($to, $subject, $message, $from);
echo "Thank you for sendin email";
?>
All built-in PHP functions can be found here:
http://www.php.net/quickref.php
all rights reseverd by Cramerz.com©. web development by
web design company
DotPeak. hosting by HostBay
all contents on cramerz are the property of Cramerz.com. web design courses