Heim > php教程 > php手册 > Hauptteil

PHP的基本常识(1)

WBOY
Freigeben: 2016-06-13 10:49:41
Original
974 Leute haben es durchsucht

这些是写给初级PHP程序员或者入门不久的同学的,老鸟可以飘过,欢迎补充和评论;接受合理意见与批评。
这些PHP的概念,有些刚开始比较难懂,很难理解,我把他们都列出来,希望能帮助一些人,在前进的路上少点荆棘。
1. variable variables(变量的变量)
variable_variables.php
 
  View Code
 
$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';
将hello 赋值给 变量 $a, 于是 $$a = ${hello} = $hello = 'hello everyone';
如果对于 $$students[1],  这样会产生混乱,php的解释器可能无法理解,‘[’ 虽然有较高运算符,但结果可能无法输出。
好的写法是:${$students[1]} = ‘Mary’;
2. array's function(数组函数)
 array_functions.php
  View Code
 
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);
 

 3. dates and times (时间和日期)
有3种方法可以创建一个unix time(从1970/1/1 到现在的秒数)
time();  返回当前的时间戳
mktime($hr, $min, $sec, $month, $day, $year); mktime(6,30,0,5,22,2012) 返回2012 5/22 6:30:00 的时间戳
strtotime($string); strtotime("+1 day") 返回明天这个时候的时间戳 更多 'last Monday' 'lasy Year'
----------------------------------------------------------
checkdate($month, $day, $year); 验证一个日期是否为真 checkdate(5,32,2012) ? 'true' : 'false'; // return false
 
得到了时间戳后,我们需要对它进行转化为可读的,如2012/5/22
我们有2种方法  date($format, $timestamp) ; strftime($format [,$timestamp])
推荐用第2种,strftime("%Y-%m-%d %H:%M:%S");  // return 2012-05-22 15:46:40

 
5. server  variables (服务器和执行环境信息)
$_SERVER
server_variables.php
  View Code
 

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

6.variable_scope(变量的作用域 global static)
static_variables.php
  View Code
 
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 '
';
 
test() 函数中的变量 $a 没有保存 $a++ 的结果 , 重复调用test() 并没有使 $a 的值增加
而test1() 函数中 变量 $a 申明了 staic $a = 0, 为静态变量。
引用:A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
一个静态变量 只能存在于本地的函数作用域内 也就是test1() 函数体内, 但是当程序离开这个test1() 作用域时,静态变量不会失去它的值,也就是 $a 变量会增加 1; 当重新调用 test1() 时,$a = 1;
global_variables.php
  View Code
 
$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;
 
引用:In PHP global variables must be declared global inside a function if they are going to be used in that function
如果这些变量将在函数中使用,全局变量必须在使用的那个函数中进行定义。 这样可以避免很多麻烦。

7.reference(引用)
variable_reference.php
  View Code
 

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

所以通过引用后, 我们改变$b的值,同时也改变了$a的值。
8. pass reference variable to function(传递引用参数给函数)
 

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

$a = 10;
ref_test($a);
echo $a;
 
当我们按引用传递参数给函数时,我们传递地不是变量的副本(copy) ,而是真实的值,
所以当我们调用函数ref_test($a)的时候已经改变了 $a 的值, 所以最后 $a = 20;
9. reference function return value(引用函数的返回值)
reference_function_return_value.php
 
function &increment(){
    static $var = 0;
    $var++;
    return $var;
}

$a =& increment(); // 1
increment(); // 2
$a++; //3
increment(); // 4
echo "a: {$a}";
 
 首先申明一个引用函数,在函数体内,申明一个静态变量 $var, 可以保存增加的值;
$a =& increment(); 这条语句是 变量$a 引用 函数increment() 的返回值,
和前面的引用变量一样, 你可以把increment()函数, 看作是一个变量; 这样就变为 $a = & $b;
所以increment() 和 $a 都指向同一个值,改变任何一个,都能改变同一个值。

 


摘自 warcraft

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!