Detailed analysis of constants and variables in PHP

Source: Internet
Author: User
Tags define definition
This article mainly and you introduced the PHP constants and variables examples of the relevant information, the need for friends can refer to, hope to help everyone.

Examples of constants and variables in PHP

"PHP receives parameters under command line"

If you debug PHP at the command line, the incoming parameters are obtained through $ARGV, note that the file name contains the element, and the number of elements in the array is obtained by $ARGC.

"Mutable variables"

Refers to the variable name variable, the identifier of the variable can be replaced by the value of another variable.

For example, the second statement assigns a value to the ARGV1 variable.


<?php    $varName = ' argv1 ';   $ $varName = ' value1 ';   Var_dump ($argv 1);  ? >

constant

Use define definition, not delete and modify, write name directly when calling. Define also has a three-parameter version, and the third parameter indicates whether it is case insensitive and defaults to false.


<?php    define (' pi ', 3.14);   echo pi;  ? >

Tip: First check that constants are defined and redefined, using the defined function:


<?php    if (!defined (' pi '))     define (' pi ', 3.14);   else     Echo ' pi has been defined<br> ';  ? >

For constants with special symbols, you need to use the constant function to call, note that this constant name should be quoted, for example:


<?php    if (!defined (' = = '))     define (' = = ', ' puzzled ');   else     Echo ' pi has been defined<br> ';      ECHO constant (' = = ');  >

Gets all the constants that have been defined:


<?php      Var_dump (get_defined_constants ());  ? >

"Magic Variable"

__line__ gets the current row, __file__ gets the current path.

An application:

Use the Str_replace function to replace file names in files with paths + filenames to ensure that file path changes are still accessible.

Str_replace (< value to find >,< replace with value >,< searched string >,< Replacement count variable (optional) >);


<?php      define (' ROOT ', str_replace (' a.php ', ' ', __file__ ');      echo ROOT;  ? >

"In-System"

Before the number 0 is octal, plus 0x is hexadecimal.

"String Type"

Double and single quotes are possible, but double quotes resolve internal variables, but single quotes are efficient.

Double quotation-Mark parsing variable: {} can be guaranteed to separate variable names from other parts.


<?php    $name = "Test";   echo "username is {$name}";  ? >

Related recommendations;

Definitions of constants and variables in PHP, usage, and examples of differences

Summary of constants and variables commonly used in thinkphp

In PHP, what are the predefined constants and variables, and how are they used and exported?

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.