PHP学习笔记1


#1

HTML页面嵌入PHP脚本的方法

<script language="php">
代码
</script>
<?php
代码
?>

访问外部变量需要加global

<?
$out=1;
function foo(){
    global $out;
    echo $out;
}
foo();

变量的值可以作为另一个变量的名称

<?
$foo="hello";
$$foo="world";
echo "$foo $hello";     // hello world
echo "$foo ${$foo}";    // hello world
?>

与C++相似的引用传递和默认参数

<?
function foo(&$bar){
  $bar.=" world";
}
$str="hello";
foo($str);
echo $str;    // hello world
?>
<?
function foo($name, $type="coffee"){ // 注意默认参数在最后面
  echo "type of $name is $type.\n";
}
echo foo("drink");

#2

变量的值可以作为另一个变量的名称, 这个还真是第一次知道 :clap: