PHP Custom Function and internal function inspection Point ___ function

Source: Internet
Author: User
Tags integer numbers time and date
Scope of a variable
Also known as the scope of a variable, the scope of a variable is the context in which it is defined (also its effective range),
Most PHP variables have only a single range, and this single range span also contains files introduced by include and require

Global keyword: a function can be accessed globally by using global keywords

You can also use $globals and other hyper-global arrays

Like what:
$str = ' xxxx ';

function Test () {
Method One:
Global $str;
Echo $str;

Method Two
echo $GLOBALS [' str ']
}

Second, static variables
Static variables exist only in local function fields, but their values do not disappear when the program executes out of this scope

Static keyword
Initialize only once
Need to assign value when initializing
The value is preserved each time the function is executed
The static-decorated variable is local and is valid only within the function
You can record the number of calls to a function so that you can terminate recursion under certain conditions

2.1, global variables, static variables
<?php

/**
 * writes out the output of the following program:
 * <?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;

The operator is also involved: the decrement null value has no effect, but the increment null value is 1
echo get_count (),//null, the first defined static $count, the content is null, now returns the content null, then null++, The result is 1
echo get_count ()//1,static $count = 1, now returns 1, then increments

2.2. Function transfer
<?php


$var 1 = 5;
$var 2 = ten;


function foo (& $my _var)
{
    global $var 1;
    $var 1 = 2;
    $var 2 = 4;
    $my _var + 3;
    return $var 2;
}


$my _var = 5;
echo foo ($my _var). "\ n";//4
Echo $my _var. "\ n";//8
echo $var 1;//7
echo $var 2;//10
$bar = ' foo ';
$my _var = ten;
Echo $bar ($my _var). "\ n";//4

2.3, the function of the reference to return
Returns a reference from a function that must be declared and assigned a return value to a variable with a reference operator &
<?php

function &myfunc ()
{
    static $b =;
    return $b;
}

Echo MyFunc ();//10

$a = &myfunc ();//This step a directly refers to the address of B

$a = 100;//modifies the value of a, equivalent to modifying the value of B

echo MyFunc ();//100, Because B is a static variable, the value retains the

Iii. Import of external files
If the path name is given according to the path, find it from the Include_path
If it is not in include_path, look for it from the directory where the calling script file resides and the current working directory

When a file is included, the code it contains inherits the variable range of the row containing the include

If none of the above is found, then the following error or warning
Require and require_once: A fatal level error occurs when a failure occurs and stops the program from running.
Include and include_once: only one warning level error is generated when the program continues to run.

The only difference is that when the contained file code already exists, it does not contain

Four, System built-in function
4.1, time and Date function
Date ()//format timestamp
Strtotime ()//To resolve the English text date time to a Unix timestamp
Mktime ()//integer Unix time stamp
Time ()//Get current timestamp
Microtime ()//Get milliseconds
Date_default_timezone_set ()//Set the default time zone

4.2, IP processing functions
LONG2IP: An Internet standard format address (IPV4) that converts a long integer to a dotted form in a string
Ip2long: Convert IPV4 string Internet Protocol to grow integer numbers

4.3. Printing function
Echo ()
You can output multiple values at one time and multiple values separated by commas. Echo is a language structure (language construct), not a real function, and therefore cannot be used as part of an expression.

Print (): A value for a simple type variable (such as int,string)
The function print () prints a value (its arguments), returns True if the string is displayed successfully, or returns false.

Print_r (): Can print out the values of complex type variables (such as arrays, objects)
You can simply print the strings and numbers, and the array will be displayed in the enclosed key and a worthwhile list, beginning with an array. However, the results of Print_r () output Boolean and null are meaningless because they are printed "\ n". Therefore, the Var_dump () function is more suitable for debugging.
Print easy to understand information about a variable, and if you give a string, integer, or float, print the value of the variable itself. If you give an array, the keys and elements will be displayed in a certain format. object is similar to an array. Remember, Print_r () moves the pointer to the last edge of the array. Use Reset () to get the pointer back to the beginning.

Var_dump ()
This function displays information about the structure of one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indenting it.
Determine the type and length of a variable, and output the value of the variable, if the variable has a value of the variable and return the data type. This function displays information about the structure of one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indenting it.

Var_export (): Output or return a string representation of a variable
This function returns the structure information about the variable passed to the function

You can return the representation of a variable by setting the second argument of the function to TRUE. Is the PHP code that returns the representation that is valid.
The difference between Var_dump and Print_r:
var_dump Returns the type and value of an expression and Print_r returns only results, which is easier to read than debugging code using Var_dump.

the difference between Var_dump and Var_export: the
var_export () function returns the structural information about the variable passed to the function, which is a valid PHP code that returns the representation of a variable by setting the second argument of the function to TRUE
Var_dump () Print the information about the variable

printf (): Output according to format
sprintf (): Converts a string according to format and returns

4.4. Serialization of serialize and deserialization unserialize
<?php   
$a = array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' coconut ');   

Serialized array   
$s = serialize ($a);  
echo $s;  
Output result: a:3:{s:1: "a"; S:5: "Apple"; s:1: "B"; S:6: "Banana"; s:1: "C"; s:7: "Coconut";   
Echo ' <br/><br/> ';  
  
Deserialization  
$o = unserialize ($s);  
Print_r ($o);  
Output result Array ([a] => Apple [b] => Banana [C] => Coconut)  

4.5, Json_encode and Json_decode
<?php 
$a = array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' coconut ');
Serialized array
$s = json_encode ($a);
echo $s;
Output result: {"a": "Apple", "B": "Banana", "C": "Coconut"}
echo ' <br/><br/> ';

Deserialization
$o = Json_decode ($s);

In the above example, the Json_encode output length is obviously shorter than the serialize output length in the previous example

4.6. String function
PHP String Usage Summary


4.7. Array function
PHP Array Operations


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.