Explanation on PHP custom functions and internal functions
1. Variable scope
is also called the scope of a variable. The scope of a variable is the context in which it is defined (also its effective scope).
Most PHP variables have only one single scope. This single scope The scope span also includes files introduced by include and require.
global keyword: You can use the global keyword inside the function to access global variables
You can also use $GLOBALS and other super-global arrays
For example:
$str = 'xxxx'; function test(){ //方法一: global $str; echo $str; //方法二 //echo $GLOBALS['str'] }
2. Static variables
Static variables only exist in the local function domain, but their values will not disappear when the program execution leaves this scope
static keyword
Only Initialize once
Need to assign a value during initialization
The value will be retained each time the function is executed
static modified variables are local and only valid within the function
The number of calls to the function can be recorded, so that it can be used in a certain Terminate recursion under these conditions
2.1, global variables, static variables
<?php /** * 写出如下程序的输出结果: * <?php * * $count = 5; * function get_count() * { * static $count; * return $count++; * } * echo $count; * ++$count; * * echo get_count(); * echo get_count(); * * ?> * */ $count = 5; function get_count() { static $count; return $count++; } echo $count;//5 ++$count; //这里还涉及到运算符:递减NULL值没有效果,但是递增NULL值为1 echo get_count();//null,第一次定义的static $count,内容为null,现返回内容null,再null++,结果为1 echo get_count();//1,static $count = 1,现返回1,再递增
2.2, function transfer
<?php $var1 = 5; $var2 = 10; function foo(&$my_var) { global $var1; $var1 += 2; $var2 = 4; $my_var += 3; return $var2; } $my_var = 5; echo foo($my_var). "\n";//4 echo $my_var. "\n";//8 echo $var1;//7 echo $var2;//10 $bar = 'foo'; $my_var = 10; echo $bar($my_var). "\n";//4
2.3, The reference of the function returns
从函数返回一个引用,必须在函数声明和指派返回值给一个变量都是用引用运算符& <?php function &myFunc() { static $b = 10; return $b; } echo myFunc();//10 $a = &myFunc();//此步a直接引用到b的地址 $a = 100;//修改a的值,相当于修改b的值 echo myFunc();//100 ,因为b是一个静态变量,该值会保留
3. Import of external files
If the path name is given, search according to the path, otherwise search from include_path
If there is no include_path, Then search from the directory where the calling script file is located and the current working directory
When a file is included, the code contained in it inherits the variable scope of the line where the include is located
If there is none of the above If found, the following error or warning will be reported
require and require_once: When it fails, a fatal level error will be generated and the program will stop running.
include and include_once: Only a warning level error is generated when it fails, and the program continues to run.
The only difference between the two is that when the included file code already exists, it is no longer included
4. System built-in functions
4.1, time and date function
date() //Format Timestamp
strtotime() //Parse the English text date and time into a Unix timestamp
mktime() //Integer Unix timestamp
time() //Get the current time timestamp
microtime () //Get milliseconds
date_default_timezone_set() //Set the default time zone
4.2, ip processing function
long2ip: Convert the long integer into a dotted Internet standard format address in string form ( IPV4)
ip2long: Convert the IPV4 string Internet protocol into a long integer number
4.3. Printing function
echo()
can output multiple values at one time, between multiple values Separate with commas. echo is a language construct, not a real function, so it cannot be used as part of an expression.
print(): The value of a simple type variable (such as int, string)
The function print() prints a value (its parameter) and returns true if the string is successfully displayed, otherwise it returns false.
print_r(): You can print out the value of complex type variables (such as arrays, objects)
You can simply print out strings and numbers, while arrays are in the form of an enclosed list of keys and values. Displayed and starts with Array. But the results of print_r() outputting Boolean values and NULL are meaningless, because they all print "\n". Therefore, using the var_dump() function is more suitable for debugging.
Print human-readable information about the variable. If a string, integer or float is given, the variable value itself will be printed. If an array is given, the keys and elements will be displayed in a certain format. object is similar to an array. Remember, print_r() will move the array pointer to the end. Use reset() to return the pointer to the beginning.
var_dump()
This function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values recursively, showing their structure through indentation.
Determine the type and length of a variable, and output the value of the variable. If the variable has a value, the value of the variable is output and the data type is returned. This function displays structural information about one or more expressions, including the expression's type and value. The array will recursively expand the values, showing its structure through indentation.
var_export(): Output or return a string representation of a variable
This function returns structural information about the variable passed to the function
You can pass the second The parameter is set to TRUE, thereby returning the representation of the variable. It returns valid PHP code.
var_dump和print_r的区别: var_dump返回表达式的类型与值而print_r仅返回结果,相比调试代码使用var_dump更便于阅读。 var_dump和var_export的区别: var_export() 函数返回关于传递给该函数的变量的结构信息,是合法的 PHP 代码,可以通过将函数的第二个参数设置为 TRUE,从而返回变量的表示 var_dump() 打印变量的相关信息 printf():根据格式进行输出 sprintf():根据格式转换字符串,并返回
4.4, serialize and deserialize unserialize
<?php $a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut'); //序列化数组 $s = serialize($a); echo $s; //输出结果:a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1:"c";s:7:"Coconut";} echo '<br /><br />'; //反序列化 $o = unserialize($s); print_r($o); //输出结果 Array ( [a] => Apple [b] => banana [c] => Coconut )
4.5, json_encode and json_decode
<?php $a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut'); //序列化数组 $s = json_encode($a); echo $s; //输出结果:{"a":"Apple","b":"banana","c":"Coconut"} echo '<br /><br />'; //反序列化 $o = json_decode($s); 在上面的例子中,json_encode输出长度比上个例子中serialize输出长度显然要短
4.6. String function
php Summary of string usage
4.7.Array function
php array operation
Related recommendations:
php custom function records log
PHP custom function determines which submission method
The built-in function in JS and How to use custom functions
The above is the detailed content of Explanation on PHP custom functions and internal functions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
