Home >php教程 >php手册 >Summary of basic knowledge of PHP_php basics

Summary of basic knowledge of PHP_php basics

WBOY
WBOYOriginal
2016-05-16 09:00:072014browse

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

Copy code The code is 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 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

Copy code 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: '.$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 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

More time and date reference

5. server variables (server and execution environment information)

$_SERVER

server_variables.php

Copy code 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 (scope of variable global static)

static_variables.php

Copy code The code is 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 $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

Copy code 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;

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

Copy code The code is 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 this way. My mother calls me Mingming, but my boss calls me Xiaoyan; whether it is Mingming or Xiaoyan, it is me.
'&' And this is how different people call our aliases, that is, references, which is equivalent to $a = {me, or the value in the memory}, $b = {leader, mother, or variable}
Through &, $b points to the only and same value of $a in memory. So no matter what your boss calls you, or what your mother calls you, you are you. Just the name is different.

So after passing the reference, we change the value of $b and also change the value of $a.

8. pass reference variable to function (pass reference parameters to function)

Copy code The code is as follows:

function ref_test(&$var){
return $var *= 2;
}

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


When we pass parameters to a function by reference, we pass It is not a copy of the variable, but the real value,

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

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

reference_function_return_value.php

Copy code The 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, and in the function body, declare a static variable $var, which can save the increased value;

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

Same as the previous reference variable, you can regard the increment() function as a variable; this becomes $a = & $b;

So increment() and $a both point to the same value. Changing either one can change the same value.

More details

Object OOP

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:

Copy code The code is 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 you use 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 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.

Copy code The 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( ) ;


正确答案是:7国征战 内斗,平民死伤无数,龙女想乘机渔翁得利;可惜 最终谁也没有得到皇冠和胜利。哈哈。

当static 碰到 private ,结合产生复杂,也产生美;就像抽象的人,像我们大学老师讲的数学课;(不过网易的公开数学课很好)

如果想要龙女 获得最后的胜利, 你只要帮她一把 将13行的 $this->getFire() 这部分去掉就可以了。同样的道理 你无法在一个静态函数里 使用任何对象操作符。

怎么使人民获得王冠呢? 你去奋斗吧!

如果你不构建大型的框架和网站 这些概念比如 Interface Implement abstract 。。。 你还是不知道的好。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn