PHP Basic Grammar, PHP basic Grammar _php Tutorial

Source: Internet
Author: User
Tags array sort php operator terminates type null

PHP basic syntax in mind, PHP basic syntax


PHP Constants:

Pre-defined constants:

PHP itself also defines a number of predefined constants, which can be viewed using get_defined_constants (), and the more commonly used predefined constants are:

Php_os : PHP System.

php_version : PHP version.

Php_eol : newline characters (which are important for cross-platform porting), will use different values depending on the operating system. Different operating system line breaks are different. window: \ r \ n *nix: \ n MAC: \ r

Php_int_max : The maximum value for shaping.

php_int_size : The number of bytes that the shaping occupies.

Path_separator : The delimiter between a path and a path in an environment variable.

Directory_separator : directory delimiter. Under window ' \ ' and '/', *nix can only use '/'.

Magic constants:

Like constant, real very amount, grammatical similar constant, called Magic constant!

__file__: Full file path and file name. Typical applications (project code) such as:

Define (' Root_path ', str_replace (' 7.php ', ', __file__));

Echo Root_path;

Result equivalent (__dir__, the constant is added in PHP5.3)

__FUNCTION__: Get the magic constant of the current function name!

Conversion of PHP binaries

In PHP, a series of conversion functions are provided:

Hex:16, Dec:10, Oct:8, Bin:2.

such as: Decbin (), converts 10 binary number 2 binary number; Octhex () converts 8 binary to 16 binary.

For shaping, once the value is too large to overflow, a type conversion occurs, and a floating-point type is turned.

Unsigned integers are not supported in PHP.

Such as:

$a = Php_int_max; 2147483648

$b = $a +1;

Var_dump ($b); Float

In PHP, Float,double is actually the same. Floating-point numbers have a precision of 14-bit decimal digits, and the maximum value is relative to the platform, usually 1.8e308. The comparison of floating-point numbers is not reliable. When writing a program, do not attempt to obtain business logic by comparing the equality of two floating-point numbers.

How strings are defined

There are four ways to define strings: double quotes, single quotes, Heredoc (delimiters), nowdoc (delimiters)

Double quotes can parse variables, and single quotes cannot parse variables. Single quotes can contain double quotes, and double quotation marks can contain single quotes, but quotation marks cannot contain the quotation marks themselves.

Variables can be parsed, not depending on which one the variable is contained, but on whether the definition string is single or double quotes, and if it is a double quote, the variable is parsed, and if it is single quotation marks, it is not resolved.

In the string, if there are {$ together, it means that the {} is parsed as a variable.

Case where the variable is considered null

1. Assigned value is NULL

2. Has not been assigned a value

3. Being unset ()

One of the most common applications is to assign the value of an object to null to destroy the object.

Data type correlation function

var_dump () : Prints the details of the variable, including the type and value.

GetType () : gets the type.

Settype () : sets the type.

is series: where Is_array () is often used.

isset () : checks whether a variable exists (set).

empty (): checks if a variable is empty.

For Isset (), returns True whenever there is a claim (value), regardless of his value.

For empty (), it is equivalent to a Boolean (variable) and then reversed.

The case of converting to Boolean is considered false

1. A Boolean value of FALSE itself

2. Integer value 0 (0)

3. Floating-point value 0.0 (0)

4. Empty string, and the string "0" (Note that "00", "0.0" is considered true)

5. Empty array

6. Special type NULL (including variables not yet set)

All other values are considered TRUE (including objects and resources).

PHP Rules for Operations

The result of a division operation, which may be a floating-point number or an integer.

In the modulo operation, if there are decimals, the fractional part is removed.

In the modulo operation, the positive or negative of the result depends on the first number.

Original code

Converts decimal into binary. The symbol bit is represented by the highest position, 0 is a positive number, and 1 indicates a negative number.

Anti-code

For positive numbers, the inverse code is the same as the original code

For negative numbers, the sign bit is constant and the other bits are reversed.

Complement

For positive numbers, the complement is the same as the original code

For negative numbers, on the basis of the inverse code + 1.

The symbol does not change when the code is transcoded, the sign bit participates in the operation,

Shift Operations

Shift right: low overflow, sign bit invariant, high position with sign bit complement (equal to n power by 2, then rounding)

Shift left: High overflow, sign bit unchanged, low 0 (equal to n power of 2 times)

Whether it is moving left or right, it only changes the size of the number and does not change the symbol, so the symbol bit is always constant during the shift operation.

PHP operator Symbol Precedence

And OR and && | | The difference.

Same usage, different priority. &&, | | > = > and, or

Break: Terminates, when execution to break, the whole loop loop statement ends directly.

Continue: Continue, the current loop body finishes execution, and the next loop body continues.

Include and require

Set the value of the Include_path

Use the function Set_include_path ().

Set_include_path (' D:/php/test '), and then directly require ' file.php ',

Note: When set, the settings will overwrite the previous settings!

Gets the value of the current include_path

Use the function: Get_include_path () to get the value of the current Include_path!

A semicolon connection is used between the directories.

Set_include_path (' D:/php/test '). Path_separator.get_include_path ());

PHP to compile the code when the source file is a unit, the current file if there is a syntax error, PHP will error. Code compilation processing is not performed.

The difference between require (require_once) and include (include_once)

When loading a file fails, the dependency on the file is different, the triggering error is inconsistent! Different levels.

Require (require_once): Triggers a fatal error that causes the script to terminate;

Include (include_once): A warning error is triggered and the script continues to run.

The difference between require (include) and require_once (include_once):

Belt once refers to loading once. When the load is executed, the once will first determine whether the file is already loaded in the current file.

Already loaded: does not load again

No load: Execute load!

Use require as far as possible can be.

controlling script execution

Terminates script execution and delays script execution.

Die () , Exit (): terminates script execution, and once it appears, the script terminates immediately, ending all execution. And you can output a string before the end.

sleep () : delay script execution, pause for a period of time, in seconds. The maximum execution period is 30 seconds, can be configured, in the php.ini file max_execution_time =

If there are some default values for some of the parameters in the parameter list, but some do not exist, then the parameters with default values are placed behind the formal parameter list.

Func_get_args (): Gets all the arguments within the function.

$GLOBALS: Predefined variables

A pre-defined variable specifically for user data hyper-global.

Unlike other hyper-global variables:

Each global variable is automatically corresponding to an element within the $GLOBALS.

Adding a global variable automatically adds an element with the same name within the $globals! Vice versa!

$v 1 = 10;

Var_dump ($GLOBALS [' v1 ']);

$GLOBALS [' v2 '] = 20;

Var_dump ($GLOBALS [' V2 ']);

The role of Global is

Declares a local variable and initializes it to a reference to a global variable of the same name!

The role of anonymous functions

A typical anonymous function can be used as a temporary function. For example, there are intrinsic functions that need to call a function to complete the operation! Image: Array_map (): Return array = Array_map (' function ', array); Use the provided function for all elements within an array!

All parameters need to callback (callable) place, are passed through the anonymous function to complete!

PHP array pointer problem

Pointer functions

PHP has: the ability to get pointers to the keys and values of the array elements! Using functions:

Current () to get the value of the current element

key () : Gets the key of the current element. Returns null if the pointer is already illegal. To determine if an element exists.

There should also be: the ability to move the pointer!

Next (): You can complete the movement of the pointer!

Array functions:

Range () : You can get an array of elements in a range.

array_merge (' $arr 1 ', ' $arr 2 ',...) : Array merging, merging multiple.

What happens when the subscript is repeated?

Numeric index: full re-indexing!

character Poute: The value of the element that appears after it overrides the previous element value!

Array_rand ( Array, number) : randomly get the element from the array, get the subscript! If multiple, returns the collection of random subscripts! The result is sorted after, from small to large!

Shuffle (& $arr) : disrupts the order of elements within the array. Note that the parameter is passed as a reference! Will disrupt the original array.

Key-value operation:

Array_keys () : gets all the keys.

array_values () : gets all the values.

In_array () : whether a value exists.

array_key_exists () : whether a key exists.

array_combine (' key array ', ' array of values ') : combine two arrays into an array, one as the key and the other as the value!

Array_fill (' First index value ', ' quantity ', ' value ') : fills an array.

Array = Array_fill (starting subscript, number of elements filled, padding value);

Array_chunk () : split the fractional group, the principle is the number of elements in the sub-array!

ARray_intersect ($arr 1, $arr 2): computes the intersection of two arrays, finds the elements that exist in $ARR1, and also exists in $ARR2, where the data appears in the first parameter.

Array_diff ($arr 1, $arr 2) : calculates the difference set of two arrays. Find elements that exist in arr1 but do not exist in ARR2!

Array emulation stacks and queues:

Buyers and queues are typical data structures and are all a list of lists.

Into the stack: Array_push (), pressing the data into the array at the end of the array.

Out stack: Array_pop (), the output element at the top of the stack.

Array_push () and Array_pop () are re-indexed to ensure that all elements are incremented by 0.

Queue: Array_push (), which presses the data into the array at the end of the array.

Out: Array_shift (), at the top of the array, take the data out.

Array_unshift (), the data can be pressed into the array by the top of the array.

Array sort function

The sort function is a reference pass value!

R: reverse, invert. a: Association, Association. u: User, Custom.

sort (' array ') : The key value is not associated with the value, ascending, and not persisted.

rsort (' array ') : The key value is not associated with the value in descending order.

asort (' array ') : keeps key values associated by value, ascending order.

arsort (' array '): Keeps key values associated by value, descending order.

ksort (' array ') : Keep key values associated by key, ascending.

krsort (' array ') : Keep key values associated by key, descending.

natsort (' array ') : Natural number sorting, you can use the calculated natural number to sort the data!

usort (' array '): Custom sort, the size relationship between the user-defined elements. The user provides a function that compares the size of two elements and can tell the size relationship of the PHP element. The user-defined function is responsible for informing Usort () of the size relationship between two elements, while Usort gets the relationship and is responsible for completing the sort! Use the return value to tell!

Return effect in ascending order:

Returns a negative number that indicates the first element is small;

Returns a positive number indicating the first element is large;

Returns 0, representing equality.

http://www.bkjia.com/PHPjc/916827.html www.bkjia.com true http://www.bkjia.com/PHPjc/916827.html techarticle PHP Basic syntax in mind, PHP basic Syntax PHP constants: Predefined constants: PHP itself also defines a large number of predefined constants, you can use Get_defined_constants () to view, more commonly used pre ...

  • Related Article

    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.