Member-only story

PHP — P14: Constants

Dino Cajic
Dev Genius
Published in
3 min readJul 4, 2020

PHP Constants

Constants are similar to variables except that they cannot change. They can’t change since constants are stored in the read-only-data segment. Why would you not want to keep data constant? A good example is when you want to store database connection information. You do not want to accidentally change the connection information somewhere later in your code and accidentally sever the database connection.

View this article and others on my website.

How do we define constants in our code? With the define function. define() accepts two arguments:

  1. The first argument is the constant name.
  2. The second argument is the constant value.
<?phpdefine("DB_USERNAME", "dances_with_squirrels");
define("DB_PASSWORD", "hakuna_matata");
?>

Constants names are by convention declared using all uppercase characters. This is purely so that the person that’s looking at your code, yourself included, can see effortlessly which values are constants.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Dino Cajic

Author of An Illustrative Introduction to Algorithms. IT Leader with a B.S. in Computer Science, a minor in Biology, and a passion for learning.

No responses yet

Write a response