understanding PHP constants

The value of a constant can’t be reassigned. Constants do not have a $ before their names, and by convention the names of constants are in uppercase letters. Constants can contain only scalar values (no arrays or objects). Constants have global scope, so they are accessible everywhere in your scripts after they have been defined – even inside functions.

You can create a constants using define(). Note however that constants are defined in a different way insides classes.

define(A_CONST, 18);

It is best to define your constants in an include file.

There are some constants which are defined by PHP. However, that is the subject of a different post.

horizontal line celtic gif

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

What is defined()?

bool defined ( string $name )

Checks whether the given constant exists and is defined.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in Coding, PHP Constants and tagged . Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s