Hot File

Function in PHP

View: 534    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Forum PHPBB, VBB   Fields: Other

10 point/2 review File has been tested

A function is a block of code which can be called from any point in a script after it has been declared. It is basically a compartmentalized PHP script designed to accomplish a single task. Furthermore, code contained within functions is ignored until the function is called from another part in the script.

Introduction

A function is a block of code which can be called from any point in a script after it has been declared. It is basically a compartmentalized PHP script designed to accomplish a single task. Furthermore, code contained within functions is ignored until the function is called from another part in the script. Functions are useful because they contribute to rapid, reliable, error-reducing coding, and increase legibility by tiding up complicated code sequences.

It is good programming practice to use functions to modularize your code and to better provide reuse. To declare a function, you simply type

<?php 
   function function_name(param_1, ... , param_n) 
   { 
      statement_1; 
      statement_2; 
      ...
      statement_m; 

      return return_value; 
   } 
?>

We can optionally pass parameters to the functions to be known as local variable, and we can also return a result with the "return value" statement. This produces the ending of the function returning a value.

Creating a simple function

Let's create two functions that will print the string "PHP Functions" five times, but the first one will not contain parameters, and the second one will. A function parameter is nothing more than a piece of data that the function requires to execute. In above example also included code to call the function.

<?php 
function firstFunction() 
{
    for($i = 0; $i != 5; $i++)
        echo "<P>PHP Functions!</P>";
 }

//let's add parameters to that function
function secondFunction($num, $msg) 
{
    for($i = 0; $i != $num; $i++)
        echo "<P>". $msg ."</P>";
}

echo "This is before the functions is called<br>";

echo "The first function output is:"
  firstFuction(5,"This is a function with parameters");

echo "The second function output is:";
  secondFuction(5,"This is a function with parameters");
  
echo "This is after the function has been called<br>";
?>

Next example creates a function that will calculate arithmetic mean and return a result with the "return value" statement:

<?php 
    
function aritmetic_mean($a, $b) 
{ 
   $result = ( $a + $b ) / 2; 
   return $result; 
} 

//print the the results of calculation    
echo aritmetic_mean(4,6),"<br>"; 
echo aritmetica_mean(3242,524543),"<br>";     
?>

Variable Scope and Lifetime :

It's important to note that if you define a variable within a function, that variable is only available within that function; it cannot be referenced in another function or in the main body of your program code.  This is known as a variable's scope.  The scope of a variable defined within a function is  local to that function.

If a function needs to use a variable that is defined in the main body of the program, it must reference it using the "global" keyword, like this:

<?php 
function AddingNumbers ( )
{
   global $sum = 2 + 2
}

$sum = 0
Addnumbers ( )
echo "2 + 2 = ".$sum
?>

While the scope of a variable defined in a function is local to that function, a variable defined in the main body of code has a global scope.  The "global" keyword tells PHP to look for a variable that has been defined outside the function.

And what about lifetime for a variable?  A variable defined within a PHP program script exists only while that script is running.  When the script ends, the variable ceases to exist.  This seems pretty obvious!  Now apply it to a function:  a variable defined within a function exists only while that function is being processed; when the function ends, the variable ceases to exist.

Function in PHP

Function in PHP Posted on 16-12-2015  A function is a block of code which can be called from any point in a script after it has been declared. It is basically a compartmentalized PHP script designed to accomplish a single task. Furthermore, code contained within functions is ignored until the function is called from another part in the script. 5/10 534

Comment:

To comment you must be logged in members.

Files with category

  • Input Validation Using Filter Functions with php

    View: 395    Download: 0   Comment: 0   Author: none  

    Input Validation Using Filter Functions with php

    Category: Forum PHPBB, VBB
    Fields: Other

    2.25/2 review
    Filter functions in PHP might not be sexy, but they can improve the stability, security, and even maintainability of your code if you learn how to use them correctly.

  • The Open/Closed Principle in PHP

    View: 272    Download: 0   Comment: 0   Author: none  

    The Open/Closed Principle in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    0/2 review
    As usual, a good way to grasp what’s actually behind the curtain of the principle and how to take advantage of its benefits is by example. In this article I’ll be setting up a few approachable ones in an attempt to unveil the principle’s driving...

  • Dependency Injection with Pimple in PHP

    View: 306    Download: 0   Comment: 0   Author: none  

    Dependency Injection with Pimple in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    1.6666666666667/3 review
    In application development, we try to create independent modules so that we can reuse code in future projects. But, it’s difficult to create completely independent modules which provide useful functionality; their dependencies can cause maintenance...

  • Working with Directories in PHP

    View: 443    Download: 0   Comment: 0   Author: none  

    Working with Directories in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    2.25/2 review
    Let's start with simple listing the contents of a directory. We need three functions to perform this task: opendir(), readdir() and closedir(). The opendir() function takes one parameter, which is the directory we want to read, and returns a...

  • Guide to Email (Text / HTML / Attachments) in PHP

    View: 419    Download: 0   Comment: 0   Author: none  

    Guide to Email (Text / HTML / Attachments) in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    0/2 review
    At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters.

  • Dynamic Image Generation in PHP

    View: 361    Download: 0   Comment: 0   Author: none  

    Dynamic Image Generation in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    0/1 review
    At first, have a look at the following script that creates captcha image. Next, we will consider each step of image generation in details.

  • Form Processing with PHP

    View: 640    Download: 0   Comment: 0   Author: none  

    Form Processing with PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    2.6666666666667/3 review
    Before you can process the information, you need to create an HTML form that will send information to your PHP script. There are two methods for sending data: POST and GET. These two types of sending information are defined in your HTML form...

  • Function in PHP

    View: 534    Download: 0   Comment: 0   Author: none  

    Function in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    2.5/2 review
    A function is a block of code which can be called from any point in a script after it has been declared. It is basically a compartmentalized PHP script designed to accomplish a single task. Furthermore, code contained within functions is ignored...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd