jump to navigation

handling functions errors September 24, 2008

Posted by Nsh15 in Basic PHP programming, php.
Tags: , , , ,
add a comment

Sometimes functions fail, sad but true. ur script should anticipate any possible function failer and handle the situation like displaying ur wn message error. this can be done using die statement

function_name () or die(“ur message“);

The die statement stops the script and prints out whatever u haved entered in its message. is is when the function returns false !

but remeber that u should shut off the display for error messages, otherwise ur used will c 2 error messages.

U can also handle possible functions faileres by using the function call as condition.

if (!function_name($variable))

{

echo “ur error message here“;

exit();

}

the exit statement does the same as die statement!

Passing values to a function September 24, 2008

Posted by Nsh15 in Basic PHP programming, php.
Tags: , ,
add a comment

you can pass values to a function by adding the values between parantheses when u call the function but before that u should define the values while defining the function

While passing values, make sure of the following:

1. passing the right data type:

the value passed can be any data type, including arrays and objects. A good practice is to check the value type before using it. especailly when u r taking these values from the user.

To use built-in functions that check the type, see this previous post:

Changing the order of statement execution

2. passing values in the correct order:

when u call the function test($a,$b); using test($x,$y);

then $a = $x and $b = $y …. each variable will take a value in order, if u call test($y,$x);

then $a = $y and $b = $x ,,, which is not the same … so pay attention to the order!

3. passing the correct number of values:

If u pass less numbers of values then the PHP will assign the missing values to be NULL and u will have a warning message.

if u pass many values than the function expected, it will ignore the extra values!

Note: u can assign default value for ur variables, so in the case that u forget to pass some values, the function will user their defaults

function function_name($varibale1= “default value”, $variable2= “default value”)

{

block of statements

return;

}

4. Passing values by reference:

when u pass values into variables in the function definition, u pass them by value (by default), u can make as many changes as u want inside the function, but when u back from the function , the value will return to it’s previous value b4 calling the function bcz u send a copy from the variable. If u want ur changes to affect the variable pass that variable by reference (think about it as u pass a pointer) any changes inside the function will stay after exiting the function!

Passing by reference is used mainly when passing really large values bcz it’s much faster!

Example:

<?php
function welcome_by_name ($name= “Visitor”)
{
echo “Welcome “, $name;
return;
}

echo “Passing the name \”Noor\” to the welcome function<br> “;
welcome_by_name(“Noor”);
echo “<br> Using the default value while calling the function <br>”;
welcome_by_name();

?>

The output:

Passing the name “Noor” to the welcome function
WelcomeNoor
Using the default value while calling the function
Welcome Visitor

Creating reusable code(functions) September 24, 2008

Posted by Nsh15 in Basic PHP programming, php.
Tags: , , , ,
add a comment

A function is a group of PHP statements that perform a specific task, u can use the function whenever u need to perform the task.

Defining functions:

function function_name ()

{

block of statements

return;

}

The return statement stops the function and returns to the main script, it is not required but it makes the functions easier to understand.U can write a function anywhere in the script, but the usual is to put all functions together at the beginning of the script file.

Returning values back from functions:

U can pass value back from a function, using the return statement. it can only return one value. However remember that the return value can be an array; thus it is actually more than one value!

u can also use the return statement in a conditional statement to end the function.

Example:

<?php
function welcome()
{
echo “Welcome”;
return;
}
function name()
{
return Noor;
}
?>

<h3> Functions in PHP </h3>

<?php
$name_variable= name();
welcome();
echo “  $name_variable”;
?>
The output:

Functions in PHP

Welcome Noor

Using variables in functions (another method to return more than 1 value):

u can create as many variables as u wish inside the function, these variables r local. which means that they r accessable inside the function, also notice that the function can’t access any variable inside the function except in the case that this variable is set to be global!

by using global variables inside the function, u can return as many value as u wish ;)

Example1 ( Local variables ,,, notice how u don’t get the result u want ):

<?php
$x=3;
$y=2;

function addition()
{
$sum= $x + $y;
}
addition();

echo “The sum result =”, $sum;
?>

The output for the first example:

The sum result =

Example2:

<?php
function addition($x1,$y1)
{
global  $sum;
$sum= $x1 + $y1;
return;
}

$x=5;
$y= 2;

addition($x,$y);

echo “The sum result =”, $sum;
?>

The output for Example2:

The sum result =7

Follow

Get every new post delivered to your Inbox.