Chapter 7 php UDF implementation code _ php entry _ script house

Source: Internet
Author: User
The purpose of using udfs is to avoid repeated code.

The purpose of using udfs is to avoid repeated code.

7. 1. Standard Functions
There are more than 1000 standard functions in the standard php release package. These standard functions are built in the system and can be directly used without user creation.
For example:
The Code is as follows:
Echo md5 ('200 ');
Echo'
';
Echo sha1 ('20140901 ');
Echo'
';
Echo pi ();
?>

Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
. 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:
// Create a function
Function funcCountArea ($ radius)
{
Return $ radius * pi ();
}
// Use the Function
$ Area = funcCountArea (20 );
Echo $ area;
Echo'
';
$ Area2 = funcCountArea (30 );
Echo $ area2;
?>

Output
1256.63706144
2827.43338823
7.2.3 passing parameters by value
The Code is as follows:
$ A = 5;
Function funcChange ($)
{
$ A = 2 * $;
}
FuncChange ($ );
Echo $;
?>

Output
5
7.2.4 passing parameters by reference
The Code is as follows:
$ A = 5;
Function funcChange (& $)
{
$ A = 2 * $;
}
FuncChange ($ );
Echo $;
?>

Output
10
7.2.5 function call that returns multiple values
The Code is as follows:
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:
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.
7.2.7 understanding scope:
Local variables:
Variable declared in the function.
Global variables:
Variables declared outside the function.
7.2.8 convert local variables to global variables
The Code is as follows:
$ A = 5;
Function funcChangeValue ()
{
Global $;
$ A = 10;
}
FuncChangeValue ();
Echo $;
?>

Output
10
7.2.9 use of super global variable $ GLOBALR
The Code is as follows:
$ GLOBALS ['a'] = 5;
Function funcChangeValue ()
{
$ GLOBALS ['a'] = 10;
}
FuncChangeValue ();
Echo $ GLOBALS ['a'];
?>

Output
10
7. 3. File Inclusion
7.3.1 use of Include, can contain the same file multiple times
The Code is as follows:
Include 'demo1. php ';
Include 'demo1. php ';
Include 'demo1. php ';
?>

Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.2 using include_once is no different from using include, but multiple calls only contain the same file once.
The Code is as follows:
Include_once 'demo1. php ';
Include_once 'demo1. php ';
Include_once 'demo1. php ';
?>

Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 the require () statement contains and runs the specified file.
The Code is as follows:
Require 'demo1. php ';
Require 'demo1. php ';
Require 'demo1. php ';
?>

Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.4 the require_once () statement contains and runs the specified file during script execution, but does not contain the same file repeatedly.
The Code is as follows:
Require_once 'demo1. php ';
Require_once 'demo1. php ';
Require_once 'demo1. php ';
?>

Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359 s
7.3.5 differences between include and require
If there is other code behind Include, when an error occurs in calling include, the subsequent code will continue to be executed, but require will not.
When calling a non-existent file, Include will give a warning, but will continue to execute the subsequent code.
The Code is as follows:
Include 'demo11.php ';
Echo ('this is demo13.php ');
?>

Output
Warning: include (demo111.php) [function. include]: failed to open stream: No such file or directory in D: \ AppServ \ www \ Basic7 \ demo13.php on line 2
Warning: include () [function. include]: Failed opening 'demo111. php 'for declaration (include_path = '.; c: \ php5 \ pear ') in D: \ AppServ \ www \ Basic7 \ demo13.php on line 2
This is demo13.php
When Require calls a non-existent file, it will give an error and abort code execution.
The Code is as follows:
Require 'demo111. php ';
Echo ('this is demo14.php ');
?>

Output
Warning: require (demo111.php) [function. require]: failed to open stream: No such file or directory in D: \ AppServ \ www \ Basic7 \ demo14.php on line 2
Fatal error: require () [function. require]: Failed opening required' demo111. php '(include_path = '.; c: \ php5 \ pear ') in D: \ AppServ \ www \ Basic7 \ demo14.php on line 2
7. 4. Magic Constants
The so-called magic constant is not a real constant, but a variable that gets a fixed value based on the occasion.
The Code is as follows:
Echo _ FILE __;
Echo'
';
Echo _ LINE __;
Echo'
';
Function funcTest ()
{
Echo _ FUNCTION __;
}
FuncTest ();
?>

Output
D: \ AppServ \ www \ Basic7 \ demo15.php
5
FuncTest

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.