PHP constants

php constants in hindi

constants बी variable की तरह होता है लेकिन cosntant को एकबार define करने के बाद उसको change या फिरसे define नहीं कर सकते||
variable का storage temparory है जब की constants permanednt है||
constant का इस्तेमाल data के लिए होता है पुरे program script में unchangeable है||
सभी constants का scope global होता है लेकिन उसको function के अंदर में define किया जाता है||
constants name letters और underscore के साथ start होता है||
variable की तरह constant name के पहले $ sign का इस्तेमाल नहीं किया जाता.

constants declaration in php

define() function के उपयोग से constants define कर सकते है|| define() funciton में three argument है||

define syntax:

define(name,value,case-insensitive);

parameters:
name : constant का name specifies करता है||
value : constant का value specifies करता है||
case-insensitive : यह specifies करता है कि constant name case-insensitive होना चाहिए या नहीं default false होता है|

valid constant declaration

Example:

<?php

  //valid constant name
  define(‘constantname’, “welcome to RjtechyG”);
  echo constantname;

?>

Invalid constant name

Example:

<?php

//invalid constant name
define(‘3constantname’,”welcome to RjtechyG”);
define(‘ constantname’,”welcome to RjtechyG”);
define(‘@constantname’,”welcome to RjtechyG”);

?>

constant name की सरुआत में हम number, space या special character के साथ start नहीं कर सकते.

PHP constants with case-insensitive

define('constantname',"values",true);

Example:

<?php

  define(‘constantname’,”welcome to RjtechyG”,true);
  echo constantname;
  echo CONSTANTNAME;
  echo coNSTANname;

?>

Create constant and asign value

Example:

<?php

 define('constantname',"rjtechyg");
 echo constantname;

?>

ऊपर के example में देख सकते है की define() function का इस्तेमाल करके हमने constant name define किया है जिसमे first argument constant के नाम के लिए है और दूसरा argument उसकी value = “rjtechyg” और इस value को print करने के लिए constant name से print करा सकते है||

Constanta are global

constants define करने के बाद से constant automatally global हो जाता है script में constant का इस्तेमाल कही बी किया जा सकता है||

Example:

<?php

define(‘constantname’,”Hello world”);
function test(){
 echo constantname;
}
test();

?>

Php constant arrays

define() function के इस्तेमाल से constant array create कर सकते है||

Example:

<?php

define(“fruits”,[
“apple”,
“banana”,
“oranges”
]);
echo $fruits[0];

?>

Leave a Comment

Your email address will not be published. Required fields are marked *