AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP Constants


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP Constants

What is a PHP constant?

PHP constants are names or identifiers that cannot be changed during script execution

Constants are similar to the variable, except that, once defined, they can never be undefined or modified. These remain constant throughout the program.

Constants does not need $ in front.
But, as a convention of PHP programmers, constants are written in capital letters (A-Z) for easier observation.

To create a constant, use the define() function.

Syntax:

<?php
define(name, value, case-insensitive)}
?>

Where:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive:

 

Constant with a case-insensitive name example

to create a constant with a case-sensitive name:

Example (this is HTML - PHP editor, change text on this window)
<?php

define("MESSAGES", "Welcome to agernic.com, have a nice day.");
echo MESSAGES;

?>

Create a constant with a case-insensitive name:

Example (this is HTML - PHP editor, change text on this window)
<?php

define("MESSAGES", "Welcome to agernic.com, have a nice day.", true);
echo messages;

?>

 

Related subjects:
PHP foreach What is an array Convert array to string comma Array to comma separated

Tags: php constant array , php constant object, php define constant, php constant variable in class, php const vs define, php 7 constants, use php constant in javascript,
php constant in string,
What is a PHP constant?
How do you call a constant in PHP?
Where do I put constants in PHP?
What is PHP magic constant?

 

PHP Constant Arrays

In PHP7, we can create an Array constant using function define().

Example (this is HTML - PHP editor, change text on this window)
<?php
define("colors", [
"red",
"green",
"blue"
]);
echo colors[2];
?>

 

Constants are Global

PHP constants are automatically global and can be used throughout the script.

Example (this is HTML - PHP editor, change text on this window)

<?php

define("MESSAGES", "Welcome to agernic.com, have a nice day.");
function my_Test() {
echo MESSAGES;
}

my_Test();
?>

 



php constant array , php constant object, php define constant, php constant variable in class, php const vs define, php 7 constants, use php constant in javascript,
php constant in string,
What is a PHP constant?
How do you call a constant in PHP?
Where do I put constants in PHP?
What is PHP magic constant?

How do you call a constant in PHP? - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...