Extract function and @ sign confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Extract function and @ sign confusion

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Extract function and @ sign confusion

Post by social_experiment »

@ is an error suppressor in php.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Re: Extract function and @ sign confusion

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Extract function and @ sign confusion

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Extract function and @ sign confusion

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Re: Extract function and @ sign confusion

Post by wtc »

Great explanations, thanks much, I get it!

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

tc
Post Reply