PHP comment

Like most languages, PHP lets you add comments to your code. Comments are chunks of text that are ignored when the code is run by the PHP engine, but which are useful to programmers who need to read the code.

In this article you learn how to create PHP comments, and look at some good commenting practices, including how to write and format comments effectively. You also find out how to use comments to "comment out" chunks of PHP code.

Creating a PHP comment

There are 2 basic types of PHP comments:

  • A single-line comment . This is a short comment within a single line of code.
  • A multi-line comment . This is a longer comment that can stretch over many lines.

Writing single-line comments

To write a single-line comment , place either // (2 slashes) or a # (hash) before the comment :


// Here's a single-line PHP comment .
# Here's another single-line PHP comment .

The first comment style tends to be more popular with PHP programmers, but both styles are perfectly valid.

You can write single-line comments on their own — as just shown — or you can place a comment at the end of a line of code:


$widgetsLeft--; // Sold another widget!

Everything from the comment symbol (// or # ) to the end of the line is treated as a comment by the PHP engine, and is ignored.

Writing multi-line comments

To write a multi-line comment , start the comment with /* (a slash followed by an asterisk) and end the comment with */ (an asterisk followed by a slash). Here's an example:


/*
Here's a long PHP comment spread over
many lines.

You can format a multi-line comment
any way you like.
*/

Be careful: You can't nest multi-line comments in PHP . The following code won't work:


/*
Here's the start of the outer comment .
/* Here's the inner comment . */
Here's the end of the outer comment .
*/

This mistake is easy to make when commenting out PHP code, as described in a moment.

Good PHP commenting practice

It's important to comment your PHP code. Code has a habit of making sense at the time you write it, then looking like a mess in 3 months' time! If you add comments at the time you write your code then you will find that your code is a lot more readable when you (or another developer) return to it later.

A comment should explain what a chunk of code does, or is intended to do. Here are some good examples:


if ( $widget->stockLeft == 0 ) $widget->delete(); // Remove the widget if it's sold out

// Show only recent articles
for ( $i=0; $i < count( $articles ); $i++ ) {
if ( time() - $articles[$i]->pubDate < (86400 * 7) ) $articles[$i]->displayTitle();
}

Don't make the mistake of being too literal with your comments, though. The following comments don't really help:


if ( $widget->stockLeft == 0 ) $widget->delete(); // Call the widget's delete() method if its stockLeft property is zero

// Loop through all articles and call $articles[$i]->displayTitle() if time() - $articles[$i]->pubDate < (86400 * 7)
for ( $i=0; $i < count( $articles ); $i++ ) {
if ( time() - $articles[$i]->pubDate < (86400 * 7) ) $articles[$i]->displayTitle();
}

A good trick is to write your comments before you write your code. This forces you to think about what the code is supposed to do, rather than getting bogged down in implementation details.

Formatting comments

It's good to format your comments to make them easy to read. The following PHP comments are hard to read due to bad formatting:


// Retrieve all widgets
$widgets = Widget::getAll();
/*Update all the widgets in the database,
changing each widget's quantity*/
for ( $i=0; $i < count( $widgets ); $i++ ) {
$widgets[$i]->quantity += $widgets[$i]->getNewStock(); //Set the quantity
$widgets[$i]->update(); //Update the widget
}

These comments, on the other hand, are much easier on the eye:


// Retrieve all widgets
$widgets = Widget::getAll();

/*
Update all the widgets in the database,
changing each widget's quantity
*/

for ( $i=0; $i < count( $widgets ); $i++ ) {
$widgets[$i]->quantity += $widgets[$i]->getNewStock(); // Set the quantity
$widgets[$i]->update(); // Update the widget
}

Commenting out PHP code

You can use PHP comments to "comment out" (temporarily disable) chunks of code. This can be useful when debugging your PHP scripts:


/*
if ( $widget->stockLeft == 0 ) $widget->delete(); // Remove the widget if it's sold out

// Show only recent articles
for ( $i=0; $i < count( $articles ); $i++ ) {
if ( time() - $articles[$i]->pubDate < (86400 * 7) ) $articles[$i]->displayTitle();
}
*/

However, be careful when commenting out PHP code containing multi-line comments. Since you can't nest multi-line comments in PHP , the following won't work:


/*
/*
Update all the widgets in the database,
changing each widget's quantity
*/

for ( $i=0; $i < count( $widgets ); $i++ ) {
$widgets[$i]->quantity += $widgets[$i]->getNewStock(); // Set the quantity
$widgets[$i]->update(); // Update the widget
}
*/

In this article you've learned how to write PHP comments — both single-line and multi-line — and looked at some useful commenting techniques, including how to write meaningful comments, how to format comments, and how to "comment out" pieces of PHP code. Happy coding!

[root@yfw ~]# cd /www/wwwroot/yfw.szrengjing.com [root@yfw yfw.szrengjing.com]# ls /www/wwwroot/yfw.szrengjing.com 404.html activity.php admin affiche.php affiliate.php ajax_68ecshop.php ajax_www_68ecshop_com.php alipay.html animated_favicon.gif api api.php app apply_index.php apply.php article_cat.php article_list.php article.php auction.php brand.php captcha.php catalog.php category.php cert certi.php chat.php chinabank_receive.php comment_add.php comment.php compare.php cycle_image.php data delete_cart_goods.php denglu.php dl_receiver.php erweima_png.php erweima_supplier.php exchange.php favicon.ico feed.php findPwd.php flash.js flow.php gallery.php goods_comment.php goods.php goods_script.php goods_shaidan.php group_buy.php help.php http-bind.php images includes index.php install Iogo.png js kuaidi.php languages login_act_ajax.php message.php mobile myship.php other.php package.php pick_out.php plugins pm.php pre_sale.php pricecut.php pro_goods.php pro_search.php qrcode.png question.php quotation.php README.md receive.php record.php region.php register.php request.log respond.php respondwx.php robots.txt scan_list.php scan.php scan_php_files.sh search.php security.php sitemaps.php sitemaps.xml sms snatch.php sound stores.php supplier supplier_article.php supplier_category.php supplier_index.php supplier_other.php supplier.php supplier.png supplier_reg.php supplier_search.php tag_cloud.php takegoods.php temp themes topic.php url301.php user.php validate.php virtual_group_goods.php virtual_group.php vote.php wap wholesale.php widget 安装说明.txt 模板修改教程.doc '鸿宇小京东7.9最新版(PC+WAP+微信+分销系统):集成新版短信+批发+预售+储值卡' '鸿宇小京东7.9最新版(电脑+手机+微信+分销系统):集成新版短信+批发+预售+储值卡 (1).zip' [root@yfw yfw.szrengjing.com]#
11-09
### 解决语法错误的方法 #### HTML 文件 可以使用 `tidy` 工具来修复 HTML 文件的语法错误。`tidy` 是一个广泛使用的 HTML 清理和修复工具,它可以自动检测并修复许多常见的 HTML 语法问题。 ```bash # 安装 tidy(如果未安装) yum install tidy -y # 对于 CentOS/RHEL 系统 apt-get install tidy -y # 对于 Debian/Ubuntu 系统 # 修复单个 HTML 文件 tidy -m -i /www/wwwroot/yfw.szrengjing.com/includes/fckeditor/editor/fckdebug.html # 批量修复目录下所有 HTML 文件 find /www/wwwroot/yfw.szrengjing.com -name "*.html" -exec tidy -m -i {} \; ``` #### JavaScript 文件 对于 JavaScript 文件,可以使用 `eslint` 工具进行语法检查和修复。`eslint` 是一个高度可配置的 JavaScript 代码检查工具,能帮助发现并修复语法错误和潜在的问题。 ```bash # 安装 ESLint npm install -g eslint # 初始化 ESLint 配置 eslint --init # 检查并修复单个 JavaScript 文件 eslint --fix /www/wwwroot/yfw.szrengjing.com/mobile/includes/fckeditor/editor/js/fckeditorcode_ie.js # 批量检查并修复目录下所有 JavaScript 文件 find /www/wwwroot/yfw.szrengjing.com -name "*.js" -exec eslint --fix {} \; ``` #### CSS 文件 `stylelint` 是一个强大的 CSS 代码检查工具,可以用来检查和修复 CSS 文件的语法错误。 ```bash # 安装 Stylelint npm install -g stylelint # 初始化 Stylelint 配置 stylelint --init # 检查并修复单个 CSS 文件 stylelint --fix /www/wwwroot/yfw.szrengjing.com/includes/fckeditor/editor/skins/default/fck_editor.css # 批量检查并修复目录下所有 CSS 文件 find /www/wwwroot/yfw.szrengjing.com -name "*.css" -exec stylelint --fix {} \; ``` ### 目录下文件列表信息分析 从给出的文件列表信息来看,存在语法错误的文件主要集中在几个富文本编辑器相关的目录,如 `fckeditor`、`ueditor`、`kindeditor` 等,这些编辑器包含大量的 HTML、JavaScript 和 CSS 文件,可能由于版本兼容性、代码编写不规范等原因导致语法错误。 另外,在 `mobile`、`supplier`、`admin` 等不同功能模块的目录下也存在大量有语法错误的文件,这可能意味着整个项目的前端代码质量存在一定问题,需要进行全面的代码审查和修复。 同时,部分文件的命名包含特殊字符,如 `搜虎精品社区www.souho.net.html`,这可能会在某些环境下导致文件访问或解析出现问题,建议规范文件命名。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值