Summary of basic PHP knowledge

Source: Internet
Author: User
These are written to junior PHP programmers or students who are not very familiar with PHP. The old bird may float. You are welcome to add and comment. You may accept reasonable comments and criticism.

These are written to junior PHP programmers or students who are not very familiar with PHP. The old bird may float. You are welcome to add and comment. You may 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

The Code is as follows:


$ 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

The Code is as follows:


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 );



More array Functions

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

More time and date references

5. server variables (server and execution environment information)

$ _ SERVER

Server_variables.php

The Code is as follows:



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'
';



More details

6. variable_scope (the scope of the variable is global static)

Static_variables.php

The Code is as follows:


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

The Code is as follows:


$ 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 functions, the website space and global variables must be defined in the function used. This can avoid a lot of trouble.

More details

7. reference)

Variable_reference.php

The Code is as follows:

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.