NameSilo

What does this code mean?

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch
Impact
19
Hey
i m reading a tutorial and they dont really explain what this line means:
PHP:
$username = (@$_SESSION['username']) ? @$_SESSION['username'] : @$_COOKIE['username'];
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
DUnno why they use the @ but it means

PHP:
if($_SESSION['username']){
 $username = $_SESSION['username'];
}else{
$username = $_COOKIE['username'];
}

I guess if the session variable 'username' isn't empty, assign it to variable username, else assign the cookie 'username' to the variable 'username'
 
0
Close
They have put the @ there I believe in case the session is not initialized. If it is not you would get a warning (an @ sign suppresses warnings).
 
0
Close
To add to what has been said, the line is what is called a ternary operator. I wrote a tutorial about them here:

http://www.zoomcities.com/forum/showthread.php?tid=2362

I do not know why they have used the @ sign to suppress a potential warning either as there will not be one. That line i actually better written as:

PHP:
$username = (strlen($_SESSION['username']) > 0) ? $_SESSION['username'] : $_COOKIE['username'];
 
0
Close
Matthew. said:
I do not know why they have used the @ sign to suppress a potential warning either as there will not be one. That line i actually better written as:

Actually that depends on what error level you have php set as. If you have it set as E_ALL then the following errors will be encountered:-

Notice: Undefined variable: _SESSION in PHPDocument1 on line 3

Notice: Undefined index: username in PHPDocument1 on line 3

The reason you do not get errors will be because you have it set to a lower level .
 
0
Close
True, i tend to advise most people to ignore notices unless they want to spend 90% of their time on endless pursuits of perfection.

PHP:
error_reporting(E_ALL ^ E_NOTICE);

FTW lol.
 
0
Close
Matthew. said:
True, i tend to advise most people to ignore notices unless they want to spend 90% of their time on endless pursuits of perfection.

PHP:
error_reporting(E_ALL ^ E_NOTICE);

FTW lol.

I agree that you should use E_ALL ^ E_NOTICE on a production server but I always prefer to run E_ALL when testing. Any script you write SHOULD be able to be run without the need to suppress the notices.
 
0
Close
filth@flexiwebhost said:
Matthew. said:
True, i tend to advise most people to ignore notices unless they want to spend 90% of their time on endless pursuits of perfection.

PHP:
error_reporting(E_ALL ^ E_NOTICE);

FTW lol.

I agree that you should use E_ALL ^ E_NOTICE on a production server but I always prefer to run E_ALL when testing. Any script you write SHOULD be able to be run without the need to suppress the notices.

It is sometimes tedious to remove all notices, especially when using libraries.
A suggestion would be to use php.ini or htaccess to change display_errors to off.
Errors may reveal information to aid a hacking attempt.
 
0
Close
Showing all notices can be very annoying, for example if i did:
PHP:
if($_GET[code'] == 21) {
And code wasn't active, it would mean that i would have to put in more code above that to check, and to be honest in my opinion its just a waste of time and space.
 
0
Close
beaver6813 said:
Showing all notices can be very annoying, for example if i did:
PHP:
if($_GET[code'] == 21) {
And code wasn't active, it would mean that i would have to put in more code above that to check, and to be honest in my opinion its just a waste of time and space.

If you did that you would get one whopping parser error lol :hehe:
 
0
Close
beaver6813 said:
Showing all notices can be very annoying, for example if i did:
PHP:
if($_GET[code'] == 21) {
And code wasn't active, it would mean that i would have to put in more code above that to check, and to be honest in my opinion its just a waste of time and space.


all it would take to fix that notice is using the isset function on $_GET['code'] so the code would end up:-

PHP:
if(isset($_GET['code']) && $_GET['code'] == 21) {

(also helps to put the right amount of quotes in)
 
0
Close
filth@flexiwebhost said:
all it would take to fix that notice is using the isset function on $_GET['code'] so the code would end up:-

PHP:
if(isset($_GET['code']) && $_GET['code'] == 21) {

(also helps to put the right amount of quotes in)

Gimme a break :P :'(
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Quick replies: search, your most used replies, and more on every post
Quick replies are easier to find and use now. The quick reply menu has a search box. Type a few letters to find the reply you want. A...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back