0

this function does not return the result, what could I be doing wrong?

<?php

function iisset($name){
    return ${$name}; // "${$name}" does not work
}
$hola = 1;
echo iisset("hello");

Note: it works correctly if it is not in a function

1

1 Answer 1

1

You have a few issues going on here. The first is an issue of scope. Nothing outside the function iisset($name) is normally visible to it. Therefore any variable defined outside iisset($name) can't been seen.

So the first step would be to make it global by adding global ${$name}; to the first line after the function declaration. The second would be that there is no variable named "hello" outside the function. If you are trying to access the $hola then I would recommend the following:

<?php
function iisset($name){
    global ${$name};
    return ${$name}; // "${$name}" does not work
}
$hola = 1;
echo iisset("hola");
Sign up to request clarification or add additional context in comments.

1 Comment

Incredible, with the global the problem is solved, thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.