<p>I'm pretty sure the answer to this question is no, but in case there's some PHP guru</p>

<p>is it possible to write a function in a way where invalid arguments or non existent variables can be passed in and php will not error without the use of <code>'@'</code></p>

<p>Much like empty and isset do. You can pass in a variable you just made up and it won't error.</p>

<pre><code>ex:

empty($someBogusVar); // no error

myHappyFunction($someBogusVar); // Php warning / notice
</code></pre>

[{'answer_id': 55065, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 1, 'selected': False, 'text': '<p>No, because this isn\'t really anything to do with the function; the error is coming from attempting to de-reference a non-existent array key. You can change the warning level of your PHP setup to surpress these errors, but you\'re better off just not doing this.</p>\n\n<p>Having said that, you <em>could</em> do something like</p>\n\n<pre><code>function safeLookup($array, $key)\n{\n  if (isset($array, $key))\n    return $array[$key];\n\n  return 0;\n}\n</code></pre>\n\n<p>And use it in place of array key lookup</p>\n\n<pre><code>defaultValue(safeLookup($foo, "bar"), "baz);\n</code></pre>\n\n<p>Now I need to take a shower :)</p>\n'}, {'answer_id': 55079, 'author': 'SeanDowney', 'author_id': 5261, 'author_profile': 'https://Stackoverflow.com/users/5261', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>@Brian: I use a trinary operation to do the check for me:</p>\n</blockquote>\n\n<pre><code>return $value ? $value : $default;\n</code></pre>\n\n<p>this returns either $value OR $default. Depending upon the value of $value. If it is 0, false, empty or anything similar the value in $default will be returned.</p>\n\n<p>I'm more going for the challenge to emulate functions like empty() and isset() </p>\n"}, {'answer_id': 55090, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': -1, 'selected': False, 'text': "<p>And going further up the abstraction tree, what are you using this for?</p>\n\n<p>You could either initialize those values in each class as appropriate or create a specific class containing all the default values and attributes, like:</p>\n\n<pre><code>class Configuration {\n\n    private var $configValues = array( 'cool' =&gt; 'Defaultcoolval' ,\n                                       'uncool' =&gt; 'Defuncoolval'  );\n\n    public setCool($val) {\n        $this-&gt;configValues['cool'] = $val;\n    }\n\n    public getCool() {\n        return $this-&gt;configValues['cool'];\n    }\n\n}\n</code></pre>\n\n<p>The idea being that, when using defaultValue function everywhere up and down in your code, it will become a maintenance nightmare whenever you have to change a value, looking for all the places where you've put a defaultValue call. And it'll also probably lead you to repeat yourself, violating DRY.</p>\n\n<p>Whereas this is a single place to store all those default values. You might be tempted to avoid creating those setters and getters, but they also help in maintenance, in case it becomse pertinent to do some modification of outputs or validation of inputs.</p>\n"}, {'answer_id': 55100, 'author': 'SeanDowney', 'author_id': 5261, 'author_profile': 'https://Stackoverflow.com/users/5261', 'pm_score': 0, 'selected': False, 'text': "<p>I'm sure there could be a great discussion on ternary operators vrs function calls. But the point of this question was to see if we can create a function that won't throw an error if a non existent value is passed in without using the '@'</p>\n"}, {'answer_id': 55127, 'author': 'reefnet_alex', 'author_id': 2745, 'author_profile': 'https://Stackoverflow.com/users/2745', 'pm_score': 2, 'selected': False, 'text': '<p>You can do this using func_get_args like so:</p>\n\n<pre><code>error_reporting(E_ALL);\nini_set(\'display_errors\', 1);\n\nfunction defaultValue() {\n    $args = func_get_args();\n\n    foreach($args as $arg) {\n        if (!is_array($arg)) {\n            $arg = array($arg);\n        }\n        foreach($arg as $a) {\n            if(!empty($a)) {\n                return $a;\n            }\n        }\n    }\n\n    return false;\n}\n\n$var = \'bob\';\n\necho defaultValue(compact(\'var\'), \'alpha\') . "\\n"; //returns \'bob\'\necho defaultValue(compact(\'var2\'), \'alpha\') . "\\n"; //returns \'alpha\'\necho defaultValue(\'alpha\') . "\\n"; //return\necho defaultValue() . "\\n";\n</code></pre>\n\n<p>This func goes one step further and would give you the first non empty value of any number of args (you could always force it to only take up to two args but this look more useful to me like this).</p>\n\n<p>EDIT: original version didn\'t use compact to try and make an array of args and STILL gave an error. Error reporting bumped up a notch and this new version with compact is a little less tidy, but still does the same thing and allows you to provide a default value for non existent vars. </p>\n'}, {'answer_id': 55128, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 0, 'selected': False, 'text': '<p>@Sean That was already answered by Brian</p>\n\n<pre><code>return isset($input) ? $input : $default;\n</code></pre>\n'}, {'answer_id': 55133, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 0, 'selected': False, 'text': "<p>Sean, you could do:</p>\n\n<pre><code>$result = ($func_result = doLargeIntenseFunction()) ? $func_result : 'no result';\n</code></pre>\n\n<p>EDIT:</p>\n\n<blockquote>\n  <p>I'm sure there could be a great\n  discussion on ternary operators vrs\n  function calls. But the point of this\n  question was to see if we can create a\n  function that won't throw an error if\n  a non existent value is passed in\n  without using the '@'</p>\n</blockquote>\n\n<p>And I told you, check it with <code>isset()</code>. A ternary conditional's first part doesn't check null or not null, it checks <code>true</code> or <code>false</code>. If you try to check <code>true</code> or <code>false</code> on a null value in PHP, you get these warnings. <code>isset()</code> checks whether a variable or expression returns a null value or not, and it returns a boolean, which can be evaluated by the first part of your ternary without any errors.</p>\n"}, {'answer_id': 55191, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 4, 'selected': True, 'text': '<p>Summing up, the proper answer is <strong>no, you shouldn\'t</strong> (see caveat below). </p>\n\n<p>There are workarounds already mentioned by many people in this thread, like using reference variables or isset() or empty() in conditions and suppressing notices in PHP configuration. That in addition to the obvious workaround, using @, which you don\'t want.</p>\n\n<p>Summarizing an interesting comment discussion with <a href="https://stackoverflow.com/users/109561/gerry">Gerry</a>: Passing the variable by reference is indeed valid if you <strong>check for the value of the variable inside the function</strong> and handle undefined or null cases properly. Just don\'t use reference passing as a way of shutting PHP up (this is where my original shouldn\'t points to).</p>\n'}, {'answer_id': 311954, 'author': 'Kornel', 'author_id': 27009, 'author_profile': 'https://Stackoverflow.com/users/27009', 'pm_score': 4, 'selected': False, 'text': "<p>You don't get any error when a variable is passed by reference (PHP will create a new variable silently):</p>\n\n<pre><code> function myHappyFunction(&amp;$var)\n {       \n }\n</code></pre>\n\n<p>But I recommend against abusing this for hiding programming errors.</p>\n"}, {'answer_id': 504472, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>While the answer to the original question is "no", there is an options no one has mentioned.</p>\n\n<p>When you use the @ sign, all PHP is doing is overriding the <code>error_reporting</code> level and temporarily setting it to zero. You can use "<code>ini_restore(\'error_reporting\');</code>" to set it back to whatever it was before the @ was used.</p>\n\n<p>This was useful to me in the situation where I wanted to write a convenience function to check and see if a variable was set, and had some other properties as well, otherwise, return a default value. But, sending an unset variable through caused a PHP notice, so I used the @ to suppress that, but then set <code>error_reporting</code> back to the original value inside the function.</p>\n\n<p>Something like:</p>\n\n<pre><code>$var = @foo($bar);\n\nfunction foo($test_var)\n{\n     ini_restore(\'error_reporting\');\n\n     if(is_set($test_var) &amp;&amp; strlen($test_var))\n     {\n          return $test_var;\n     }\n     else\n     {\n          return -1;\n     }\n}\n</code></pre>\n\n<p>So, in the case above, if <code>$bar</code> is not set, I won\'t get an error when I call <code>foo()</code> with a non-existent variable. However, I will get an error from within the function where I mistakenly typed <code>is_set</code> instead of <code>isset</code>.</p>\n\n<p>This could be a useful option covering what the original question was asking in spirit, if not in actual fact.</p>\n'}, {'answer_id': 884770, 'author': 'Gerry', 'author_id': 109561, 'author_profile': 'https://Stackoverflow.com/users/109561', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>is it possible to write a function in a way where invalid arguments or non existent variables can be passed in and php will not error without the use of \'@\'</p>\n</blockquote>\n\n<p><strong>Yes you can!</strong></p>\n\n<p>porneL is correct [edit:I don\'t have enough points to link to his answer or vote it up, but it\'s on this page]</p>\n\n<p>He is also correct when he cautions "But I recommend against abusing this for hiding programming errors." however error suppression via the Error Control Operator (@) should also be avoided for this same reason.</p>\n\n<p>I\'m new to Stack Overflow, but I hope it\'s not common for an incorrect answer to be ranked the highest on a page while the correct answer receives no votes. :(</p>\n'}, {'answer_id': 931565, 'author': 'BraedenP', 'author_id': 95764, 'author_profile': 'https://Stackoverflow.com/users/95764', 'pm_score': 0, 'selected': False, 'text': '<p>If you simply add a default value to the parameter, you can skip it when calling the function. For example:</p>\n\n<pre><code>function empty($paramName = ""){\n    if(isset($paramName){\n        //Code here\n    }\n    else if(empty($paramName)){\n        //Code here\n    }\n}\n</code></pre>\n'}, {'answer_id': 1867434, 'author': 'Bob Fanger', 'author_id': 19165, 'author_profile': 'https://Stackoverflow.com/users/19165', 'pm_score': 2, 'selected': False, 'text': "<p>There are valid cases where checking becomes cumbersome and unnessesary.<br>\nTherfore i've written this little magic function: </p>\n\n<pre><code>/**\n * Shortcut for getting a value from a possibly unset variable.\n * Normal:\n *   if (isset($_GET['foo']) &amp;&amp; $_GET['foo'] == 'bar') {\n * Short:\n *   if (value($_GET['foo']) == 'bar') {\n *\n * @param mixed  $variable \n * @return mixed  Returns null if not set\n */\nfunction value(&amp;$variable) {\n    if (isset($variable)) {\n        return $variable;\n    }\n}\n</code></pre>\n\n<p>It doesn't require any changes to myHappyFunction().<br>\nYou'll have to change</p>\n\n<pre><code>myHappyFunction($someBogusVar);\n</code></pre>\n\n<p>to</p>\n\n<pre><code>myHappyFunction(value($someBogusVar));\n</code></pre>\n\n<p>Stating your intent explicitly. which makes it <strong>good practice</strong> in my book.</p>\n"}, {'answer_id': 10890296, 'author': 'Omar', 'author_id': 931377, 'author_profile': 'https://Stackoverflow.com/users/931377', 'pm_score': 0, 'selected': False, 'text': '<p>With a single line, you can acomplish it: <code>myHappyFunction($someBogusVar="");</code></p>\n\n<p>I hope this is what you are looking for. If you read the php documentation, under <a href="http://php.net/manual/en/functions.arguments.php" rel="nofollow">default argument values</a>, you can see that assigning a default value to an function\'s argument helps you prevent an error message when using functions.</p>\n\n<p>In this example you can see the difference of using a default argument and it\'s advantages:</p>\n\n<p><strong>PHP code:</strong></p>\n\n<pre><code>&lt;?php\nfunction test1($argument)\n{\n    echo $argument;\n    echo "\\n";\n}\n\nfunction test2($argument="")\n{\n    echo $argument;\n    echo "\\n";\n}\n\ntest1();\ntest1("Hello");\ntest1($argument);\n$argument = "Hello world";\ntest1($argument);\n\ntest2();\ntest2("Hello");\ntest2($argument);\n$argument = "Hello world";\ntest2($argument);\n?&gt;\n</code></pre>\n\n<p><strong>Output for <code>test1()</code> lines:</strong></p>\n\n<blockquote>\n  <p><b>Warning</b>:  Missing argument 1 for test1() .<br/>\n  Hello. <br/>\n  . <br/>\n  Hello world.</p>\n</blockquote>\n\n<p><strong>Output for <code>test2()</code> lines:</strong></p>\n\n<blockquote>\n  <p>. <br/>\n  Hello. <br/>\n  <br/>\n  Hello world.</p>\n</blockquote>\n\n<p>This can also be used in combination to <code>isset()</code> and other functions to accomplish what you want.</p>\n'}]
['https://Stackoverflow.com/questions/55060', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5261/']