• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Constants

PHP Constants

25 May 2009 / Leave a Comment

You might be familiar with using PHP variables to store values in PHP scripts. PHP constants are similar, but have a few important differences, as you’ll see in this article.

PHP constants explained

Like variables, PHP constants can store values. However, once a constant has been created, its value cannot be changed while the script runs.

Constants are useful for storing data that doesn’t (and shouldn’t) change while the script is running. Common examples of such data include configuration settings (such as database usernames and passwords) and fixed strings of text to display to visitors (such as “Please login to continue”).

Constants are also often used to represent integer values with special meanings in a particular context, such as error codes and flags.

How to create a constant

PHP constant names follow the same rules as PHP variable names. The only difference is that constant names don’t start with a $ (dollar) symbol, while variable names do.

PHP constant names are case-sensitive. Usually, constant names use all-uppercase letters, with underscores to separate words within the name.

To create a PHP constant, use the following syntax:


define( "CONSTANT_NAME", constant_value );

“CONSTANT_NAME” is a string holding the name of the constant to create, while constant_value is the value that the constant will hold. Here are a couple of examples:


define( "HELLO", "Hello, world!" );
define( "WIDGET_PRICE", 29.99 );

The first line of code creates a PHP constant called HELLO with a string value of “Hello, world!”, while the second line creates a constant called WIDGET_PRICE with a numeric value of 29.99.

PHP constants can only hold scalar values — these include numbers, strings of text, and boolean values. They can’t hold arrays or objects, like variables can.

You can only define a constant once within a script. If you try to redefine a constant, the PHP engine generates an error.

Using constants in PHP

You access a PHP constant in exactly the same way as you use a variable. To use a constant’s value, simply write the constant name. The following example displays the values of the HELLO and WIDGET_PRICE constants:


echo HELLO;
echo WIDGET_PRICE;

Predefined constants

PHP features a large number of built-in, predefined constants holding various useful values. Some of these are always available, while other constants become available when certain PHP extensions are enabled.

For example, the PHP constant M_PI holds the mathematical constant Pi. The following code displays this value:


echo M_PI;

Another useful predefined constant is PHP_VERSION, which holds the current version of the running PHP engine:


echo PHP_VERSION;

The PHP website has a list of built-in predefined constants.

You now know what PHP constants are, as well as how to create constants and use them. Happy coding!

Filed Under: PHP Tagged With: constants in php, creating php constants, php basics, php constants, php define, php predefined constants, php tutorial

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits