NameSilo

What does this code mean?

Spaceship Spaceship
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
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back