> Online tutorial : function in php
Showing posts with label function in php. Show all posts
Showing posts with label function in php. Show all posts

argument passed by reference

By default arguments are passed by value(so that it will changed inside function only not outside the function).So we are going the value then send value by reference,prepend by (&) to the argument name in the function definition.
Syntax
function fun_name(&$varname)
Example

<?php
function my(&$num)
{
$num='welcome';
}
$str="welcome";
my($str);
echo $str;

?>


Output


passing arguments

Information may be passed to function via the argument list.which is a comma-delimited list of expression
Example

<?php
function my($num)
{
echo "$num";
}
my("india");
?>


Output

PHP Cookies

PHP Cookies
A cookie is often used to identify a user.
What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
How to Create a Cookie?
The setcookie() function is used to set a cookie.
Note:
          The setcookie() function must appear BEFORE the <html> tag. 
 Syntax
setcookie(name, value, expire, path, domain);
Example
In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour:

<?php
setcookie("user", "Alex Porter", time()+3600);
?>
Note:
The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).

PHP Functions with Return values

PHP Functions - Return values
Functions can also be used to return values.

Example


<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
<body>
</html>


The output of the code above will be:


1 + 16 = 17

php functions with multiple parameters

PHP Functions - Adding parameters
Our first function (writeMyName()) is a very simple function. It only writes a static string.
To add more functionality to a function, we can add parameters. A parameter is just like a variable. You may have noticed the parentheses after the function name, like:
writeMyName(). The parameters are specified inside the parentheses.
Example 1
The following example will write different first names, but the same last name:

<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.";
}
echo "My name is ";
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
<body>
</html>
The output of the code above will be:
My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.

Example 2

The following function has two parameters:

<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "
";
}
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Ståle","...");
?>
<body>
</html>

The output of the code above will be:

My name is Kai Jim Refsnes.
My name is Hege Refsnes!
My name is Ståle Refsnes...

function in php

PHP Functions
The real power of PHP comes from its functions.
In PHP - there are more than 700 built-in functions available.

PHP Functions:
In this tutorial we will show you how to create your own functions.
For a reference and examples of the built-in functions.
Creating PHP functions:
• All functions start with the word "function()"
• Name the function - It should be possible to understand what the function does by its
name. The name can start with a letter or underscore (not a number)
• Add a "{" - The function code starts after the opening curly brace
• Insert the function code
• Add a "}" - The function is finished by a closing curly brace

Use a PHP Function
Now we will use the function in a PHP script:

<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
echo "Hello world!";
echo "My name is ";
writeMyName();
echo ".That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>


The output of the code above will be:


Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name.

More about Function
Function with multiple parameter
Function with return value