Công ty Thiết kế web Thiên Việt | Dịch vụ Thiết kế website | Web Design | Quảng bá web top 10 Google | Search Engine Optimition
 
   Trang Chủ  
  Giới Thiệu  

Kiểm tra Tên miền

  Liên Hệ  
  Quảng cáo Miễn Phí  
  Thư Giãn  
  Diễn đàn  
 
 
Liên Hệ Thiết Kế Website
 
     Hỏi đáp trực tuyến
 
 
 
PHP: Functions

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.

Contents provided by Thiet ke web Thiên Việt
    
Gởi trang này cho bạn bè Ghi nhớ trang này
Ý kiến mới nhất
 
 
 
Đóng góp ý kiến
 
 
Tên gọi
Website
Email
Nội dung
=
 
1 2 3 4 5 6 7 8 9 10
 
 
     
 
   
 
   
   
 
Goc trai tren   Goc phai tren
 
Mẩu Web Tham Khảo
Nut thiet ke web Web Flash
 

 

Goc trai duoi   Goc phai duoi
 
 
Liên Kết Website
 
 
 
 
 
 
 
ĐỐI TÁC - KHÁCH HÀNG

Phòng kinh doanh
YM Yahoo Support
Mobile icon Mr.Lộc 0919.22.10.30
Email icon Email phuocloc@vietweblink.net
Hỗ trợ kỹ thuật:
YM Yahoo Support
Mobile icon Mr.Tranh 0168.69.12.5.88
Email icon Email tranh@vietweblink.net
 
 
Triển khai dịch vụ thiết kế web flash cao cấp dành cho các khách hàng muốn sở hữu một website tuyệt đẹp bất kỳ ai cũng phải khen ngợi khi chiêm ngưỡng. Tham khảo các mẫu thiết kế tại đây

Chương trình khuyến mãi

  Uong cafe o dau
 
 
  Mẩu Web Flash Cao Cấp
 
   
 

  Hiện đang có 41 khách online
 
 
Khuyen mai KHÁCH HÀNG TIỂU BIỂU
The gioi dong ho Mia ca Quang Thong Cong ty CBL Viet Nam May dong goi an Phuc Thanh Toan Cuoc Phi Thiep cuoi nhu y Ao cuoi nhu muoi
 
     
  Site Help | Tuyển dụng | Liên hệ | Khách Hàng | Thiết Kế Website | Quản trị website | Quảng bá website | Hỏi đáp trực tuyến | Liên kết website Click here to switch to other Office Online sites worldwide
Công ty Thiết Kế Web © 2009 THIÊN VIỆT WEB-Design. All rights reserved.