xxxxxxxxxx
$a = 1;
$b = 2;
function Sum(){
global $a, $b; // allows access to vars outside of function
$b = $a + $b;
}
Sum();
echo $b; // output is 3
xxxxxxxxxx
<?php
function setGlobalVariable() {
$GLOBALS['variable_name'] = "Some Value";
}
setGlobalVariable();
echo $variable_name;
// Outputs: Some Value
xxxxxxxxxx
$GLOBALS['a'] = 'localhost';
function body(){
echo $GLOBALS['a'];
}
xxxxxxxxxx
<?php
// Creating a global variable
$globalVar = "I'm a global variable!";
function myFunction() {
// Accessing the global variable within a function
global $globalVar;
// Modifying the global variable
$globalVar = "Modified global variable!";
}
// Initially, printing the global variable
echo $globalVar; // Output: I'm a global variable!
// Calling the function to modify the global variable
myFunction();
// Printing the modified global variable
echo $globalVar; // Output: Modified global variable!
?>
xxxxxxxxxx
<?php
$myVariable = "a";
function changeVar($newVar) {
global $myVariable
$myVariable = "b";
}
echo $myVariable; // Should echo b
?>
xxxxxxxxxx
$globalVariable = "Hello, world!";
function myFunction() {
global $globalVariable;
echo $globalVariable;
}
myFunction(); // Output: Hello, world!
xxxxxxxxxx
const { removeToast } = useToast();
removeToast(toastId); // Removes the toast with the given id
// if you want to delete by index
removeToast.byIndex(0); // Removes the first toast