Home php教程 php手册 小结:PHP编程过程中应当注意的各种注意事项

小结:PHP编程过程中应当注意的各种注意事项

Jun 21, 2016 am 09:01 AM
php quot

  最近一段时间,PHP凭借其功能强大,简单易用的特点被越来越多的人们所喜爱和接受,成为目前最为流行的脚本编程语言之一。本文将结合用户在使用PHP过程中经常容易出现的问题,包括语法错误,程序运行错误以及逻辑错误等展开详细的介绍,希望能够对那些目前正在学习PHP语言的用户有所帮助。此外,本文还将会在文章的结束部分对PHP编程过程中应当注意的各种事项加以汇总,供读者参考。

  PHP脚本语言所具有一个显著的特点就是能够自动生成变量实例,不要求用户对所需要使用的变量进行声明。该特点既有值得推崇之处,但是同时也为用户所编写的脚本程序埋下了隐患。从好的方面来说,用户不再需要象使用C语言那样,在程序的开头对每一个将要在程序中使用的变量进行声明;但是,现在用户必须非常小心每一个变量名称的正确拼写,否则就可能会在无意当中创建了新的变量。有些编程语言,例如PERL等,可以记录下程序中只使用过一次的变量的信息,根据用户的预先设置发出警告提示。此外,PERL还提供了一个功能十分强大的调试器。截止到4.0.2版本,PHP语言中还没有包含调试器。但是这并没有影响到PHP语言的流行和推广。PHP提供了非常广泛的功能,即使与象C语言这样成熟的编程语言相比也毫不逊色。可以说,PHP是目前使用最方便,对WEB支持功能最完善的脚本语言之一。

  下面,就让我们言归正传,来看一看在PHP编程过程容易出现哪些问题,以及相应的应当采用什么样应对措施。

  分号的使用正象我们平时写文章时必须要在每一句话的结尾处加上句号一样,PHP要求程序中的每一条语句都必须使用分号“;”结束。这是一条最基本的语法规则,但是同时也最容易出现问题。我们在编写程序时,很少会一行一行的检查是否漏掉了分号,但是一旦出现任何一处疏忽,解析程序就回立刻发出错误报告。有时,报告中可能会包含出现问题的语句行数。

    $Output="Hello World";
  echo $Output
  $Other="Blah";
  print $SomeMoreText;
  ?>

  上述代码的第二行“echo $Output”的结尾处我们漏掉了一个分号,如果执行该脚本将会产生如下错误提示:

  Parse error: parse error, expecting `’,’’ or `’;’’ in /usr/local/apache/htdocs/test.php on line 8报告虽然指出了出现错误的原因,即漏掉了逗号“,”或分号“;”,但是却将出现问题的语句定在了第八行。因为这段代码非常简单,我们很容易就可以找到真正出现错误的地方。但是,如果程序非常复杂时,要想顺利的找出错误就有些困难了。

  根据笔者以往的经验,建议采用以下方法:

  如果当错误报告中指出的语句没有明显的问题时,可以检查位于该语句之前的其它指令行(不包括注释行)是否正确。如果仍然没有找到错误,可以将报告中指出的语句行注释掉(在该语句行的最前面加上“//”或“#”注释符号)或者改为用户能够确保完全没有问题的其它语句。之后,重新运行程序,如果错误提示仍然指向同一行,就表明真正有问题的语句应当位于被注释掉的语句行的前面。按照上述方法逐一检查位置靠前的每一行指令,直到错误提示信息发生改变。这时,我们就成功的挖出了真正的罪魁祸首。

  变量的问题与其它编程语言要求用户显式声明变量不同,PHP语言允许用户自动使用所有变量而不必进行事先的声明。变量名称的拼写错误成为困扰PHP用户的一个大问题。

    function Combine ($FirstHalf, $SecondHalf)
  {
   $Combined_String=$FirstHalf.$SecondHalf;
   return $Combined_String;
  }
  $FirstString="WDVL - ";
  $SecondString="Illustrated Encyclopedia";
  $Combine_Result=Combine ($FirstString, $SecondString);
  print $Combined_Result;
  ?>

  当我们运行以上脚本时,将会看到由于程序没有返回任何数据而出现的错误提示。这里,我们为了更好的说明问题选择了一个非常直观的例子。在现实当中,有时问题并不会这么简单。相信大家都已经找到了出现问题的原因,那就是“print $Combined_Result;”中的变量名称“$Combined_Result”应当改为“$Combine_Result”。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles