若天明 发表于 2015-2-4 00:00:13

PHP编程:为您详解PHP开辟东西的利用与剖析

培训的第二阶段,开始了PHP语言语法结构和应用的学习。     有一段工夫一向利诱于PHP中援用的传递,后来查手册及C源法式,并重复测试,大致对援用传递在内存中的形式有了必定的懂得,后来为了加深印象,写了个总结,应当不会有大的成绩――固然这是在PHP4中,在今后的版本中能够会有变更。事先写总结的时分,想熬炼一下英语,因而就对付了一篇。不外自己英语欠好,也懒得翻译,归正事先想本人看得懂就好了。明天血汗来潮,俄然感觉还蛮有效的,因而在这里现丑了,请人人斧正。那位看得懂的协助翻译一下吧,我没空了。
<P><?php
/*

filename: SetGetTest.php

comment on assignment by value and referrence
assuming $var is a varialbe, its handle(pointer) is named as *var,
and its content is named as @var

the memory area of @var is referred by *var, if the *var is the same,   
then the memory areas are the same, so *var is just like a pointer.

1. when $var = $var1
@var copied from @var1, but in the different memory area,   
new *var assigned by the system, pointing to the memory area holding @var
*var and *var1 are different

2. when $var =& $var1,
*var assigned by *var1, and the @var is not assigned or copied,   
it is absolutely the same as @var1, and in the same memory area
both *var and *var1 pointing to the memory area, that means they are the same.

passing by referrence
3.   
function set1(&$s){
$var =& $s;
}
set1($var1) results:
*var1 passing to the function, and *s is the same as *var1,
then *var is the same as *s, the result is that *var is the same as *var1
and all the contents are the same obviously.

4.
function set2(&$s){
$var = $s;
}
set2($var1) results:
*var1 passing to the function, and *s is the same as *var1,
but when $var = $s executes, from 1. we can see @var is the same as @s,   
but *var is different from *s, so @var and @s is not in the same memory area,
while @s and @var1 is sharing the same memory area, also *var1 and *s are the same.

5.
normal function return:
function get(){ return $var1; }
assuming the result is referred by a variable $result.
then @result is copied from @var1 but *result is not the same as *var1
when $var = get();
first you get a variable $result, as I said above, @result is the same as @var1, but *result
is different from *var1, and next $var = $result executes.   
As I said in 1., you can find, @var is the same as @result and the same as @var1,   
but *var is different from *result AND *var1;

while $var =& get() just means:
*var is the same as *result, so @var and @result are in the same memory area,   
but they are still different from those of $var1,   
both the memory area of @var1 and *var1,




6.
returning by referrence
function &get(){ return $var1; }
there are two ways to get the result

$var = get(); and $var =& get(); now I will tell the difference
I. $var = get();
the *result is the same as *var1 and so @result and @var1 are the same.
and then $var = $result executes,   
*var is not the same as *result, and also different from *var1,   
but their contents are the same.

I. $var =& get();
the *result is the same as *var1 and so @result and @var1 are the same.
and then $var =& $result executes,   
this means $var and $result are the same, both of @ and *

*/

// the test is the following

function println($s = ""){
print "$s<br>\n";
}

class GetSetTest
{
var $var = null;

function setByRef(&$arg){
$this->var =& $arg;
}

function passByRef(&$arg){
$this->var = $arg;
}

function setByVal($arg){
$this->var = $arg;
}

function &getByRef(){
return $this->var;
}

function getByVal(){
return $this->var;
}
}

$o = new GetSetTest;

println("============ setByRef getByRef =============");
println("-----------------Before change----------------");
$in = "before change";
$o->setByRef($in);
$outByVal = $o->getByRef();
$outByRef =& $o->getByRef();
println("\$in: ".$in);
println("\$outByVal: ".$outByVal);
println("\$outByRef: ".$outByRef);
println("\$this->var: ".$o->var);
println("-----------------After change-----------------");
$in = "after change";
println("\$in: ".$in);
println("\$outByVal: ".$outByVal);
println("\$outByRef: ".$outByRef);
println("\$this->var: ".$o->var);
println();

println("============ setByRef getByVal =============");
println("-----------------Before change----------------");
$in = "before change";
$o->setByRef($in);
$outByVal = $o->getByVal();
$outByRef =& $o->getByVal();
println("\$in: ".$in);
println("\$outByVal: ".$outByVal);
println("\$outByRef: ".$outByRef);
println("\$this->var: ".$o->var);
println("-----------------After change-----------------");
$in = "after change";
println("\$in: ".$in);
println("\$outByVal: ".$outByVal);
println("\$outByRef: ".$outByRef);
println("\$this->var: ".$o->var);
println();

println("============ passByRef getByVal =============");
println("-----------------Before change----------------");
$in = "before change";
$o->passByRef($in);
$outByVal = $o->getByVal();
$outByRef =& $o->getByVal();
println("\$in: ".$in);
println("\$outByVal: ".$outByVal);
println("\$outByRef: ".$outByRef);
println("\$this->var: ".$o->var);
println("-----------------After change-----------------");
$in = "after change";
println("\$in: ".$in);
println("\$outByVal: ".$outByVal);
println("\$outByRef: ".$outByRef);
println("\$this->var: ".$o->var);
println();

/*
以下输入了局是我(夜猫子)私自编纂添加的,次要是为后来人检查便利加在这里,越肉代办,向longnetpro道歉
输入了局:
============ setByRef getByRef =============
-----------------Before change----------------
$in: before change
$outByVal: before change
$outByRef: before change
$this->var: before change
-----------------After change-----------------
$in: after change
$outByVal: before change
$outByRef: after change
$this->var: after change

============ setByRef getByVal =============
-----------------Before change----------------
$in: before change
$outByVal: before change
$outByRef: before change
$this->var: before change
-----------------After change-----------------
$in: after change
$outByVal: before change
$outByRef: before change
$this->var: after change

============ passByRef getByVal =============
-----------------Before change----------------
$in: before change
$outByVal: before change
$outByRef: before change
$this->var: before change
-----------------After change-----------------
$in: after change
$outByVal: before change
$outByRef: before change
$this->var: after change
*/

?>
我先把我自己学习PHP的过程做一下概括:

海妖 发表于 2015-2-4 07:59:53

说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。

飘飘悠悠 发表于 2015-2-9 19:28:59

为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。

活着的死人 发表于 2015-2-21 12:49:04

首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。

透明 发表于 2015-3-6 20:14:58

建议加几个专业的phper的群,当然啦需要说话的人多,一处一点问题能有人回答你的,当然啦要让人回答你的问题,平时就得躲在里面聊天,大家混熟啦,愿意回答你问题的人自然就多啦。

不帅 发表于 2015-3-13 08:07:14

本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。

兰色精灵 发表于 2015-3-13 08:07:36

使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的

山那边是海 发表于 2015-3-15 00:31:43

首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。

不帅 发表于 2015-3-16 21:45:44

使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。

愤怒的大鸟 发表于 2015-3-19 17:20:01

多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。

分手快乐 发表于 2015-3-24 09:36:16

我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:

只想知道 发表于 2015-3-25 08:52:41

本文当是我的笔记啦,遇到的问题随时填充

简单生活 发表于 2015-3-26 19:09:15

建议加几个专业的phper的群,当然啦需要说话的人多,一处一点问题能有人回答你的,当然啦要让人回答你的问题,平时就得躲在里面聊天,大家混熟啦,愿意回答你问题的人自然就多啦。

深爱那片海 发表于 2015-3-29 19:44:43

写的比较杂,因为我也是个新手,不当至于大家多多指正。

仓酷云 发表于 2015-4-4 04:42:04

不禁又想起那些说php是草根语言的人,为什么认得差距这么大呢。

谁可相欹 发表于 2015-4-18 14:29:26

写的比较杂,因为我也是个新手,不当至于大家多多指正。

小女巫 发表于 2015-4-22 13:20:01

实践是检验自己会不会的真理。

蒙在股里 发表于 2015-4-23 03:39:44

写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。

若天明 发表于 2015-4-26 11:32:03

真正的方向了,如果将来要去开发团队,你一定要学好smarty ,phplib这样的模板引擎,

飘灵儿 发表于 2015-5-12 14:05:23

至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。
页: [1] 2
查看完整版本: PHP编程:为您详解PHP开辟东西的利用与剖析