P
P
PO6OT2015-06-18 12:14:51
PHP
PO6OT, 2015-06-18 12:14:51

How can a function import a variable into the current symbol table, but not into the global one?

<?php

 //Это пример:
 //Уровень первый, глобальный
 ch_frst_lvl_var(){
  $GLOBALS['lvl_1']='false';
 }
 $lvl_1='true'; 
 ch_frst_lvl_var();
 echo 'lvl_1: '.$lvl_1;
 //Выводит "lvl_1: false"


 //Это задача:
 //Уровень второй, внутри функции
 ch_scnd_lvl_var(){
  $lvl_2='false';
 //Что здесь надо дописать, чтоб функция foo() вывела "lvl_2: false" ?
 }
 function foo(){
  $lvl_2='true';
  ch_scnd_lvl_var();
  echo 'lvl_2: '.$lvl_2;
 }
 foo();
 //Выводит "lvl_2: true", а надо "lvl_2: false"
?>

How can I use the ch_scnd_lvl_var() function to import the $lvl_2 variable into the current symbol table (extract() somehow can)?
For the gifted:
How to make the task work similarly to the example?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cat Anton, 2015-06-18
@woonem

<?php

function ch_scnd_lvl_var(&$var)
{
    $var = 'false';
}

function foo()
{
    $lvl_2 = 'true';
    ch_scnd_lvl_var($lvl_2);
    echo 'lvl_2: ' . $lvl_2;
}
foo();

?>

M
magazovski, 2015-07-17
@magazovski

Elementary Watson!

function ch_scnd_lvl_var()
{
    $lvl_2 = 'false';
    echo 'lvl_2: '.$lvl_2;
    die();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question