Dev Genius

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

Follow publication

Member-only story

PHP — P14: Constants

--

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.

--

--

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