Several Notes for using PHP constants (Be careful when using constants in PHP) and constants in php

Source: Internet
Author: User
Tags define function php define php framework zend framework

Several Notes for using PHP constants (Be careful when using constants in PHP) and constants in php

Why be careful when using constants in PHP?

The Zend Framework document states that a constant contains numbers, letters, and underscores. A number can be used as a constant name. All letters of the constant name must be capitalized. A class constant must be defined as a member of a class through "const". It is strongly discouraged to use a global constant defined by "define.

As the official PHP framework, why is there such a requirement?

Let's analyze it together.

1. define is prone to unexpected errors.

PHP constants cannot be modified or assigned again after they are defined. But what if I assign a value again?

<?php define('C', 12345); define('C', 123);?>

This code will report a notice error. The consequence is that, before you define a constant with the same name, you may not know what the value is.

2. How can I determine whether PHP constants are defined? Easy to write wrong judgment methods

<? Php define ('C', 12345); // method 1 of the Error. if (isset (C )){......} // Method 2 of the Error. if (defined (C )){......} // The correct method is if (defined ('C') {……}?>

3. Low execution efficiency

<? Php define ('Forum _ THEME ', $ FORUM ['Theme']); $ this-> display ('/'. FORUM_THEME. '@ Public: login'); // The system looks for FORUM_THEME from the execution process.

Because php performs multiple searches when processing constants, the efficiency is low.

Summary: The problem with PHP constants lies in the fact that PHP is too loose in dealing with constants. If it can be strict, it will avoid many problems. In the actual process, you can use variables instead of constants, because the efficiency of variables is more convenient to use.

Therefore, if you do not want to use constants or class variables, you can use the following methods:

<?php class foo {  const WEBSITE = "www.zhuyinghao.com";  protected $_forum_theme;  function name()  {    echo WEBSITE;    $this->_forum_theme = $forum['theme'];  }  function displace()   {    echo $this->_forum_theme;  } }?>

Function Name

In PHP 4, the class constructor must be the same as the class name. The constructor name of the subclass is the same as that of the subclass. In the subclass, the constructor of the parent class is not executed automatically. To execute the constructor of the parent class in the subclass, you must execute statements similar to the following:

$ This-> [constructor name of the parent class ()]

In PHP 5.0 and later versions, construct () is used as the constructor, but it is still compatible with the definition rules of constructor 4.0. If both construct 4.0 and construct () are defined, the construct () function takes precedence.

Use php eol to replace/r/n for line feed

Line breaks are often used when writing a program, and the PHP built-in constant PHP_EOL is used for line breaks.

A small line feed has different implementations on different platforms. In the unix world, the newline is replaced by \ n, but in windows, \ r \ n is used to reflect its differences. What's more interesting is that \ r is used in mac. Therefore, \ n is used for unix, \ r \ n for windows, and \ r for mac.

Therefore, the system will convert to different line breaks based on different platform systems. Use the PHP_EOL variable to wrap a line in the browser.


How to Use PHP constants?

1. constants are generally capitalized.
Define ('mystr', "My constants ");
2. constants cannot be defined repeatedly. They are fixed values. Relative Variables

In the output of constants in php, under what circumstances do we need to use constant?

A constant indicates that it will not change and can only be assigned once. The define function is used for the definition of a constant: define ('pai ', 3.14 );

Echo constant ('pai '); effect: echo PAI;

Therefore, the constant function is generally not used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.