首页 > php教程 > php手册 > 正文

第四章_PHP基本语法

WBOY
发布: 2016-06-13 10:17:52
原创
946 人浏览过

1.第一个PHP程序

<span 1</span> <span //</span><span 弱爆了有木有</span>
<span 2</span> <?<span php
</span><span 3</span>     <span echo</span> "<span Hello PHP!';
</span><span 4</span> ?>
登录后复制

2.PHP共支持8种基本数据理性,包括4种标量类型:boolean(布尔型)、integer(整型)、float(浮点型,也称为double)、string(字符串),2种复合类型:array(数组)、object(对象),2种特殊类型:resource(资源)、NULL(空类型)

3.函数var_dump可以用来打印变量的相关信息。

<span 1</span> <?<span php
</span><span 2</span>     <span var_dump</span>((bool) <span false</span><span );
</span><span 3</span>     <span var_dump</span>((bool) 0<span );
</span><span 4</span>     <span var_dump</span>((bool) 0.0<span );
</span><span 5</span>     <span var_dump</span>((bool) ''<span );
</span><span 6</span>     <span var_dump</span>((bool) "0"<span );
</span><span 7</span>     <span var_dump</span>((bool) <span array</span><span ());
</span><span 8</span>     <span var_dump</span>((bool) <span NULL</span><span );
</span><span 9</span> ?>
登录后复制

4.关于整型。

  在32位操作系统中,整型数的有效范围是-2147483648~+2147483647。如果超过了这个范围,将会被PHP引擎解释成浮点数(float)。

  用八进制加0,用十六进制加0x(是数字0而非字母O)

<span  1</span> <?<span php
</span><span  2</span>     <span $dec_int</span>=63<span ;
</span><span  3</span>     <span var_dump</span>(<span $dec_int</span><span );
</span><span  4</span>     
<span  5</span>     <span $oct_int</span>=077<span ;
</span><span  6</span>     <span var_dump</span>(<span $oct_int</span><span );
</span><span  7</span>     
<span  8</span>     <span $hex_int</span>=0x3f<span ;
</span><span  9</span>     <span var_dump</span>(<span $hex_int</span><span );
</span><span 10</span> ?>
登录后复制

5.字符串型

  有3种定义字符串的方式,分别是单引号(‘)、双引号(“)和界定符(<<<)。前面2个的区别是,假如字符串中包含变量名,双引号中的变量名会被实际值替代,而单引号中的变量名会按普通字符直接输出。

<span 1</span> <?<span php
</span><span 2</span>     <span $name</span>='麦兜'<span ;
</span><span 3</span>     <span $age</span>=19<span ;
</span><span 4</span>     <span echo</span> '本人大名$name,年方$age'<span ;
</span><span 5</span>     <span echo</span> '<p>'<span ;
</span><span 6</span>     <span echo</span> "本人大名<span $name</span>,年方<span $age</span>"<span ;
</span><span 7</span> ?>
登录后复制

  关于界定符使用如下:

<span 1</span> <?<span php
</span><span 2</span>     <span echo</span> <<<<span MY_STR
</span><span 3</span> <span     我是PHP初学者
</span><span 4</span> <span MY_STR;      //最后结尾的标识符前面不能有空格或者缩进,必须顶咯
</span><span 5</span> ?>
登录后复制

6.对象类型

<span  1</span> <?<span php
</span><span  2</span> <span class</span><span  Person {
</span><span  3</span>     <span public</span> <span $name</span><span ;
</span><span  4</span>     <span private</span> <span $age</span><span ;
</span><span  5</span>     
<span  6</span>     <span function</span> Person(<span $name</span>,<span $age</span><span ){
</span><span  7</span>         <span $this</span>->name=<span $name</span><span ;
</span><span  8</span>         <span $this</span>->age=<span $age</span><span ;
</span><span  9</span> <span     }
</span><span 10</span>     
<span 11</span>     <span function</span><span  getInfo(){
</span><span 12</span>         <span echo</span> '我叫'.<span $this</span>->name.'<br>我今年'.<span $this</span>->age.'岁了'<span ;
</span><span 13</span> <span     }
</span><span 14</span> <span }
</span><span 15</span>     <span $boy</span> = <span new</span> Person("kimi",5<span );
</span><span 16</span>     <span $boy</span>-><span getInfo();
</span><span 17</span> ?>    
登录后复制

7.类型转换

  print_r() 显示关于一个变量的易于理解的信息。如果给出的是 stringintegerfloat,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素

  

<span  1</span> <?<span php
</span><span  2</span>     <span $temp_var</span>=3.141592653<span ;
</span><span  3</span>     <span $var1</span>=(<span string</span>)<span $temp_var</span><span ;
</span><span  4</span>     <span echo</span> "<p><span $var1</span>"<span ;
</span><span  5</span>     
<span  6</span>     <span $temp_var</span>='Kimi'<span ;
</span><span  7</span>     <span $var2</span>=(<span array</span>)<span $temp_var</span><span ;
</span><span  8</span>     <span echo</span> '<p>'<span ;
</span><span  9</span>     <span print_r</span>(<span $var2</span><span );
</span><span 10</span>     
<span 11</span>     <span $temp_var</span>='Cindy'<span ;
</span><span 12</span>     <span $var3</span>=(<span unset</span>)<span $temp_var</span><span ;
</span><span 13</span>     <span echo</span> '<p>'<span ;
</span><span 14</span>     <span var_dump</span>(<span $var3</span><span );
</span><span 15</span> ?>
登录后复制

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板