您的位置:首页 > 编程语言 > PHP开发

php编程基础学习(1)

2008-09-08 01:53 375 查看
首先说下我使用的IDE(Integerated Development Environment),使用的是zend studio5,我个人比较喜欢用它,呵呵

让我们从HelloWorld开始吧~

看如下代码:

<html>

<head>

<title>HelloWorld!</title>

</head>

<body>

<h1><center>

<?php

$string="Hello World!";

echo $string;

?>

</center></h1>

</body>

</html>

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

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

<html>

<head>

<title>Type testing!</title>

</head>

<body>

<h1><center>

<?php

//字符串的定义

$mysting="字符串";

$NewLine="换行!/n";

//整型的定义

$int1=38;

$int2=49;

$hexint=0x10;

//浮点型的定义

$float1=1.632;

$float2=1.4E+2;

//数组的定义,注意最后的分号不能少

$MyArray1=array(1962,1968,1964,1981);

$MyArray2=array(

"地支"=>array("子","丑","寅","卯"),

"生肖"=>array("鼠","牛","虎","兔"),

"数字"=>array(1,2,3,4)

);

//类的定义

//对象变量的定义

class Star{

function makeStar(){

echo "This is Star!";

}

}

//变量的声明

$bar=new Star;

$bar->makeStar();

?>

</center></h1>;

</body>

</html>

常量的应用

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

<html>

<head>

<title>常量范例</title>

<body>

<h1><center>

//使用_FILE_和_LINE_

<?php

function report_error($file,$line,$message){

echo "An error occured in $file on line $line:$message.";

}

report_error(__FILE__,__LINE__,"Something went wrong!");

?>

</center></h1>

</body>

</head>

</html>

用户自己定义常量

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

<html>

<head>

<title>Define常量</title>

</head>

<body>

<h1><center>

<?php

define("CONSTANT","Hello World.");

echo CONSTANT;//output "Hello World."

undefine("CONSTANT");

?>

</center></h1>

</body>

</html>

定义数组和对象类型

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

<html>

<head>

<title>Define Array & Define Object</title>

</head>

<body>

<h1><center>

<?php

//Define Array

$name[]="hehe";//与C语言一样,数组也是从o开始的,此处的数组$name[]初始化相当于$name[0]="hehe";

//Define Object

//初始化object变量

class Star{

function name(){

echo "Good name,hehe!";

}

}

//声明变量

$obj=new Star;

$obj->name();

?>

</center></h1>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: