What is the way to define constants in php
How to define constants in php: 1. Use the define() function to define, the syntax "define (constant name, constant value, whether case sensitive)"; 2. Use the const keyword to define, the syntax "const Constant name = constant value;".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
How to define constants in php
define() function
const keyword
Compared with variables, Constants have the following characteristics:
There is no dollar sign ($) in front of the constant;
Constant can only be defined with define() and const ;
The scope of constants is global;
Once a constant is defined, it cannot be redefined or undefined.
define() function defines constants
Legal constant names can only start with letters and underscores, and can be followed by letters of any length, Numbers or underscores. In PHP, you can define a constant through the define() function. The syntax of the define() function is as follows:
define($name, $value , $case_insensitive)
$name Required parameters, constant name (need to use double quotes Or wrapped in single quotes)
$value Required parameter, the value of the constant
$case_insensitive Optional parameter, specifies whether it is case sensitive, The default is case-sensitive. If set to true, it means that it is not case-sensitive
<?php header("Content-type:text/html;charset=utf-8"); define('WebSite', 'php中文网'); echo WebSite.'<br>'; ?>
const keyword definition constant
Syntax:
const 常量名 = 常量值;
Example:
<?php const FOO = 'BAR'; echo FOO.'<br>'; ?>
##The difference between define() and const:
const defines constants at compile time, while the define() method defines constants at runtime. const cannot be used in if statements, defne() can be used in if statements.if(...) { const FOO = 'BAR';//错误 } if(...) { define('FOO', 'BAR');//正确 }define() A common scenario is to first determine whether the constant has been defined before defining the constant:
if(defined('FOO)) { define('FOO', 'BAR') }const When defining a constant, the value can only be a static scalar (number, string, true , false, null), and the define() method can use the value of any expression as the value of a constant. Starting from PHP 5.6, const also allows expressions to be used as the value of constants.
const BIT_5 = 1 << 5; //PHP5.6后支持,之前的PHP版本不支持 define('BIT_5', 1 << 5);// 所有PHP版本都支持const 只允许简单的常量名,而define()可以把任何表达式的值用作常量名
for ($i = 0; $i < 32; $i++) { define('BIT_' . $i, 1 << $i); }const The constant names defined are case-sensitive, and case-insensitive constants can be defined by passing true to the third parameter of the define() method.
define('FOO', 'BAR', true); echo FOO; //BAR echo foo; //BARListed above are some shortcomings or inflexibility of const compared to define(). Let’s take a look at why I personally recommend using const instead of define() to define constants (unless you need to Define constants in the scenarios listed above). const has better readability, const is a language structure rather than a function, and is consistent with the form of defining class constants in a class. const defines constants in the current namespace, and define() must pass the complete namespace name when defining to achieve similar effects:
namespace A\B\C; //To define the constant A\B\C\FOO: const FOO = 'BAR'; define('A\B\C\FOO', 'BAR');Recommended learning: "
PHP Video tutorial》
The above is the detailed content of What is the way to define constants in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Public members can be accessed at will; 2. Private members can only be accessed within the class; 3. Protected members can be accessed in classes and subclasses; 4. Rational use can improve code security and maintainability.

Using MySQLi object-oriented method: establish a connection, preprocess UPDATE statements, bind parameters, execute and check the results, and finally close the resource. 2. Using MySQLi procedure method: connect to the database through functions, prepare statements, bind parameters, perform updates, and close the connection after processing errors. 3. Use PDO: Connect to the database through PDO, set exception mode, pre-process SQL, bind parameters, perform updates, use try-catch to handle exceptions, and finally release resources. Always use preprocessing statements to prevent SQL injection, verify user input, and close connections in time.

cURLinPHPenablessendingHTTPrequests,fetchingAPIdata,anduploadingfiles.Initializewithcurl_init(),setoptionslikeCURLOPT_URLandCURLOPT_RETURNTRANSFER,useCURLOPT_POSTforPOSTrequests,sendJSONwithproperheaders,handleerrorsviacurl_errno()andHTTPcodeswithcur

ToreadaCSVfileinPHP,usefopen()toopenthefile,fgetcsv()inalooptoreadeachrowasanarray,andfclose()tocloseit;handleheaderswithaseparatefgetcsv()callandspecifydelimitersasneeded,ensuringproperfilepathsandUTF-8encodingforspecialcharacters.

AJAXwithPHPenablesdynamicwebappsbysendingasynchronousrequestswithoutpagereloads.1.CreateHTMLwithJavaScriptusingfetch()tosenddata.2.BuildaPHPscripttoprocessPOSTdataandreturnresponses.3.UseJSONforcomplexdatahandling.4.Alwayssanitizeinputsanddebugviabro

isset()checksifavariableexistsandisnotnull,returningtrueevenforzero,false,oremptystringvalues;2.empty()checksifavariableisnull,false,0,"0","",orundefined,returningtrueforthese"falsy"values;3.isset()returnsfalsefornon-exi

Chrome bookmark editing is simple and practical. Users can enter the bookmark manager through the shortcut keys Ctrl Shift O (Windows) or Cmd Shift O (Mac), or enter through the browser menu; 1. When editing a single bookmark, right-click to select "Edit", modify the title or URL and click "Finish" to save; 2. When organizing bookmarks in batches, you can hold Ctrl (or Cmd) to multiple-choice bookmarks in the bookmark manager, right-click to select "Move to" or "Copy to" the target folder; 3. When exporting and importing bookmarks, click the "Solve" button to select "Export Bookmark" to save as HTML file, and then restore it through the "Import Bookmark" function if necessary.

Answer: Using the PHPMailer library to configure the SMTP server can enable sending mails through SMTP in PHP applications. PHPMailer needs to be installed, set up SMTP host, port, encryption method and authentication credentials of Gmail, write code to set sender, recipient, topic and content, enable 2FA and use application password to ensure that the server allows SMTP connection, and finally call the send method to send email.
