variable

Estimated reading time: 2 minutes

variable

  • start with $ sign
$name = "kamal";
echo $name;
echo "${name}";
// kamal
// kamal
scope variable
$name = "kamal";
function user(){
  $name = "jamal";
  echo $name;
}
user();
echo $name;
### jamal
### kamal
global
$name = "kamal";
function user(){
  global $name;
  $name = "jamal";
  echo $name;
}
users();
echo $name;
# jamal
# jamal
  • global alternative $GLOBAL[]
$name = "kamal";
function user(){
  $GLOBALS['name'] = "jamal";
  echo $name;
}
users();
echo $name;
# jamal
# jamal

static

~ static variable only call once

  • without
function amount(){
  $loan = 1000;
  echo "$loan\n";
  $loan = $loan + 200;
}
amount();
amount();
// 1000
// 1000

  • with static
function amount(){
  static $loan = 1000;
  echo "$loan\n";
  $loan = $loan + 200;
}
amount();
amount();
// 1000
// 1200

variables variable
$name = "kamal";
$who = "name";
echo $$who;
# kamal
# who => name => kamal
constant
  • outside of class
define("VERSION", "1.0.0");
echo VERSION;
# 1.0.0
  • inside class
const VERSION = "1.0.0";
echo VERSION;
# 1.0.0
php