PHP Constant Variable

  • Hi guys, in this tutorial we will learn PHP Constant Variable, defining constant, php constant array, php constant variable, php constant function, php const keyword.
  • A constant is a name or identifier for a fixed value. Constant variables are like, once defined, they cannot be obscured or changed.
  • Constants are very useful for storing changing data while the script is running. Common examples of such data include configuration settings such as database username and password, base url, locations etc.
  • php constant name valid starts with a letter or underscore (no $ sign before the constant name).

PHP Syntax

<?php
	define(name, value, case-insensitive);
  
 /*
  name: It specifies the constant name.
  value: It specifies the constant value.
  case-insensitive: Specifies whether it is not static case-sensitive. The default value is incorrect. That means it is basically case sensitive. Let's look at an example for a PHP static definition using defined ().
  */
?>

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
  // Defining constant
  define("BASE_URL", "https://webthestuff.com");
 
  // Using constant
  echo 'Thank you for visiting - ' . base_url; // case-insensitive
?>


</body>
</html>

30

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
   define("weekend", [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
  ]);
  echo "Today is a ".weekend[0];
?>


</body>
</html>

Today is a Monday

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
  define("BASE_URL", "https://webthestuff.com");

  function mywebsite() {
    echo BASE_URL;
  }
 
  mywebsite();
?>


</body>
</html>

https://webthestuff.com

  • Previous
  • Next