2
<?php

function myFunction($yesNname) { } 

empty($noName);
print_r($noName);
isset($noName);

myFunction($noName);

output:

PHP Notice:  Undefined variable: noName ... on line 6 // print_r
PHP Notice:  Undefined variable: noName ... on line 9 // myFunction

The undefined variable is used in empty() and isset(). But PHP didn't issue notice about it. Why PHP shows discrimination to some function? How can I write such type of function?

4
  • What'd the point of having a function to test whether a variable exist, and make it trigger an error message when it doesn't exist? Commented Dec 14, 2012 at 10:52
  • @ÁlvaroG.Vicario I can understand that. I want to know how can I write such type of function. Commented Dec 14, 2012 at 10:55
  • Why would you want that? Could you please sketch out the situation? Commented Dec 14, 2012 at 10:58
  • @giorgio I thought there will be some technique in PHP to do that. Commented Dec 14, 2012 at 11:07

3 Answers 3

3

Neither isset() nor empty() are functions. As the manual explains:

this is a language construct and not a function

To get this behaviour you'd need to tweak the PHP source code written in C.

It's possible that you can also get this behaviour with a PHP extension, but you'd also need to write it in C and install it in your server.

Update:

Sign up to request clarification or add additional context in comments.

Comments

3

Did you mean like:

if(!empty($noName)) {
 // print_r
 // function($noName);
}

5 Comments

Why should I put the condition for calling a function? did you see any such condition around the isset() or empty() in my code?
Please note that isset is not needed in conjunction with empty (empty does already return false if a var does not exist)
take it easy @habeebperwad, he's trying to help you... and is right actually
@giorgio He tried to solve the PHP notice! He didn't tried to answer my question " How can I write such type of function?". Anyway, +1 for considering my question.
Someone already mentioned it: you can't. And why should you, anyway? The functions are already there, and atomic to boot. Why would you want to replicate something you can just use?
2

There is no way to do it on function side. If you simply don't want to show errors you can either check variable first before calling function or use error control operator "@".

Comments

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.