Php language summary, php Summary

Source: Internet
Author: User
Tags object serialization string back

Php language summary, php Summary

Php language summary: basic syntax type constant operator flow control function class and object namespace super global variable garbage collection mechanism


1. Basic syntax
* Php tag
<? Php
Echo "Hello world ";
//... More code
Echo "Last statement ";
// The script ends now and no PHP end mark is displayed.

Note: If the file content is pure PHP code, it is best to delete the PHP end mark at the end of the file.
This avoids the accidental addition of spaces or line breaks after the PHP end mark, which may cause PHP to output these spaces,
In the script, there is no output intention at this time.

* Separating from html
<? Php if ($ expression = true):?>
This will show if the expression is true.
<? Php else:?>
Otherwise this will show.
<? Php endif;?>

2 type
* Introduction
PHP supports eight types of raw data.

Four scalar types:
Boolean (boolean)
Integer)
Float (float, also called double)
String (string)

Two composite types:
Array)
Object)

There are two special types:
Resource)
NULL (no type)

Note: view the value and type of the expression. Use the var_dump () function to obtain the type. Use gettype () to determine whether it is an is_type () type ()

* Boolean
To specify a Boolean value, use the keyword TRUE or FALSE. Both are case insensitive.

When converted to boolean, the following values are considered FALSE:
Boolean value FALSE
Integer value 0 (0)
Floating point value: 0.0 (0)
Empty string and string "0"
Array that does not contain any elements
<? Php
Var_dump (bool) ""); // bool (false)
Var_dump (bool) 1); // bool (true)
Var_dump (bool)-2); // bool (true)
Var_dump (bool) "foo"); // bool (true)
Var_dump (bool) 2.3e5); // bool (true)
Var_dump (bool) array (12); // bool (true)
Var_dump (bool) array (); // bool (false)
Var_dump (bool) "false"); // bool (true)
?>

* Integer
The maximum value can be expressed by the constant PHP_INT_MAX.

* Float
* String
Single quotes: Do not parse the variables
Double quotation marks: parses the variables
(If there is no variable, we recommend that you use single quotes to improve efficiency, because you do not need to parse the content)
Complex (curly braces): simply write an expression like a string, and enclose it with curly braces {And }.
<? Php
// Valid. When multiple arrays are used in a string, enclose them with parentheses.
Echo "This works: {$ arr ['foo'] [3]}";
// Valid
Echo "This works:". $ arr ['foo'] [3];
?>

* Array

<? Php
// Create a simple array
$ Array = array (1, 2, 3, 4, 5 );
Print_r ($ array );
// Delete all elements, but keep the array unchanged:
Foreach ($ array as $ I =>$ value ){
Unset ($ array [$ I]);
}
Print_r ($ array );
// Delete the entire array unset ($ array)
?>
* Object
To create a new object, use the new statement to instantiate a class:

<? Php
Class foo
{
Function do_foo ()
{
Echo "Doing foo .";
}
}
$ Bar = new foo;
$ Bar-> do_foo ();
?>
* Resource
Like a database connection, a resource is returned.
* Null
The NULL type has only one value, that is, the case-insensitive constant NULL.
The variable is considered NULL in the following cases:
The value is NULL.
Not assigned.
Unset ().
Determine whether it is null. Use is_null ()

2 variables
* Basics
$ This is a special variable and cannot be assigned a value.
By default, values are always assigned and variables are referenced. This means that changing new variables will affect the original variables.
* Predefined Variables
* Variable range
In a user-defined function, a local function range is introduced. Any variable used in the function will be limited to the local function by default.
<? Php
$ A = 1;/* global scope */
Function Test ()
{
Echo $ a;/* reference to local scope variable */
}
Test ();
?>
Unlike C, in C, global variables automatically take effect in functions unless they are overwritten by local variables.
Global variables in PHP must be declared as global when used in functions.
<? Php
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B;


$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>

The state variable only exists in the local function domain, but its value is not lost when the execution of the program leaves this scope.
<? Php
Function test ()
{
Static $ a = 0;
Echo $;
$ A ++;
}
?>

* Variable
<? Php
$ A = 'hello ';
$ A = 'World ';
Echo "$ a $ {$ }";
// The output result is exactly the same as that of the following statement:
Echo "$ a $ hello ";
?>
* Variables outside php
$ _ POST $ _ GET

3 Constants
<? Php
// Valid constant name
Define ("FOO", "something ");
Define ("FOO2", "something else ");
Define ("FOO_BAR", "something more ");
// The constant name is invalid.
Define ("2FOO", "something ");
// The following definition is valid, but it should be avoided: (do not start with _ as a custom constant)
// Maybe one day, PHP will define a magic constant of _ FOO _.
// This will conflict with your code
Define ("_ FOO _", "something ");
?>

4 Operators
* Priority
<? Php
$ A = 3*3% 5; // (3*3) % 5 = 4
$ A = true? 0: true? 1: 2; // (true? 0: true )? 1: 2 = 2
$ A = 1;
$ B = 2;
$ A = $ B + = 3; // $ a = ($ B + = 3)-> $ a = 5, $ B = 5
// Mixing ++ and + produces undefined behavior
$ A = 1;
Echo ++ $ a + $ a ++; // may print 4 or 5
?>
* Error Operators
Supports an error control operator: @. Any error information that may be generated by this expression is ignored.
* Execution Operator
Note that this is not a single quotation mark! PHP will try to execute the content in the back quotes as a shell command and return its output information
The effect of using the unquoted operator "'" is the same as that of the shell_exec () function.
<? Php
$ Output = 'LS-al ';
Echo "<pre> $ output </pre> ";
?>
Result: The file directory structure is output.
* String concatenation operator
There are two string operators. The first is the concatenation operator ("."), which returns the string after the left and right parameters are connected.
The second is to connect the value assignment operator (". ="), which attaches the right parameter to the parameter after the left
<? Php
$ A = "Hello ";
$ B = $ a. "World! "; // Now $ B contains" Hello World! "
$ A = "Hello ";
$ A. = "World! "; // Now $ a contains" Hello World! "
?>
* Array Operators
$ A + $ B join $ a and $ B.
$ A = $ B is equal. If $ a and $ B have the same key/value pair, the value is TRUE.
$ A ===$ B. TRUE if $ a and $ B have the same key/value pairs and the order and type are the same.
$! = $ B: TRUE if $ a is not equal to $ B.
$ A <> $ B: TRUE if $ a is not equal to $ B.
$! = $ B incomplete. If $ a is not all equal to $ B, TRUE is returned.

+ The operator attaches the array element on the right to the back of the array on the left. If both arrays have the key names, only the elements in the array on the left are ignored.
<? Php
$ A = array ("a" => "apple", "B" => "banana ");
$ B = array ("a" => "pear", "B" => "strawberry", "c" => "cherry ");

$ C = $ a + $ B; // Union of $ a and $ B
Echo "Union of \ $ a and \ $ B: \ n ";
Var_dump ($ c );

$ C = $ B + $ a; // Union of $ B and $
Echo "Union of \ $ B and \ $ a: \ n ";
Var_dump ($ c );
?>

If the elements in the array have the same key name and value, the comparison is equal.
<? Php
$ A = array ("apple", "banana ");
$ B = array (1 => "banana", "0" => "apple ");

Var_dump ($ a = $ B); // bool (true)
Var_dump ($ a ===$ B); // bool (false)
?>
* Type operators
Instanceof is used to determine whether a PHP variable belongs to an instance of a certain class.
<? Php
Class MyClass
{
}

Class NotMyClass
{
}
$ A = new MyClass;


Var_dump ($ a instanceof MyClass );
Var_dump ($ a instanceof NotMyClass );
?>

V. Process Control
* If else
* Process Control replaces syntax
PHP provides alternative syntaxes for process control, including if, while, for, foreach, and switch.
The basic form of the alternative syntax is to replace left curly braces ({) with colons (:) and right curly braces (})
Endif;, endwhile;, endfor;, endforeach; and endswitch ;.

<? Php if ($ a = 5):?>
A is equal to 5
<? Php endif;?>

* While
While (expr ):
Statement
...
Endwhile;

*
<? Php
$ People = Array (
Array ('name' => 'kalle', 'salt' => 856412 ),
Array ('name' => 'Pierre ', 'salt' => 215863)
);

For ($ I = 0, $ size = sizeof ($ people); $ I <$ size; ++ $ I)
{
$ People [$ I] ['salt'] = rand (000000,999 999 );
}
?>

* Foreach
You can easily add & before $ value to modify the elements of the array.
This method will assign values by reference instead of copying a value.
<? Php
$ Arr = array (1, 2, 3, 4 );
Foreach ($ arr as & $ value ){
$ Value = $ value * 2;
}
// $ Arr is now array (2, 4, 6, 8)
Unset ($ value); // cancel the reference
?>
* Require and include
If a require error occurs, the script program is stopped and the include Operation generates a warning.
Require_once is basically the same as require. The only difference is that PHP checks whether the file is
Already included. If yes, it will not be included again.

Six Functions
* Variable Functions
A variable function is similar to a variable.
This means that if a variable name has parentheses, PHP will look for a function with the same name as the value of the variable and try to execute it.
Variable functions can be used for some purposes, including callback functions and function tables.
<? Php
Function foo (){
Echo "In foo () <br/> \ n ";
}

Function bar ($ arg = ''){
Echo "In bar (); argument was '$ arg'. <br/> \ n ";
}

// Use echo's packaging function
Function echoit ($ string)
{
Echo $ string;
}

$ Func = 'foo ';
$ Func (); // This callfoo ()

$ Func = 'bar ';
$ Func ('test'); // This callbar ()

$ Func = 'echoit ';
$ Func ('test'); // This callechoit ()
?>
* Anonymous Functions
<? Php
Echo preg_replace_callback ('~ -([A-z]) ~ ', Function ($ match ){
Return strtoupper ($ match [1]);
}, 'Hello-World ');
// Output helloWorld
?>

Class 7 and objects
* Basic Concepts
$ This is a reference to the caller object (usually the object that the method belongs)
* Attributes
In the member methods of the class, you can use the-> (Object operator): $ this-> property (where property is the property name) method to access non-static properties.
Static attributes are accessed using: (double colon): self: $ property. More static attributes and non-static attributes
* Constants
You can define a constant value in a class.
<? Php
Class MyClass
{
Const constant = 'constant value ';
}
?>
* Automatically load classes
* Static keywords
If the declared class property or method is static, you can directly access it without instantiating the class. Static attributes cannot be accessed through an object that has been instantiated by a Class (but static methods are acceptable)
Because static methods are loaded before compilation, they cannot access non-static methods because they do not exist.
* Abstract class
If at least one method in a class is declared as abstract, the class must be declared as abstract.
* Interface
Using interfaces, you can specify the methods that a class must implement, but you do not need to define the specific content of these methods.
* Traits
Trait is similar to a class, but it is only intended to combine functions in a fine-grained and consistent manner.
Trait cannot be instantiated by itself. It adds a combination of horizontal features for traditional inheritance;
That is to say, the members of the application class do not need to inherit.
<? Php
Trait ezcReflectionReturnInfo {
Function getReturnType () {/* 1 */}
Function getReturnDescription () {/* 2 */}
}

Class ezcReflectionMethod extends ReflectionMethod {
Use ezcReflectionReturnInfo;
/*...*/
}

Class ezcReflectionFunction extends ReflectionFunction {
Use ezcReflectionReturnInfo;
/*...*/
}
?>
* Traversing objects
* Final keywords
If the method in the parent class is declared as final, the subclass cannot overwrite the method. If a class is declared as final, it cannot be inherited.
* Object Replication
* Object comparison
When the comparison operator (=) is used to compare two object variables, the comparison principle is: if the attributes and attribute values of both objects are equal,
And the two objects are instances of the same class, so these two object variables are equal.


If the full equality operator (=) is used, these two object variables must point to the same instance of a class (that is, the same object ).
* Static binding later
* Objects and references
One key point is that "by default, objects are passed through references ". However, this is not completely correct. The following are examples.
* Object serialization
All values in php can be expressed using the serialize () function to return a string containing byte streams. The unserialize () function can re-convert the string back to the original value of php.
Serializing an object will save all the variables of the object, but will not save the object method, will only save the class name

8. namespace
* Namespace Overview
The user-written code conflicts with the names of PHP internal classes/functions/constants or third-party classes/functions/constants.
<? Php
Namespace my \ name; // refer to the "define namespace" section.

Class MyClass {}
Function myfunction (){}
Const MYCONST = 1;

$ A = new MyClass;
$ C = new \ my \ name \ MyClass; // refer to the "Global Space" section.

$ A = strlen ('Hi'); // refer to "use namespace: Reserve global functions/constants ".

$ D = namespace \ MYCONST; // refer to the section "namespace operator and _ NAMESPACE _ constant ".

$ D = _ NAMESPACE _. '\ MYCONST ';
Echo constant ($ d); // see the "namespaces and Dynamic Language Features" section.
?>

* Namespace keyword and _ NAMESPACE _ constant
<? Php
Namespace MyProject;
Echo '"', _ NAMESPACE __, '"'; // output "MyProject"
?>

* Global Space
If no namespace is defined, all classes and functions are defined in the global space,
It is the same as before PHP introduced the namespace concept. Add the prefix \ before the name to indicate that the name is
The name in the global space, even if it is in another namespace.
<? Php
Namespace A \ B \ C;

/* This function is A \ B \ C \ fopen */
Function fopen (){
/*...*/
$ F = \ fopen (...); // call the global fopen function
Return $ f;
}
?>

9. Global Variables
$ GLOBALS
$ _ SERVER
$ _ GET
$ _ POST
$ _ FILES
$ _ COOKIE
$ _ SESSION
$ _ REQUEST
$ _ ENV

Ten garbage collection mechanisms
* Reference Counter. Every php variable is saved in a variable container named "zval", except for the type and value of the variable,
It also includes two bytes of additional information. The first one is "is_ref", which is a bool value used to identify whether it belongs to the reference set.
The second additional byte is refcount used to count the number of references.
<? Php
$ A = "new string ";
Xdebug_debug_zval ('A ');
?>
Result: a: (refcount = 1, is_ref = 0) = 'new string'
* If a reference count is increased, it will continue to be used. If the reference count is reduced to 0, the variable container will be cleared (free)






Php programmer's one-year work summary

You still have to write this thing on your own. What have you done in a year, such as what sites you have made and what aspects you have done? How do you feel like you are doing, what is the difference between the first half of the year and the second half of the year,
If you only maintain the company's website, isn't it a website construction company? What problems have you solved in the past year, what new features have you developed, and how stable and fast your website is, what contribution has the website made? What is the difference between the website and the beginning of the year?
What have you learned within one year, what necessary knowledge you have mastered, how you have changed your attitude towards the company, and whether you have changed your company's development direction?
Finally, write down the plan for next year, including the work plan and study plan.

What is a function? I don't want to copy the article. I just want to hear it in plain words. I 'd better summarize it in my own language. What is the main function in PHP?

In plain words, it is absolutely original.
Simply put, a function is a piece of code, but this code can be repeatedly called by code elsewhere, this eliminates the need to copy and paste this code every time you use this code, which is more rational. This is the significance of structured program design.
The function mentioned here is different from the function mentioned in mathematics. It is similar in that it maps input to output, but not exactly the same. The main role of a function is those above.

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.