php编程基础学习(1)

首先说下我使用的IDE(Integerated Development Environment),使用的是zend studio5,我个人比较喜欢用它,呵呵

 

让我们从HelloWorld开始吧~

看如下代码:

  1. <html>
  2. <head>
  3. <title>HelloWorld!</title>
  4. </head>
  5. <body>
  6. <h1><center>
  7. <?php 
  8. $string="Hello World!";
  9. echo $string
  10. ?> 
  11. </center></h1>
  12. </body>
  13. </html>

  还记得C中的Hello World怎么写么?这里的php语句是不是很简洁呢?呵呵,可爱的php脚本。看代码,只看<?php...?>部分,这里就是php代码了。其他部分是html。我们会发现,变量($string为字符串变量,C中没有的)的初始化不需要变量说明,输出是便是echo+输出内容,如果是输出字符串,则字符串要用""号引起。对了,不要忘了语句后面的 ; 哦!

 

Hello World完后我再来看下几种类型变量的定义和初始化!

 

  1. <html>
  2. <head>
  3. <title>Type testing!</title>
  4. </head>
  5. <body>
  6. <h1><center>
  7. <?php
  8. //字符串的定义
  9. $mysting="字符串";
  10. $NewLine="换行!/n";
  11. //整型的定义
  12. $int1=38;
  13. $int2=49;
  14. $hexint=0x10;
  15. //浮点型的定义
  16. $float1=1.632;
  17. $float2=1.4E+2;
  18. //数组的定义,注意最后的分号不能少
  19. $MyArray1=array(1962,1968,1964,1981);
  20. $MyArray2=array(
  21.                 "地支"=>array("子","丑","寅","卯"),
  22.                 "生肖"=>array("鼠","牛","虎","兔"),
  23.                 "数字"=>array(1,2,3,4)
  24.                 );
  25. //类的定义
  26. //对象变量的定义               
  27. class Star{
  28.     function makeStar(){
  29.         echo "This is Star!";
  30.      
  31.     }
  32. }
  33. //变量的声明
  34. $bar=new Star;
  35. $bar->makeStar();
  36. ?>
  37. </center></h1>;
  38. </body>
  39. </html>

常量的应用

自己运行一下代码,就知道这两个常量是做什么的了,还有很多常量,以后会提到。

  1. <html>
  2. <head>
  3. <title>常量范例</title>
  4. <body>
  5. <h1><center>
  6. //使用_FILE_和_LINE_
  7. <?php
  8. function report_error($file,$line,$message){
  9.     echo "An error occured in $file on line $line:$message.";
  10. }
  11. report_error(__FILE__,__LINE__,"Something went wrong!");
  12. ?>
  13. </center></h1>
  14. </body>
  15. </head>
  16. </html>

用户自己定义常量

这一范例中,用户自己定义一个名为CONSTANT的字符串常量,代码如下:

  1. <html>
  2. <head>
  3. <title>Define常量</title>
  4. </head>
  5. <body>
  6. <h1><center>
  7. <?php
  8. define("CONSTANT","Hello World.");
  9. echo CONSTANT;//output "Hello World."
  10. undefine("CONSTANT");
  11. ?>
  12. </center></h1>
  13. </body>
  14. </html>

定义数组和对象类型

代码能说明一切,别的不多说了:

  1. <html>
  2. <head>
  3. <title>Define Array & Define Object</title>
  4. </head>
  5. <body>
  6. <h1><center>
  7. <?php
  8. //Define Array
  9. $name[]="hehe";//与C语言一样,数组也是从o开始的,此处的数组$name[]初始化相当于$name[0]="hehe";
  10. //Define Object
  11. //初始化object变量
  12. class Star{
  13.     function name(){
  14.         echo "Good name,hehe!";
  15.     }
  16. }
  17. //声明变量
  18. $obj=new Star;
  19. $obj->name();
  20. ?>
  21. </center></h1>
  22. </body>
  23. </html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值