Some of these PHP concepts are difficult to understand at first. I list them all, hoping to help some people and reduce thorns on the way forward.
1. variable variables (variables of variables)
variable_variables.php
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 variable $a, so $$a = ${hello} = $hello = 'hello everyone';
If for $$students[1], This will cause confusion, and the PHP interpreter may not be able to understand it. Although '[' has a higher operator, the result may not be output.
A good way to write it is: ${$students[1]} = ‘Mary’;
2. array's function
array_functions.php
shift & unshift
';// shifts first elemnt out of an array
// the index will 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 '
pop & push
';// push the element to the last of array
$d = array_push($numbers, 'last');
echo 'd: '.$d.'
';
print_r($numbers);
More array function references
3. dates and times (time and date)
There are 3 ways to create a unix time (number of seconds from 1970/1/1 to now)
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 the timestamp of this time tomorrow more 'last Monday' ' Lasy Year'
checkdate($month, $day, $year); Verify whether a date is true checkdate(5,32,2012) ? 'true' : 'false'; // return false
After getting the timestamp, we need to convert it into a readable one, such as 2012/5/22
We have 2 methods date($format, $timestamp); strftime($ format [,$timestamp])
It is recommended to use the second type, strftime("%Y-%m-%d %H:%M:%S"); // return 2012-05-22 15 :46:40
5. server variables (server and execution environment information)
$_SERVER
server_variables.php
6.variable_scope (scope of variable global static)
static_variables.php
test();
echo '
';
test();
echo '
';
test();
echo '
' ;
echo '
test1();
echo '
';
test1();
echo '
';
test1();
echo '
' ;
The variable $a in the test() function does not save the result of $a, and repeated calls to test() do not increase the value of $a
The variable $a in the test1() function declares staic $a = 0, which is a static variable.
Quote: 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 body. However, when the program leaves the test1() scope, the static variable will not lose its value, that is, the $a variable will Increase by 1; when test1() is called again, $a = 1;
global_variables.php
function Sum()
{
global $a, $b;
$b = $a $b;
}
Sum();
echo $b;
echo '
function Sum1()
{
$GLOBALS['b'] = $GLOBALS['a'] $GLOBALS['b'];
}
Sum1();
echo $b;
Quote: In PHP global variables must be declared global inside a function if they are going to be used in that function
If these variables will be used within a function, the global variables must be defined within that function. This can avoid a lot of trouble.
More details
7.reference(reference)
variable_reference.php
1.Fatal error: Using $this when not in object context
This mistake is definitely easy to happen when you first learn OOP, because there is a concept that you don’t really understand. The accessibility of a class can also be said to be scope. You can also think of a Chinese person abroad. He does not belong to any culture and does not speak foreign languages (maybe he knows a little bit); but he cannot pass himself. Communicate with foreigners because they were not born in the same country.
So how did the error occur? Look at the example below:
This mistake is very classic and very practical. Let’s look at the definition of static first:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
Translation: Defining the properties or methods of a class as static allows them to be accessed directly without initializing a class. A property defined as static cannot be accessed by objects of the class using the object operator * -> * (it can be accessed through static methods).
Example description:
Line 7 and Line 10 made the same mistake. The first one was to use the object operator to access static variables. If you look at the definition, $this is a pseudo variable equivalent to object, an instance. If you use the object operator -> to access it, an error will be reported.
Similarly, you cannot use the static operator :: to access a public variable. The correct access should be lines 14 and 25, one is accessed in the definition of the class (self:: === Trones::), and the other is accessed outside the class.
For inherited classes, the above rules also apply.
2.Fatal error: Call to private method
There is a very interesting TV series recently called Game of Thrones. We assume that there are three parties, seven kings, common people, and a dragon lady. The three of them competed below for the final victory, which was the crown.
The following story also has a title: Class Visibility (Visibility) If you know the final answer, you can skip the explanation part.
当static 碰到 private ,结合产生复杂,也产生美;就像抽象的人,像我们大学老师讲的数学课;(不过网易的公开数学课很好)
如果想要龙女 获得最后的胜利, 你只要帮她一把 将13行的 $this->getFire() 这部分去掉就可以了。同样的道理 你无法在一个静态函数里 使用任何对象操作符。
怎么使人民获得王冠呢? 你去奋斗吧!
如果你不构建大型的框架和网站 这些概念比如 Interface Implement abstract 。。。 你还是不知道的好。