Page 1 of 1

Extract function and @ sign confusion

Posted: Wed Dec 01, 2010 11:15 am
by wtc
Hello;

Being somewhat new to php, I am trying to understand some syntax regarding the use of "@" in the code I'm currently working on.

First, the pre-existing code works just fine. the line that confuses me is written like this:

Code: Select all

@extract(load_page(info/file.php));
and am trying to understand the first portion - "@extract".

"Load_page" is a custom function - I know what it does, no problem.
"info/file.php" is also a known entity to me, and is not today's issue - it works fine. The total function points to a record in a table, gets a bunch of info from the correct record into an array, later used to display on the page. As I say, it all works.

But what do you suppose "@extract" is? I know php has an extract() function, and because I find no reference to "@extract" being any sort of custom code within this particular web site, I am assuming the line is referring to the php function. but why wouldn't the original author have simply used:

Code: Select all

extract(load_page(info/file.php));
without the "@"? I am not familiar with why and what the "@" is doing here? Or am I way off base, and need to find some custom function called "@extract" in my program?

thanks,
tc

Re: Extract function and @ sign confusion

Posted: Wed Dec 01, 2010 11:18 am
by social_experiment
@ is an error suppressor in php.

Re: Extract function and @ sign confusion

Posted: Wed Dec 01, 2010 11:31 am
by wtc
thanks - so, by using @, we get the benefit of running the "extract()" function, but without it stopping execution if an error is encountered, is that roughly it?

Re: Extract function and @ sign confusion

Posted: Wed Dec 01, 2010 11:38 am
by social_experiment
Almost. From the php manual (sic) Error Control Operators: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

Re: Extract function and @ sign confusion

Posted: Wed Dec 01, 2010 11:40 am
by AbraCadaver
wtc wrote:thanks - so, by using @, we get the benefit of running the "extract()" function, but without it stopping execution if an error is encountered, is that roughly it?
Well, most likely you would only get a warning, normally if the argument passed to extract was not an array so execution would not stop, but the error would not be displayed. If a fatal error occurs the @ won't prevent the script from aborting. Using error suppression is a bad practice and is used to hide bad coding practices. In this case you should make sure that you are passing an array to extract() and ideally make sure that the array is not empty because later in the code you may not have vars that you expect to have.

Re: Extract function and @ sign confusion

Posted: Wed Dec 01, 2010 11:57 am
by wtc
Great explanations, thanks much, I get it!

This is a great forum....! Thanks to all,

tc