PHP学习之语法基础篇

<?#asp风格短标记
  echo "hello world!<br/>";
?>

<?php 
  $date = "2013-5-21";
?>

<p>today's date is <?php echo $date;?></p>

<?php 
  print("<br/>print test!");
  $name = "zhangsan";
  $age = 20;
  printf("<br/>name:%s,age:%d",$name,$age);
  
  $str = sprintf("<br/>hello...<br/>");
  echo $str;
 
  $a = true;
  $lst["aa"] = "aaaaa";
  $lst["bb"] = "bbbb";
  $lst["cc"] = "cccc";
  
  var_dump($lst);
  print_r($lst);
  
  $k = (int)10.8; #强制类型转换
  print("<br/>".$k);
  
  #类型转换
  $i = "123";
  echo "<br/>i的类型为:".gettype($i);
  settype($i, integer);
  echo "<br/>转换后i的类型为:".gettype($i);
  
  
  $t = "ssss";
  function checkType($obj){
       if(is_integer($obj)){
           echo "<br/>传递的类型为int";
       }elseif (is_bool($obj)){
           echo "<br/>传递的类型为bool";
       }elseif (is_string($obj)){
           echo "<br/>传递的类型为string";
       }else{
           echo "<br/>未知类型...";
       }
  }
  
  checkType("100000");
  checkType(200);
  checkType(false);
  
  #引用赋值
  $y = "hahaha";
  #$z = $y;
  $r = &$y;
  #$z = "hhhhh";
  $r = "uuuu";
  
  echo "<br/>$y";
   
  #传引用
  function refTest(&$kk){
      $kk = 2 * $kk;
  }
  
  $dd = 100;
  refTest($dd);
  echo "<br/>".$dd;
  
  #传值
  function valTest($kk){
      $kk = 5 * $kk;
  }
  
  $k1 = 500;
  valTest($k1);
  echo "<br/>".$k1;
  
  
  #变量作用域
  #局部变量与全局变量
  
  $t2 = 1200; #全局变量
  function changeNumber(){
     $t2 = 1500;#局部变量
  }
 
  changeNumber();
  echo "<br/>$t2"; #打印1200
  
  function changeNumber1(){
      global $t2;
      #global $t2 = 1800; error
      $t2 = 1800;
  }
  
  changeNumber1();
  echo "<br/>$t2"; #打印1800
  
  
  function Test()
  {
    $GLOBALS["txn"] = 500; #$GLOBALS 必须大写
  }
  Test();
  echo "<br/>".$GLOBALS["txn"];
  
  
  #静态变量
  function keep_trace(){
      static $count = 0;
      $count++;
      echo "$count<br/>";
  }
  
  keep_trace();
  keep_trace();
  keep_trace();
  keep_trace();
  
  
  #超级全局变量
  foreach($_SERVER as $k => $v){
      print("$k => $v<br/>");
  }
  
  #$_GET
  if(isset($_GET["name"])){
      printf("当前用户:%s",$_GET["name"]);
  }
  #$_POST
  if(isset($_POST["name"])){
      printf("当前用户:%s",$_POST["name"]);
  }
  
  #cookie
  setcookie("domain","my.com");
  if(isset($_COOKIE["domain"])){
      printf("当前域信息:%s<br/>",$_COOKIE["domain"]);
  }
  
  #files
  #$_FILES["file"]["name"];
  #$_FILES["file"]["type"];
  #$_FILES["file"]["size"];
  #$_FILES["file"]["error"];
  
  #seession
  $_SESSION["user"] = "wangwu";
  echo "当前操作人:".$_SESSION["user"]."<br/>";
  
  #常量的定义
  define("PI",3.1415926);
  $pi2 = PI * 2;
  echo "2pi:$pi2<br/>";  
?>

 

posted @ 2013-05-21 23:32  玻璃鱼儿  阅读(206)  评论(0编辑  收藏  举报