{"id":233,"date":"2018-07-11T00:46:53","date_gmt":"2018-07-11T00:46:53","guid":{"rendered":"https:\/\/www.go4expert.com\/tutorials\/?p=233"},"modified":"2018-08-01T01:18:22","modified_gmt":"2018-08-01T01:18:22","slug":"php-constants","status":"publish","type":"post","link":"https:\/\/www.go4expert.com\/tutorials\/php-constants\/","title":{"rendered":"Constants in PHP"},"content":{"rendered":"<p>A constant is a name or an identifier for a simple fixed value. Constants are called so because once they are defined, they cannot be changed during the course of script execution.<\/p>\n<p>Due to their global scope PHP constants are usually used to store data that needs to have a global scope or whose value is bound to be constant.<\/p>\n<h2>Naming Conventions for PHP Constants<\/h2>\n<p>Constant names and variable names have similar naming conventions which can be summarized as follows:<\/p>\n<ol>\n<li>A valid constant name starts with a letter or an underscore, followed by any number of letters, numbers or underscores.<\/li>\n<li>The $ prefix is not required for constant names.<\/li>\n<li>Once a constant is defined, it can never be changed or undefined.<\/li>\n<\/ol>\n<p>Eg: Configuration settings such as database username and password, company name are defined as constants because their values will remain constant during the course of the program.<\/p>\n<p>Some examples of valid constant names are as follows :<br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">\n\/\/ Valid constant names because they start with a letter or an alphabet\ndefine(\"VALID_NAME_ONE\", \"first\");\ndefine(\"VALID_NAME_TWO2\", \"second one\");\ndefine(\"Three3\", \"third one\");\n<\/textarea><\/div><\/p>\n<p>Some examples of invalid constant names are as follows :<\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">\n\/\/ Invalid constant names because they start with a number and\u00a0 an underscore\ndefine(\"2TWO\", \"second \");\ndefine(\"__THREE_ _\", \"third\");\n<\/textarea><\/div>\n<h2>Define() Function<\/h2>\n<p>We can use the define() function to define a constant for which the syntax is :<\/p>\n<p>define(constant name, value , case-sensitivity)<\/p>\n<p>In which the constant name parameter is the name of the constant whereas the case-sensitivity parameter defines whether the constant is case-sensitive or not.<\/p>\n<p>Eg: define(\"php_message\", \"Learn PHP because PHP is fun!\", true);<\/p>\n<h2>Constant() function<\/h2>\n<p>We can retrieve the value of a constant by simply specifying its name. The constant() function is used in order to get the value of a constant or retrieve the constant name dynamically.<\/p>\n<p>constant() example<\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: PHP<\/div><textarea readonly=\"readonly\">\n&lt;?php\ndefine(\"onemeter\", 100);\necho onemetre;\necho constant(\"onemeter\"); \/\/ same thing as the previous line and hence the output is 100 for both statements\n?&gt;\n<\/textarea><\/div>\n<h2>Differences between constants and variables<\/h2>\n<ul>\n<li>There is no need to specify a dollar sign ($) before a constant, but a variable name should always be preceded by the $ symbol.<\/li>\n<li>Constants cannot be defined using a simple assignment like a variable because they can only be defined using the define() function<\/li>\n<li>They can be defined and accessed anywhere without variable scoping rules because they only have a global scope<\/li>\n<li>Constants are so called because once they have been defined they cannot be re-defined or undefined.<\/li>\n<\/ul>\n<h2>PHP Magic Constants<\/h2>\n<p>PHP has a large number of predefined constants which are called magic constants. They are called magic constants because they are pre-defined by default in any script and because the programmer can access their values at any time in the script.<\/p>\n<p>There are five magic constants whose values change due to the position where they are used. For eg, the value of '__LINE__' depends on the line that it's being used on in your PHP code.<\/p>\n<p>Magic constants are case-insensitive hence they can be used without caution. Some of the magic constants which are commonly used are as follows:<\/p>\n<ul>\n<li>__LINE__ - Contains the number of the lines in the PHP file which is currently being executed.<\/li>\n<li>__FILE__ - Contains the name of the PHP script file, which is currently being executed.<\/li>\n<li>__FUNCTION__ - Has the name of the PHP function which is currently being executed.<\/li>\n<li>__CLASS__ - Contains the class name which is currently in use<\/li>\n<li>__METHOD__ - Contains the name of the method which is currently executing in the current class<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This article provides an overview of constants, naming conventions, scope, constant functions and a clear understanding of the usage of constants.<\/p>\n","protected":false},"author":1,"featured_media":210,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":{"0":"post-233","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-php","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Constants in PHP - Go4Expert Tutorials<\/title>\n<meta name=\"description\" content=\"This article provides an overview of constants, naming conventions, scope, constant functions and a clear understanding of the usage of constants.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.go4expert.com\/tutorials\/php-constants\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constants in PHP - Go4Expert Tutorials\" \/>\n<meta property=\"og:description\" content=\"This article provides an overview of constants, naming conventions, scope, constant functions and a clear understanding of the usage of constants.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.go4expert.com\/tutorials\/php-constants\/\" \/>\n<meta property=\"og:site_name\" content=\"Go4Expert Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-11T00:46:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-01T01:18:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"770\" \/>\n\t<meta property=\"og:image:height\" content=\"385\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"shabbir\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"shabbir\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-constants\/\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/php-constants\/\",\"name\":\"Constants in PHP - Go4Expert Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\"},\"datePublished\":\"2018-07-11T00:46:53+00:00\",\"dateModified\":\"2018-08-01T01:18:22+00:00\",\"author\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6\"},\"description\":\"This article provides an overview of constants, naming conventions, scope, constant functions and a clear understanding of the usage of constants.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-constants\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.go4expert.com\/tutorials\/php-constants\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-constants\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.go4expert.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Constants in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/\",\"name\":\"Go4Expert Tutorials\",\"description\":\"Programming And Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6\",\"name\":\"shabbir\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g\",\"caption\":\"shabbir\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constants in PHP - Go4Expert Tutorials","description":"This article provides an overview of constants, naming conventions, scope, constant functions and a clear understanding of the usage of constants.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.go4expert.com\/tutorials\/php-constants\/","og_locale":"en_US","og_type":"article","og_title":"Constants in PHP - Go4Expert Tutorials","og_description":"This article provides an overview of constants, naming conventions, scope, constant functions and a clear understanding of the usage of constants.","og_url":"https:\/\/www.go4expert.com\/tutorials\/php-constants\/","og_site_name":"Go4Expert Tutorials","article_published_time":"2018-07-11T00:46:53+00:00","article_modified_time":"2018-08-01T01:18:22+00:00","og_image":[{"width":770,"height":385,"url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","type":"image\/jpeg"}],"author":"shabbir","twitter_card":"summary_large_image","twitter_misc":{"Written by":"shabbir","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.go4expert.com\/tutorials\/php-constants\/","url":"https:\/\/www.go4expert.com\/tutorials\/php-constants\/","name":"Constants in PHP - Go4Expert Tutorials","isPartOf":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#website"},"datePublished":"2018-07-11T00:46:53+00:00","dateModified":"2018-08-01T01:18:22+00:00","author":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6"},"description":"This article provides an overview of constants, naming conventions, scope, constant functions and a clear understanding of the usage of constants.","breadcrumb":{"@id":"https:\/\/www.go4expert.com\/tutorials\/php-constants\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.go4expert.com\/tutorials\/php-constants\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.go4expert.com\/tutorials\/php-constants\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.go4expert.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Constants in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.go4expert.com\/tutorials\/#website","url":"https:\/\/www.go4expert.com\/tutorials\/","name":"Go4Expert Tutorials","description":"Programming And Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6","name":"shabbir","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g","caption":"shabbir"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9JSSi-3L","_links":{"self":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":17,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/233\/revisions\/362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}