Answers for "php set global variables from function"

PHP
1

php global variable function

<?php
  $myVariable = "a";
  function changeVar($newVar) {
    global $myVariable
    $myVariable = "b";
  }
  echo $myVariable; // Should echo b
?>
Posted by: Guest on May-16-2021
0

php set global variables from function

<?php 
function setGlobalVariable() {
    $GLOBALS['variable_name'] = "Some Value";
}
setGlobalVariable();
echo $variable_name;
// Outputs: Some Value
Posted by: Guest on September-10-2021
0

How to not use directly global variables in PHP

<?php
$search_html = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS);
$search_url = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_ENCODED);
echo "Vous avez recherché $search_html.\n";
echo "<a href='?search=$search_url'>Nouvelle recherche.</a>";
?>
Posted by: Guest on April-19-2021

Code answers related to "php set global variables from function"

Browse Popular Code Answers by Language