Jump to content

Strange! No warning for missing argument?


reefat

Recommended Posts

I have been scripting (some may say programming) in PHP for last 5 years but never faced this issue. Interesting!

 

Say for example, I created a custom function with one argument:

 

function myFunc($arg1) {
echo $arg1;
}

 

Now, if I call this function without supplying any argument it doesn't give any error/warning. Any other high level language like C++ or Java would produce error in this case. So, I had to modify my code as follows:

 

function myFunc($arg1) {
if (!isset($arg1)) {
	echo "Missing argument 1";
}
echo $s;
}

 

But this is annoying. Now I have to add these EXTRA lines of code in every function and class methods I have created. Is there any shortcut/easy method that the script will always produce an error if its missing any required argument? I have tried with adding error_reporting(E_ALL); at the very beginning of my code. But it still didn't work out. By the way, I am using PHP 5.3.1 on Apache/2.2.14 (Unix).

 

Any suggestion will be appreciated.

Link to comment
Share on other sites

Oops! Forgot to mention that I'm using a custom error handler set by set_error_handler("__error_handler"); . E_COMPILE_WARNING was turned off there. This was causing the problem. Sorry for the confusion. And thanks a lot for the cooperation.

Link to comment
Share on other sites

PHP, like Perl, has a "no complaints mode."  Use this line to turn error-reporting on:

error_reporting(E_ALL);

Put that at the top of your script, or edit your PHP.ini file, and you'll see all the errors you've been missing.

 

Also, as a side note, it's possible to make a function argument optional in PHP by supplying it with a default value:

function myFunc ( $a = "Hello, World!" ) {}

-Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.