PHP Lesson 25 – Constant – PHP Tutorials

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

PHP Lesson 25 – Constant - PHP Tutorials - PHP Lesson 25 – Constant - PHP Tutorials - PHP Lesson 25 – Constant - PHP Tutorials - php constant
php constant

The value of a constant is, unlike a variable, no longer changeable once it has been defined. This is useful, for example, to define fixed, immutable values, such as the maximum length of a post or the version of your script.

Constant Syntax – PHP Tutorial

To define a constant, you use the keyword const, You can then access the constant similar to variables, but without the dollar sign in front of the name.

<?Php
const VERSION = "1.0.5";
const MAX_EMAIL_LENGTH = 10;

$email = "info@coding180.com";

echo "This is Version" .VERSION;

if(strlen($email) > MAX_EMAIL_LENGTH) {
   echo "<br /> The e-mail may not be longer than" .MAX_EMAIL_LENGTH. "characters.";
}
?>

n the above example, two constants have been defined, VERSION and MAX_EMAIL_LENGTH. It has become commonplace that constants always consist of capital letters, but this is not an obligation.

You can access them in a similar way as you can to variables, only the opposite of variables cannot change their value. If you try to assign constants a new value, you will receive a corresponding error message. So you can be sure that in no other place in the code is the value of the constant MAX_EMAIL_LENGTH changed.

Prior to PHP 5.6, constants can only contain simple data types (boolean, integer, float, and string). As of PHP 5.6, it is also possible to store arrays in constants.

class constants – PHP Tutorial

In object-oriented programming, you can also define class constants that are valid only in combination with a class:

<?php
class User {
	const VERSION = "1.0.5";
	const MAX_EMAIL_LENGTH = 10;

	public function checkEmail($email) {
		if(strlen($email) > self::MAX_EMAIL_LENGTH) {
			return false;
		}
		return true;
	}
}

$user = new User ();
echo "The maximum length is" .User :: MAX_EMAIL_LENGTH. "<br />";

if (!$user-> checkEmail ("info@coding180.com")) {
echo "The e-mail address is too long";
}
?>

These constants you can then either by class name:: NAME_OF_THE_CONSTANT access or methods within the class using self:: NAME_OF_THE_CONSTANT.

 

Predefined Constants – PHP Tutorial

PHP has several predefined constants that can be useful depending on the application. For example, the PHP version can be found using PHP_VERSION. All predefined constants can be found in the PHP manual – Predefined Constants.

<?php
echo "Current PHP Version:" .PHP_VERSION;
echo "<br /> Server operating system:" .PHP_OS;
echo "<br /> Maximum size for an integer:" .PHP_INT_SIZE;
?>

Magic constants – PHP Tutorial

There are also magical constants in PHP. Unlike ordinary constants, these change their value depending on where you use them in the program code. An example is the magic constant __LINE__, which always indicates the current line in your program code. All magic constants can be found in the PHP manual – Magic Constants.

<?php
echo "Current program line:" .__ LINE__;
echo "<br /> Current program line:" .__ LINE__;
echo "<br /> Current filename:" .__ FILE__;
echo "<br /> Current directory:" .__ DIR__;
?>

 

About The Author

Related posts

Leave a Reply