php

位置:IT落伍者 >> php >> 浏览文章

PHP的基本常识小结


发布日期:2018年10月13日
 
PHP的基本常识小结

这些PHP的概念有些刚开始比较难懂很难理解我把他们都列出来希望能帮助一些人在前进的路上少点荆棘

variable variables(变量的变量)

variable_variablesphp

复制代码 代码如下:
<?php
$a = hello;
$hello = hello everyone;

echo $$a<br />;

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

$students = array(bce);

echo ${$students[]};
/*
foreach($students as $seat){
echo $$seat<br />;
}
$$var[]
${$var[]} for #
*/

$a = hello;

将hello 赋值给 变量 $a 于是 $$a = ${hello} = $hello = hello everyone;

如果对于 $$students[] 这样会产生混乱php的解释器可能无法理解‘[ 虽然有较高运算符但结果可能无法输出

好的写法是${$students[]} = ‘Mary;

arrays function(数组函数)

array_functionsphp

复制代码 代码如下:
<?php
echo <p>shift & unshift </p>;
$numbers = array();
print_r($numbers);
echo <br />;

// shifts first elemnt out of an array
// the index will reset
$a = array_shift($numbers);

echo a: $a<br />;
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 <br />b: $b<br />;
print_r($numbers);

echo <hr />;
echo <p>pop & push </p>;
// pop the last element out of array
$c = array_pop($numbers);
print_r($numbers);
echo <br />;

// push the element to the last of array
$d = array_push($numbers last);
echo d: $d<br />;

print_r($numbers);

更多数组函数参考

dates and times (时间和日期)

种方法可以创建一个unix time(从// 到现在的秒数)

time(); 返回当前的时间戳

mktime($hr $min $sec $month $day $year); mktime() 返回 / 的时间戳

strtotime($string); strtotime("+ day") 返回明天这个时候的时间戳 更多 last Monday lasy Year

checkdate($month $day $year); 验证一个日期是否为真 checkdate() ? true : false; // return false

得到了时间戳后我们需要对它进行转化为可读的//

我们有种方法 date($format $timestamp) ; strftime($format [$timestamp])

推荐用第strftime("%Y%m%d %H:%M:%S"); // return ::

更多时间日期参考

server variables (服务器和执行环境信息)

$_SERVER

server_variablesphp

复制代码 代码如下:
<?php

echo SERVER details:<br />;
echo SERVER_NAME: $_SERVER[SERVER_NAME]<br />;
echo SERVER_ADD: $_SERVER[SERVER_ADDR]<br />;
echo SERVER_PORT: $_SERVER[SERVER_PORT]<br />;
echo DOCUMENT_ROOT: $_SERVER[DOCUMENT_ROOT]<br />;
echo <br />;

echo Page details:<br />;
echo REMOTE_ADDR: $_SERVER[REMOTE_ADDR]<br />;
echo REMORT_PORT: $_SERVER[REMOTE_PORT]<br />;
echo REQUEST_URI: $_SERVER[REQUEST_URI]<br />;
echo QUERY_STRING: $_SERVER[QUERY_STRING]<br />;
echo REQUEST_METHOD: $_SERVER[REQUEST_METHOD]<br />;
echo REQUEST_TIME: $_SERVER[REQUEST_TIME]<br />;
echo HTTP_USER_AGENT: $_SERVER[HTTP_USER_AGENT]<br />;
echo <br />;

更多详细信息

variable_scope(变量的作用域 global static)

static_variablesphp

复制代码 代码如下:
<?php
function test()
{
$a = ;
echo $a;
$a++;
}

test();
echo <br />;
test();
echo <br />;
test();
echo <br />;

echo <hr />;
function test()
{
static $a = ;
echo $a;
$a++;
}

test();
echo <br />;
test();
echo <br />;
test();
echo <br />;

test() 函数中的变量 $a 没有保存 $a++ 的结果 重复调用test() 并没有使 $a 的值增加

而test() 函数中 变量 $a 申明了 staic $a = 为静态变量

引用A static variable exists only in a local function scope but it  does not lose its value when program execution leaves this scope

一个静态变量 只能存在于本地的函数作用域内 也就是test() 函数体内 但是当程序离开这个test() 作用域时静态变量不会失去它的值也就是 $a 变量会增加 当重新调用 test() 时$a = ;

global_variablesphp

复制代码 代码如下:
<?php
$a = ;
$b = ;

function Sum()
{
global $a $b;

$b = $a + $b;
}

Sum();
echo $b;
echo <hr />;
$a = ;
$b = ;

function Sum()
{
$GLOBALS[b] = $GLOBALS[a] + $GLOBALS[b];
}

Sum();
echo $b;

引用In PHP global variables must be declared global inside a function if they are going to be used in that function

如果这些变量将在函数中使用全局变量必须在使用的那个函数中进行定义 这样可以避免很多麻烦

更多详细信息

reference(引用)

variable_referencephp

复制代码 代码如下:
<?php
$a = arist;
$b = $a;
$b = ming;
echo "My name is:{$a} But my mother call me {$b}<br />";

echo <hr />;

$a = arist;
$b = &$a;
$b = ming;

echo "My name is:{$a} And my mother call me {$b}<br />";



这个概念可以这样理解我妈叫我明明但是我的领导会叫我小言不管是明明或者是小言都是我
& 而这个就是不同的人叫我们的别名的方法 即引用相当于 $a = {我或者内存中的值} $b = {领导妈妈或者变量}
通过 & $b指向了$a 在内存中唯一也是相同的值 所以不管你领导叫你什么或者你妈叫你什么你都是你只是称呼不同

所以通过引用后 我们改变$b的值同时也改变了$a的值

pass reference variable to function(传递引用参数给函数)

复制代码 代码如下:
<?php
function ref_test(&$var){
return $var *= ;
}

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



当我们按引用传递参数给函数时我们传递地不是变量的副本(copy) 而是真实的值

所以当我们调用函数ref_test($a)的时候已经改变了 $a 的值 所以最后 $a = ;

reference function return value(引用函数的返回值)

reference_function_return_valuephp

复制代码 代码如下:
<?php
function &increment(){
static $var = ;
$var++;
return $var;
}

$a =& increment(); //
increment(); //
$a++; //
increment(); //
echo "a: {$a}";



首先申明一个引用函数在函数体内申明一个静态变量 $var 可以保存增加的值

$a =& increment(); 这条语句是 变量$a 引用 函数increment() 的返回值

和前面的引用变量一样 你可以把increment()函数 看作是一个变量 这样就变为 $a = & $b;

所以increment() 和 $a 都指向同一个值改变任何一个都能改变同一个值

更多详细信息

对象 OOP

Fatal error: Using $this when not in object context

这个错误刚学 OOP 肯定容易出现因为有个概念你没有真正理解 类的可访问性(accessible)也可以说是作用域 你还可以认为是  个 中国人 在国外他不属于哪个文化他不讲外语(可能他知道点)但是他无法通过自己跟老外沟通因为他们不是在一个共同国度出生
那么错误是如何发生的呢?看下面的例子

复制代码 代码如下:
<?php
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 "<br />" ;
$trones = new Trones ;
$trones>fire ; // Notice: Undefined property: Trones::$fire (base on defferent error setting) simple is error
echo Trones::$fire ;

这个错误很经典 也很实用先看 static 的定义

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)

翻译定义一个类的属性或方法为静态时可以使他们在不需要初始化一个类时就能直接访问 一个被定义为了静态的属性不能被类的对象用对象操作符访问* > *(可以通过静态的方法访问)

例子说明
行 犯了同一个错误第一个是用对象操作符来访问静态变量你看看定义$this 是一个伪变量 相当于 object一个实例你用对象操作符 > 访问就会报错

同样你也不能用 静态操作符 :: 来访问一个公共变量 正确的访问应该是 一个是在类的定义里访问(self:: === Trones::)一个是在类的外部访问

对于继承类以上的规则同样适合

Fatal error: Call to private method


最近有部连续剧很好看叫权利的游戏我们假设有 方人马 个国王 平民 龙女 他们三方人马在下面争夺最终的胜利 也就是王冠

下面的故事还有一个标题类的可见性(visibility) 你如果知道最终的答案解释部分你可以略过了

复制代码 代码如下:
<?php
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( ) ;



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

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

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

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

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

               

上一篇:javascript中直接写php代码的方法

下一篇:php中如何同时使用session和cookie来保存用户登录信息