variable declearation... "@$variable"

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
bwan37
Forum Newbie
Posts: 9
Joined: Thu May 22, 2008 11:42 am

variable declearation... "@$variable"

Post by bwan37 »

I am new to PHP... very very dumb in it..

my file... "login.php" require a variable "$_REQUEST['username'] to check for login..
but I got an error...
Undefined index: username in D:\public_html...

after decleared my variable to "@$_REQUEST['username']"
the error is gone..

question:
what does "@" in front of the variable does?

Thank you so much!!

Sincerely,
Ben
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: variable declearation... "@$variable"

Post by nowaydown1 »

Hi Ben,

The @ operator tells PHP to stop it's complaining. This prevents it from outputting any notice/warning stuff to the page if display_errors is on in your php.ini.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: variable declearation... "@$variable"

Post by aditya2071990 »

placing the symbol '@' before any statement supresses all error messages and doesn't allow php to output them.

It is generally used before mysql functions like mysql_query(), mysql_connect() and so on.

That is done so that if the mysql function fails to execute properly, then instead of displaying the ugly error message it outputs, we can display a more decent error message like "Sorry, the database is F***ed up at the moment!" :D

In your case, there is a script error and php is only complaining that it can't run the script properly unless you correct it. By placing '@', u r telling it to shut up, so it aint saying anything.

But your code still won't work. There is an error which needs to be corrected. Put the whole script outta here (remember to delete the username and password in mysql connect functions).

We'll try to help.

Good Luck.
bwan37
Forum Newbie
Posts: 9
Joined: Thu May 22, 2008 11:42 am

Re: variable declearation... "@$variable"

Post by bwan37 »

wow.. I understand it la..
um.. here's the code I wrote...

---------login.php----------
//url: http://localhost/login.php?site=my-nude-pic.php

<?php
$_loginInfo['user'] = 'myname';
$_loginInfo['pass'] = 'mypass';

//if successfully login (username & password is correct)
if( (@$_REQUEST['user']==$_loginInfo['user']) && ($_REQUEST['pass']==$_loginInfo['pass']) ) {
// goto "my-nude-pic.php"
}
//otherwise ask for username & password
else {
echo login_form();
}

//** FUNCTION BEGINS **//

// here's the necessary login forum
function login_form() {
$form = "<p class=heading>Login Page!</p>\n";
if(@$_GET['site']!=null) { $site = "?site=".$_GET['site']; }
$form .= "<form action='login.php".@$site."' method='post' name=login_form>\n";
$form .= " Username : <input type='text' name='user' value=\"\">\n";
$form .= " Password : <input type='password' name='pass' value=\"\">\n";
$form .= " <input class='button' type='submit' value='Submit'>";
$form .= "</form>\n";
return $form;
}
?>
-----------------

what do you think the correct syntax should be, (if I don't use the @ in front of $var)?

Thank you very much.. :D
Post Reply