0

i'm in need of a php function whose first argument will check that the variable exist or not outside of the function, if variable exist then echo it's value and if the variable doesn't exist then echo a default value for the variable given in second argument of the function. and in last remove(delete) both the variable passed to the function just after echoing their values.

simply :

function if_exists ($argument, $default)
{
// if $argument exist then echo it's value and then remove $argument variable.
// if the $argument doesn't exist then echo it's $default value and then remove $default variable.
}

i will use it like this :

$any_variable
if_exists ($any_variable, 'this variable is not defined');

this code is not doing the perfect job for me :

function if_exist(&$argument, $default = '')
{
    if (isset ($argument))
    {
        echo $argument;
    }
    else
    {
        echo $default;
        unset ($default);
    }
}

thanks.

13
  • Why do you need it? How is this different from: stackoverflow.com/questions/6654538/… -- also: You cannot "delete" a variable in a different scope (except for the global scope). Commented Jul 12, 2011 at 8:58
  • that function doesn't work right for me, i need exactly the same thing i said in this question, please guide me if you can, i will appreciate your help, that code is bullshit for me, i need some fresh code of you guys. Commented Jul 12, 2011 at 9:02
  • You will confuse yourself in (lets say) about 2 months, if a function called if_exists() outputs something and additional change the given arguments. Commented Jul 12, 2011 at 9:03
  • Pawan: In that case, update your question with code you have, that doesn't work. Tell us, what it does that you don't want it to, or rather: what you would like to have it do differently. Commented Jul 12, 2011 at 9:06
  • 1
    And what isn't it doing for you? Note: You do not need to unset $default. That serves no purpose. Again: You are not telling us what you're trying to do, you are asking for a solution to make your solution work. Commented Jul 12, 2011 at 9:16

1 Answer 1

2

You don't need a function for this

$var = isset($var) ? $var : $default;
Sign up to request clarification or add additional context in comments.

7 Comments

" and in last remove(delete) both the variable passed to the function just after echoing their values." See my answer to a slightly different question of his: stackoverflow.com/questions/6643692/…
hi king, can you wrap you code in a fucntion, it will be a great help. thanks.
@Pawan: I have given you the exact same idiom (as linked above). It's not in a function because it shouldn't be in a function. I doubt KingCrunch will put it into a function for you.
Thats a one-liner and its already a simple operator of its own :? Why you need a function for it? Creating a function would mean, that you make just an alias.
yup i want to have a alias for it, please try to understand.
|

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.