Php UDF usage

Source: Internet
Author: User

Custom functions

7.2.1 Basic function naming principles:

1. The function name cannot be the same as the existing function name.

2. The function name can only contain letters, numbers, and underscores.

3. The function name cannot start with a number.

 

7.2.2 basic usage: declare with function

The code is as follows: Copy code

<? Php

// Create a function

Function funcCountArea ($ radius)

{

Return $ radius * pi ();

}

 

// Use the function

$ Area = funcCountArea (20 );

Echo $ area;

Echo '<br/> ';

 

$ Area2 = funcCountArea (30 );

Echo $ area2;

?>
 

Output

1256.63706144
2827.43338823
 

 

7.2.3 passing parameters by value

The code is as follows: Copy code

<? Php

$ A = 5;

Function funcChange ($)

{

$ A = 2 * $;

}

FuncChange ($ );

Echo $;

?>
 

Output

5
 

 

7.2.4 passing parameters by reference

The code is as follows: Copy code

<? Php

$ A = 5;

Function funcChange (& $)

{

$ A = 2 * $;

}

FuncChange ($ );

Echo $;

?>
 

Output

10
 

 

7.2.5 function call that returns multiple values

The code is as follows: Copy code

<? Php

Function funcUserInfo ($ username, $ password)

{

$ UserInfo = array ($ username, $ password );

Return $ userInfo;

}

 

$ Arr = funcUserInfo ('anllin', '20140901 ');

 

Print_r ($ arr );

?>
 

Output

Array ([0] => anllin [1] => 123456)


 

 

7.2.6 another function call that returns multiple values (practical: recommended)

The code is as follows: Copy code

<? Php

Function funcUserInfo ($ username, $ password)

{

$ UserInfo [] = $ username;

$ UserInfo [] = $ password;

Return $ userInfo;

}

 

$ Arr [] = funcUserInfo ('Bob', '123 ');

$ Arr [] = funcUserInfo ('John', '123 ');

$ Arr [] = funcUserInfo ('Mark', '123 ');

Print_r ($ arr );

?>
 

Output

Array ([0] => Array ([0] => Bob [1] => 512655) [1] = & gt; Array ([0] = & gt; John [1] = & gt; 458736) [2] => Array ([0] => Mark [1] => 925472 ))
 

 

Note: function calls are case-insensitive, but variable names are case-sensitive.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.