PHP Basic Knowledge Summary _php Tutorial

Source: Internet
Author: User
Tags ming time and date
Some of these PHP concepts are just beginning to be difficult to understand, I have listed them, hoping to help some people, on the way forward less thorns.

1. Variable variables (variable variable)

variable_variables.php

Copy the Code code as follows:
$a = ' Hello ';
$hello = ' Hello everyone ';

echo $ $a. '
';

$b = ' John ';
$c = ' Mary ';
$e = ' Joe ';

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

If this is confusing for $ $students [1], PHP's interpreter may not understand, ' [' Although there is a higher operator, the result may not be output.

The good wording is: ${$students [1]} = ' Mary ';

2. Array ' s function (arrays functions)

array_functions.php

Copy the Code code 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 would reset
$a = Array_shift ($numbers);

Echo ' A: '. $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 the array
$d = Array_push ($numbers, ' last ');
Echo ' d: '. $d. '
';

Print_r ($numbers);


More Group Function Reference

3. Dates and times (time and date)

There are 3 ways to create a Unix time (the number of seconds from 1970/1/1 to the present)

Time (); Returns the current timestamp

Mktime ($hr, $min, $sec, $month, $day, $year); Mktime (6,30,0,5,22,2012) returns the timestamp of 2012 5/22 6:30:00

Strtotime ($string); Strtotime ("+1 Day") returns tomorrow at this time of the timestamp more ' last Monday ' Lasy year '

Checkdate ($month, $day, $year); Verify that a date is true checkdate (5,32,2012)? ' True ': ' false '; return False

After getting the timestamp, we need to translate it into readable, such as 2012/5/22

We have 2 ways to date ($format, $timestamp); Strftime ($format [, $timestamp])

Recommended to use the 2nd type, strftime ("%y-%m-%d%h:%m:%s"); return 2012-05-22 15:46:40

More Time Date Reference

5. Server variables (servers and execution environment information)

$_server

server_variables.php

copy code code as follows:
!--? php
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 detailed information

6.variable_scope (Scope of the variable global static)

static_variables.php
Copy the Code code as follows:
function test ()
{
$a = 0;
echo $a;
$a + +;
}

Test ();
Echo '
';
Test ();
Echo '
';
Test ();
Echo '
';

Echo '

';
function Test1 ()
{
static $a = 0;
echo $a;
$a + +;
}

Test1 ();
Echo '
';
Test1 ();
Echo '
';
Test1 ();
Echo '
';

The variable in the test () function $a did not save the result of $a + + and repeated calls to test () did not increase the value of the $a

The variable $a in the Test1 () function declares Staic $a = 0 as a static variable.

Reference: A static variable exists only with a local function scope, but it does not lose its value when program execution leaves t His scope.

A static variable can only exist within the local function scope, which is the test1 () function body, but when the program leaves the Test1 () scope, the static variable does not lose its value, that is, the $a variable will increase by 1, and when the Test1 () is recalled, $a = 1;

global_variables.php

Copy the Code code 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 is declared global inside a function if they is going to being used in this function

If these variables are to be used in a function, the global variable must be defined in the function that is used. This can avoid a lot of trouble.

More detailed information

7.reference (citation)

variable_reference.php
Copy the Code code as follows:
$a = ' arist ';
$b = $a;
$b = ' Ming ';
echo "My name is:{$a}. But my mother call me {$b}.
";

Echo '

';

$a = ' arist ';
$b = & $a;
$b = ' Ming ';

echo "My name is:{$a}. And my mother call me {$b}.
";


This concept can be understood, my mother calls me clearly, but my leader will call me small words, whether it is Ming or small words, it is me.
' & ' And this is the way that different people call our aliases, which is a reference, equivalent to $a = {I, or the memory value}, $b = {leader, mom, or variable}
By &, $b points to $ A and the only value that is the same in memory. So no matter what your leader calls you, or what your mother calls you, you are you. It's just a different name.

So by referencing, we change the value of $b and also change the value of $ A.

8. Pass reference variable to function (passing reference parameters to functions)

Copy CodeThe code is as follows:
Function Ref_test (& $var) {
return $var *= 2;
}

$a = 10;
Ref_test ($a);
echo $a;


When we pass arguments to a function by reference, we pass the ground not a copy of the variable, but the actual value,

So when we call the function ref_test ($a) has changed the value of the $a, so the last $a = 20;

9. Reference function return value (reference function return values)

reference_function_return_value.php

Copy CodeThe code is as follows:
function &increment () {
static $var = 0;
$var + +;
return $var;
}

$a =& increment (); 1
Increment (); 2
$a + +; 3
Increment (); 4
echo "A: {$a}";


First, declare a reference function, in the function body, declare a static variable $var, you can save the added value;

$a =& increment (); This statement is the return value of the variable $ A reference function increment ().

As with the previous reference variable, you can think of the increment () function as a variable, so that it becomes $a = & $b;

So increment () and $a all point to the same value, and any change can change the same value.

More detailed information

Object OOP

1.Fatal error:using $this When not in object context

This error just learns that OOP must be easy to come by because there is a concept you don't really understand. The accessibility of the class (accessible), or the scope, you can also think of is 1 Chinese abroad, he does not belong to which culture, he does not speak a foreign language (perhaps he knows point); but he cannot communicate with foreigners by himself because they are not born in a common country.
So how did the mistake happen? Look at the following example:

Copy the Code code as follows:
Class trones{
static public $fire = "I am fire.";
Public $water = "I am Water";

static function Getfire () {
return $this->fire; Wrong
}
static function Getwater () {
Return $self:: water; Wrong
}

static function Fire () {
Return self:: $fire; Be sure your use of self to access the static property before you invoke the function
}
}

/*
Fatal error:using $this When not in object context
*/
Echo Trones::getfire ();
Echo Trones::getwater ();

Correct
Echo Trones::fire ();
echo "
" ;
$trones = new Trones;
$trones->fire; notice:undefined property:trones:: $fire (Base on Defferent error setting) Simple is error
echo trones:: $fire;

This error is very classic, but also very practical, first look at the definition of static:

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can is accessed with an instantiated class object (though a static method can).

Translation: When defining a class's properties or methods as static, they can be accessed directly without having to initialize a class. A property that is defined in order to be static cannot be accessed by a class object using the object operator (which can be accessed by a static method).

Example Description:
7 rows and 10 rows make the same error, the first is to use the object operator to access the static variable. You look at the definition, $this is a pseudo-variable equivalent to an object, an instance. You will get an error if you access it using the object operator.

Also you cannot use static operators:: To access a public variable. The correct access should be 14 rows and 25 rows, one in the definition of the class (self:: = = = Trones::), and one is accessed outside the class.

For inheriting classes, the above rules are also appropriate.

2.Fatal Error:call to Private method


Recently there is a TV series very good-looking, called the game of rights, we assume that there are 3 square horses, 7 kings, civilians, Dragon Girl. The three of them competed for the final victory, the crown.

The following story also has a title: Class Visibility (visibility) If you know the final answer, you can skip the explanation section.

Copy CodeThe code is as follows:
Class Trones {
Protected $fire = "Fire";
Public $water = "Water";
static Private $trones = "Trones";

protected function Getfire () {
$this->fire;
}

static public Function Thedragenofmather () {
return __method__. "Use". $this->getfire (). "Gets the". Self::gettrones ();
}

static public Function Getwater () {
return __method__;
}

Static Private Function Gettrones () {
Return self:: $trones;
}

}

Class Kings extends Trones {
static function thesevenking () {
return __method__. " Gets the ". Self::gettrones ();
}
}

class people extends trones{
static function ThePeople () {
return __method__. " Gets the ". Self::gettrones ();
}
}
Echo kings::thesevenking ();
Echo Trones::thedragenofmather ();
Echo People::thepeople ();


The correct answer is: 7 countries fighting inside, the civilian casualties countless, dragon woman want to fly Yuwengdeli, but ultimately no one get crown and victory. Ha ha.

When static encounters private, the combination produces complexity, and it also produces beauty, like an abstract person, like a math class taught by our college teachers (but NetEase's open math class is good)

If you want the Dragon girl to win the final victory, you just have to help her. The 13-row $this->getfire () is removed. In the same way, you cannot use any object operators in a static function.

How can the people get the crown? You go to fight it!

If you do not build large frameworks and websites these concepts such as Interface Implement abstract ... Well, you still don't know.

http://www.bkjia.com/PHPjc/328069.html www.bkjia.com true http://www.bkjia.com/PHPjc/328069.html techarticle Some of these PHP concepts are just beginning to be difficult to understand, I have listed them, hoping to help some people, on the way forward less thorns. 1. Variable variables (variable ...

  • 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.