本节继续上一节的乱码问题,以此展开学习php变量 php解析 CSS常见元素与属性等。
1.PHP中使用HTML注释
新建立一个study.php文件,内容如下:
<?php
header("Content-Type: text/html;charset=utf-8");
$aa = 1;
//PHP注释
?>
<?php
echo $aa;
?>
<html>
<head>
<!--<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />-->
<title>My First PHP Program</title>
</head>
<body>
中文是否是乱码
<h1 align="left">Left-Aligned Heading</h1>
<p>This heading uses the align attribute with a value of left.</p>
<h1 align="center">Centered Heading</h1>
<p>This heading uses the align attribute with a value of center.</p>
<h1 align="right">Right-Aligned Heading</h1>
<p>This heading uses the align attribute with a value of right.</p>
</body>
</html>
因为使用了header函数告诉浏览器文本的格式为UTF-8,所以上述文档保存为UTF-8无BOM格式时,中文显示正常。
将上面的第一段PHP代码使用HTML注释起来,会发现中文为乱码。照理说PHP解析的时候会忽视掉自己不认识的字符,将其原样交给HTML,对于其认识的代码会正常解析,echo $aa正常打印了1即可说明问题,那么为什么header函数没有起作用呢?查看网页源码如下:
<!--<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at E:\wamp\www\study.php:1) in <b>E:\wamp\www\study.php</b> on line <b>2</b><br />
-->
1<html>
<head>
<!--<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />-->
<title>My First PHP Program</title>
</head>
问题清楚了,正如上节所述,header函数之前有了输出“<!--”,所以会告警了。因为警告信息被包含在HTML注释之中,所以并未显示出来。这种使用HTML注释来进行错误保护的手法还是比较常见的。比如下面:
<!--<?php
include "111.php";
$aa = 1;
//PHP注释
?>-->
<?php
echo $aa;
?>
include一个根本不存在的文件会导致警告,HTML注释很好的掩盖了这一警告。仅能从源码中看到警告信息如下:
<!--<br />
<b>Warning</b>: include(111.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in <b>E:\wamp\www\study.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>: include() [<a href='function.include'>function.include</a>]: Failed opening '111.php' for inclusion (include_path='.;C:\php5\pear') in <b>E:\wamp\www\study.php</b> on line <b>2</b><br />
-->
1<html>
2.变量的作用域
从上面的例子可以看出,即便位于两段php代码之中,$aa同样有效。
<?php
$aa = 1;
?>
<?php
echo $aa."<br />";
if($aa > 0)
{
$bb = 0;
}
echo $bb."<br />";
?>
这样$bb居然还是有效的,从C转过来的同学表示亚历山大啊,没办法这就是PHP。
<?php
$aa = 1;
?>
<?php
echo $aa."<br />";
if($aa > 0)
{
$bb = 0;
}
echo $bb."<br />";
function fun()
{
//global $aa;
echo $aa;
}
fun();
?>
这次将在函数中使用$aa,发现无法打印出$aa,因为外面定义的$aa在函数体中是无法访问的,若要访问则需要首先用global申明。如下:
<?php
$aa = 1;
?>
<?php
echo $aa."<br />";
if($aa > 0)
{
$bb = 0;
}
echo $bb."<br />";
function fun()
{
global $aa;
$aa += 1;
}
fun();
echo $aa;
?>
下面看看在include时,全局变量通过$GLOBALS[]来访问的例子。
<?php
$aa = 1;
?>
<?php
include "g06.php"
?>
<?php
header("Content-Type: text/html;charset=utf-8");
echo "hello world!";
echo $GLOBALS['aa']."<br />";
echo $aa;
?>
因为include相当于将代码原样引入进来,所以是可以正常访问先前已经定义好的全局变量$aa的。使用$GLOBALS来引用,代码可读性更好。
3 常用的元素和属性
eg:
<h2>Example of the <listing>, <plaintext>, and <xmp> Elements</h2>
<pre name="code" class="html"><span style="font-family: FangSong_GB2312; ">展示的效果为:</span>
<span style="font-family: FangSong_GB2312; "></span><pre name="code" class="html"><h2>Example of the <listing>, <plaintext>, and <xmp> Elements</h2>
 ,>,$lt都是比较常用的。
eg:
<body background="images/background_large.gif" bgcolor="#f2f2f2">
<h2>Example of the background Attribute</h2>
</body>
background设置背景图片,bgcolor背景颜色,这些以后会在CSS中完成。#f2f2f2这个RGB是灰色的,还比较好看。
eg: ul/li的无需列表; ol/li的有序列表; table/th/tr/td的表格结构;dl/dt/dd这个层级列表有点意思
<dl>
<dt>我们在做列表标题</dt>
<dd>我们在做列表</dd>
<dt>我们在做列表标题</dt>
<dd>我们在做列表</dd>
<dd>我们在做列表</dd>
<dd>我们在做列表</dd>
</dl>
本章最后举了一个综合性的例子,涉及到外部CSS的引入,表布局 连接等。