首页 > 开发 > PHP > 正文

PHP的基本常识小结

2024-05-04 22:24:47
字体:
来源:转载
供稿:网友

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

1. variable variables(变量的变量)

variable_variables.php

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

echo $$a.'<br />';

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

$students = array('b','c','e');

echo ${$students[1]};
/*
foreach($students as $seat){
    echo $$seat.'<br />';
}
 $$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

代码如下:
<?php
echo '<p>shift & unshift </p>';
$numbers = array(1,2,3,4,5,6);
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);


更多数组函数参考

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

代码如下:
<?php

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表