Results 1 to 4 of 4

Thread: Functions in PHP

  1. #1
    Join Date
    Aug 2008
    Posts
    30

    Functions in PHP

    A function is a block of PHP code generally designed to be reused several times. Rather than write X time the piece of code, it puts in a function and this function is known when they will have decided. Can be used for functions such as display a well-defined text (we'll see below) or repetitive calculations based on input parameters (eg calculate conversion degrees C ° => degrees fahrenheit You provide the value of degrees Celsius and function returns the value. In this case, the parameter is the inlet temperature in degrees C °). Here is the basic syntax for PHP to say "this is a function, it should be treated as such:

    Code:
    <?php 
    function mafonction () 
    { 
    
    } 
    ?>
    It notes that the first word is the word function, it is mandatory and means that the block will be handled by PHP function.

    A function always parentheses.

    There are two main types of functions: functions that return a value, and those who do not return (we call these procedures).

    Example procedure displaying "Hello World!" :

    Code:
    <?php 
    function hello_world () 
    { 
    echo 'Hello everybody  !' ; 
    } 
    ?>
    To see Hello World! Anywhere in your code, you will only have to type this:
    Code:
    <? PHP 
    hello_world (); / / calls the function "hello_world" which will display "Hello World!" 
    ?>
    Warning! for example that this work requires that the code of your function is placed in the same PHP file (or so we have to do what is called a PHP include file containing the function in the PHP file where you use

  2. #2
    Join Date
    Aug 2008
    Posts
    30

    Re: Functions in PHP

    The parameters

    We have just seen an example of a procedure which did not contain any parameter. Parameters are variables that are placed between parentheses. These parameters used data on which the function. Generally, the parameters imply that it will return something (we will be very likely to have functions and procedures). We see later that the move by reference parameters (do not worry if you do not understand yet what I say, you will understand the bottom of this page) can usually escape the return of a value.

    Example of a function calculating the product of two numbers and returning the result:

    Code:
    <? php 
    function product ($ number1, $ number2) 
    {
    return number1 $ * $ number2;
    }
    ? >
    Here it will return the product of two numbers. Note that using the keyword return to return a value. You can also use the keyword return to stop the execution of the function (by returning a value). Everything after return will not be executed.

    If you want to display the product of 10 and 2 for example, you can do this:

    Code:
    <? 
    php echo product (10, 2);
    ? >
    We can assign values to default settings. Why assign default values? it's simple, assign a value to a default setting, you can do without the passage of setting when you call the function (attention, this is not general). Here's an example:

    Code:
    <? php
    function product (number1 = $ 10, $ number2 = 2)
           (
    return $ number1 * $ number2;
           )
    >
    Here, we assign as defaults numbers 10 and 2. If we now call our function like this:

    Code:
    <? php
    Product echo ();
    >
    We will get the same result as above, because both settings are not mandatory here. As against attention, if you contact a setting, it will be a priority, which means that if you call the function like this:

    Code:
    ]
    <? php
    echo product (5, 2);
    >
    Here, we multiply 5 by 2 and 10 by 2. Give a default settings should be when you want not to have to add parameters when you tell PHP "performs this function me." Attention with the following example:

    Code:
    <? php
    echo product (5);
    >
    There is only one parameter! then where will it replace? Well this is the first parameter. In fact, when you leave all the settings, PHP will replace the parameters in order.

  3. #3
    Join Date
    Aug 2008
    Posts
    30

    Re: Passing parameters by reference

    The passing parameters by reference is not complicated either. Let's take a simple example and then explain how it works:


    Code:
    <? php
    ajouter_cinq function ($ number)
           (
    + $ number = 5; / / equivalent of $ number = $ number + 5
    return $ number;
           )
    
    $ mon_entier = 15;
    ajouter_cinq echo ($ mon_entier); / / display 20
    
    echo $ mon_entier / / display 15
    >
    Here we do what is called a parameter passing by recopy. Do not take it too the head with these words, it is a little general In fact it means that the variable that we will move into the function ($ mon_entier here) will be copied in a another variable called $ number (which will take the same value). The function then uses this variable $ number for his treatment, not the $ mon_entier. That is why when displaying the contents of the variable $ mon_entier after calling the function, we get always 15.

    Now with the version passed by reference:

    Code:
    <? php
    ajouter_cinq function ($ number)
           (
    + $ number = 5; / / equivalent of $ number = $ number + 5
    return $ number;
           )
    
    $ mon_entier = 15;
    echo ajouter_cinq (& $ mon_entier); / / display 20
    
    echo $ mon_entier / / display 20
    >
    The advantage of this type of operation is that you work directly on the variable Originally, there was no recopy and therefore performance can be improved. You also have more need to return a value. Consider this example which is exactly the same as the previous one:

    Code:
    	
    <? php
    ajouter_cinq function ($ number)
           (
    + $ number = 5; / / equivalent of $ number = $ number + 5
           )
    
    $ mon_entier = 15;
    ajouter_cinq (& $ mon_entier);
    
    echo $ mon_entier / / display 20
    >

  4. #4
    marshtric Guest

    Re: Functions in PHP

    thanks for the
    example code

Similar Threads

  1. trim functions of VB
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 31-12-2009, 11:51 AM
  2. SQL Scalar Functions
    By Bhardwaj in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 12:38 PM
  3. How to call a C functions within PHP?
    By Rixwel in forum Software Development
    Replies: 3
    Last Post: 05-09-2009, 09:12 AM
  4. PHP fsockopen Functions
    By Japesh in forum Software Development
    Replies: 3
    Last Post: 22-05-2009, 06:02 PM
  5. Array Functions in PHP
    By SuperXCM in forum Software Development
    Replies: 2
    Last Post: 26-03-2009, 10:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,709,310.00645 seconds with 17 queries