How to use global variables in PHP

2 Answers

0 votes
$first_name = "Filey";
$last_name = "Dump";
 
function concat() {
    $GLOBALS['full_name'] = $GLOBALS['first_name'] . " " . $GLOBALS['last_name'];
}
 
concat();

echo $full_name . "\n";
echo $GLOBALS['full_name'];




/*
run:

Filey Dump
Filey Dump

*/

 



answered Jun 9, 2023 by avibootz
0 votes
$full_name = "";
  
function concat() {
    $first_name = "Filey";
    $last_name = "Dump";

    global $full_name;
    
    $full_name = $first_name . " " . $last_name;
}
  
concat();
 
echo $full_name;

 
 
 
 
/*
run:
 
Filey Dump

*/

 



answered Dec 3, 2023 by avibootz

Related questions

1 answer 174 views
1 answer 156 views
1 answer 170 views
170 views asked Sep 29, 2020 by avibootz
1 answer 218 views
3 answers 214 views
214 views asked Oct 17, 2015 by avibootz
1 answer 232 views
...