Basic knowledge of PHP (1 ). These are written to junior PHP programmers or those who are not very familiar with PHP. the old bird may float. You are welcome to add and comment, and accept reasonable comments and criticisms. Some of these PHP concepts are written to junior PHP programmers or students who are not very familiar with them at the beginning. laruence can move over. You are welcome to add and comment, and accept reasonable comments and criticism.
Some of these PHP concepts are difficult to understand at the beginning. I listed them all in the hope that they could help some people with less thorns on the way forward.
1. variable variables (variable)
Variable_variables.php
View Code
$ A = 'hello ';
$ Hello = 'Hello everone ';
Echo $ .'
';
$ B = 'John ';
$ C = 'Mary ';
$ E = 'job ';
$ Students = array ('B', 'c', 'E ');
Echo $ {$ students [1]};
/*
Foreach ($ students as $ seat ){
Echo $ seat .'
';
}
$ Var [1]
$ {$ Var [1]} for #1
*/
$ A = 'hello ';
Assign hello to the variable $ a, so $ a =$ {hello} = $ hello = 'Hello everone ';
For $ students [1], this can cause confusion. the interpreter of php may not be able to understand it. although '[' has a high operator, the result may not be output.
Good syntax: $ {$ students [1]} = 'Mary ';
2. array's function (array function)
Array_functions.php
View Code
Echo'
Shift & unshift
';
$ Numbers = array (1, 2, 3, 4, 5, 6 );
Print_r ($ numbers );
Echo'
';
// Shifts first elemnt out of an array
// The index will reset
$ A = array_shift ($ numbers );
Echo 'A: '. $ .'
';
Print_r ($ numbers );
// Push element to the front of array
// Returns the count of array and reset array index
$ B = array_unshift ($ numbers, 'first ');
Echo'
B: '. $ B .'
';
Print_r ($ numbers );
Echo'
';
Echo'
Pop & push
';
// Pop the last element out of array
$ C = array_pop ($ numbers );
Print_r ($ numbers );
Echo'
';
// Push the element to the last of array
$ D = array_push ($ numbers, 'last ');
Echo 'd: '. $ d .'
';
Print_r ($ numbers );
3. dates and times (time and date)
There are three ways to create a unix time (from to the current number of seconds)
Time (); returns the current timestamp
Mktime ($ hr, $ min, $ sec, $ month, $ day, $ year); mktime (2012, 5/22,) returns the 6:30:00 timestamp.
Strtotime ($ string); strtotime ("+ 1 day") returns the timestamp of tomorrow at this time. more 'last Monday' lasy year'
----------------------------------------------------------
Checkdate ($ month, $ day, $ year); verify whether a date is true checkdate (2012 )? 'True': 'false'; // return false
After obtaining the timestamp, we need to convert it to readable, such
We have two methods: date ($ format, $ timestamp); strftime ($ format [, $ timestamp])
2nd recommended types, strftime ("% Y-% m-% d % H: % M: % S"); // return 15:46:40
5. server variables (server and execution environment information)
$ _ SERVER
Server_variables.php
View Code
Echo 'server details:
';
Echo 'server _ NAME: '. $ _ SERVER ['server _ name'].'
';
Echo 'server _ ADD: '. $ _ SERVER ['server _ ADDR'].'
';
Echo 'server _ PORT: '. $ _ SERVER ['server _ port'].'
';
Echo 'document _ ROOT: '. $ _ SERVER ['document _ root'].'
';
Echo'
';
Echo 'Page details:
';
Echo 'remote _ ADDR: '. $ _ SERVER ['remote _ ADDR'].'
';
Echo 'remort _ PORT: '. $ _ SERVER ['remote _ port'].'
';
Echo 'request _ URI: '. $ _ SERVER ['request _ URI'].'
';
Echo 'query _ STRING: '. $ _ SERVER ['query _ string'].'
';
Echo 'request _ METHOD: '. $ _ SERVER ['request _ method'].'
';
Echo 'request _ TIME: '. $ _ SERVER ['request _ time'].'
';
Echo 'http _ USER_AGENT: '. $ _ SERVER ['http _ USER_AGENT'].'
';
Echo'
';
6. variable_scope (the scope of the variable is global static)
Static_variables.php
View Code
Function test ()
{
$ A = 0;
Echo $;
$ A ++;
}
Test ();
Echo'
';
Test ();
Echo'
';
Test ();
Echo'
';
Echo'
';
Function test1 ()
{
Static $ a = 0;
Echo $;
$ A ++;
}
Test1 ();
Echo'
';
Test1 ();
Echo'
';
Test1 ();
Echo'
';
The variable $ a in the test () function does not save the result of $ a ++. the repeated call of test () does not increase the value of $.
The variable $ a in the test1 () function declares staic $ a = 0, which is a static variable.
Reference: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
A static variable can only exist in the local function scope, that is, the test1 () function, but when the program leaves the test1 () scope, the static variable will not lose its value, that is, the $ a variable increases by 1. when test1 () is called again, $ a = 1;
Global_variables.php
View Code
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
Echo'
';
$ A = 1;
$ B = 2;
Function Sum1 ()
{
$ GLOBALS ['B'] = $ GLOBALS ['A'] + $ GLOBALS ['B'];
}
Sum1 ();
Echo $ B;
Reference: In PHP global variables must be declared global inside a function if they are going to be used in that function
If these variables are used in the function, the global variables must be defined in the function in use. This can avoid a lot of trouble.
7. reference)
Variable_reference.php
View Code
$ A = 'arist ';
$ B = $;
$ B = 'Ming ';
Echo "My name is: {$ a}. But my mother call me {$ B }.
";
Echo'
';
$ A = 'arist ';
$ B = & $;
$ B = 'Ming ';
Echo "My name is: {$ a}. And my mother call me {$ B }.
";
This concept can be understood in this way. My mom calls me Mingming, but my leaders call me Xiaoyan. whether it is Mingming or Xiaoyan, it is me.
'&' And this is a reference of the alias method called by different people, which is equivalent to $ a = {me, or a value in memory}, $ B = {lead, mom, or variable}
Through &, $ B points to the unique and identical value of $ a in the memory. So no matter what your leader calls you or what your mom calls you, you are all you. The name is different.
Therefore, after referencing, we change the value of $ B and the value of $.
8. pass reference variable to function (pass the reference parameter to the function)
Function ref_test (& $ var ){
Return $ var * = 2;
}
$ A = 10;
Ref_test ($ );
Echo $;
When we pass a parameter to a function by reference, we do not pass a copy of the variable, but a real value,
So when we call the function ref_test ($ a), the value of $ a has been changed, so the last $ a = 20;
9. reference function return value (reference function return value)
Reference_function_return_value.php
Function & increment (){
Static $ var = 0;
$ Var ++;
Return $ var;
}
$ A = & increment (); // 1
Increment (); // 2
$ A ++; // 3
Increment (); // 4
Echo "a: {$ }";
First, declare a reference function. in the function body, declare a static variable $ var to save the added value;
$ A = & increment (); this statement is the return value of the variable $ a referencing the function increment,
Like the referenced variable, you can regard the increment () function as a variable, which becomes $ a = & $ B;
Therefore, both increment () and $ a point to the same value. changing any one can change the same value.
From warcraft
Bytes. Some of these PHP concepts have just been compared...