php程序时的基本语法是,PHP 基本语法

本文详细介绍了PHP代码的嵌入方式,包括短标记、ASP风格标记及其开启条件,强调了正确使用开始标记的重要性。同时,讨论了如何在PHP中进行条件输出和文本分离,以及PHP中的注释规则,提醒开发者注意避免嵌套注释和误用HTML注释。文章还提到了分号的必要性以及代码效率问题,为PHP编程提供了实用指导。
摘要由CSDN通过智能技术生成

PHP 标记

<?php ?>,这告诉 PHP 开始和停止解析二者之间的代码。

1.<?php echo 'if you want to serve XHTML or XML documents, do it like this'; ?>

2.

echo 'some editors (like FrontPage) don\'t

like processing instructions';

3. echo 'this is the simplest, an SGML processing instruction'; ?>

= expression ?> This is a shortcut for " echo expression ?>"

4.

上例中的 1 和 2 中总是可用的,其中示例 1 中是建议使用的。

短标记(上例 3)仅在通过 php.ini 配置文件中的指令 short_open_tag 打开后才可用,或者在 PHP 编译时加入了 --enable-short-tags 选项。

ASP 风格标记(上例 4)仅在通过 php.ini 配置文件中的指令 asp_tags 打开后才可用。

如:

注意事项:

开始标记是:<?php [whitespace],而不是<?php。

另外,如下代码:

 
 

my

name

Oliver

结果是:

my

name

is: Oliver

代码如果改成:

 
 

my

name

<?php echo "is:"?> //这里的最后加上一个空格

Oliver

结果就是:

my

name

is:

Oliver

从 HTML 中分离

可以使 PHP 嵌入到 HTML 文档中去,如下例所示。

this is a title in the browser

<?php echo "

this is another paragraph.

" ?>

this is a paragraph

上述代码的结果直接显示:

this is a title in the browser

this is another paragraph.

this is a paragraph

如果将代码改为:

this is a title in the browser

<?php echo "

this is another paragraph.

\r" ?>

this is a paragraph

结果则显示的是:

this is a title in the browser

this is another paragraph.

this is a paragraph

当 PHP 解释器碰到 ?> 结束标记时就简单地将其后内容原样输出除非马上紧接换行,例外是处于条件语句中间时,此时 PHP 解释器会根据条件判断来决定哪些输出,哪些跳过。如:

使用条件的高级分离术:

This will show if the expression is true.

Otherwise this will show.

如:

<?php if (100 > 20): ?>

bigger

smaller

上述代码的结果直接显示:

bigger

要输出大段文本时,跳出 PHP 解析模式通常比将文本通过 echo 或 print 输出更有效率。

又或者如下for循环代码:

hello i am in the loop.

结果则显示的是:

hello i am in the loop.

hello i am in the loop.

hello i am in the loop.

hello i am in the loop.

如foreach循环代码:

hello

结果则显示的是:

1

hello

2

hello

3

hello

4

hello

5

hello

6

hello

又如while循环语句:

in the loop.

结果则显示的是:

0

in the loop.

1

in the loop.

2

in the loop.

3

in the loop.

4

in the loop.

5

in the loop.

6

in the loop.

7

in the loop.

8

in the loop.

9

in the loop.

指令分隔符

PHP 需要在每个语句后用分号结束指令。

<?php echo "hello"; ?> //加上分号

文件末尾的 PHP 代码段结束标记可以不要

有些情况下当使用 include 或者 require 时省略掉会更好些

注释

注释风格如下:

echo "testing area."; //the one-line c++ style comment

/* multi line comment

multi line comment.*/

echo "Testing."; #another one-line shell-style comment

?>

C 风格的注释在碰到第一个 */ 时结束。要确保不要嵌套 C 风格的注释。试图注释掉一大块代码时很容易出现该错误。

/*

echo "This is a test"; /* This comment will cause a problem */

*/

?>

另外,使用如下代码可以方便其他人阅读(J.Prettyman):

//======================================================================

// CATEGORY LARGE FONT

//======================================================================

//-----------------------------------------------------

// Sub-Category Smaller Font

//-----------------------------------------------------

/* Title Here Notice the First Letters are Capitalized */

# Option 1

# Option 2

# Option 3

/*

*This is a detailed explanation

*of something that should require

*several paragraphs of information.

*/

// This is a single line quote.

?>

此外,不要对php代码部分使用html注释格式():

这样的做法是不正确的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值