[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 12-30-2006, 07:37 PM   #1 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold


what does this code mean?

Hey
i m reading a tutorial and they dont really explain what this line means:
PHP Code:
$username = (@$_SESSION['username']) ? @$_SESSION['username'] : @$_COOKIE['username'];
unknowngiver is offline  
Old 12-30-2006, 10:19 PM   #2 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

Noobie is on a distinguished road


DUnno why they use the @ but it means

PHP Code:
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'
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 12-31-2006, 05:53 AM   #3 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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).
Peter is offline  
Old 12-31-2006, 06:21 AM   #4 (permalink)
Stud Sausage
 
Join Date: Dec 2006
Location: England
Posts: 1,546
34.41 NP$ (Donate)

Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of

Adoption Breast Cancer Breast Cancer Cancer Survivorship
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 Code:
$username = (strlen($_SESSION['username']) > 0) ? $_SESSION['username'] : $_COOKIE['username'];
Matthew. is offline  
Old 12-31-2006, 09:33 AM   #5 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Quote:
Originally Posted by Matthew.
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:-

Quote:
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 .
Peter is offline  
Old 12-31-2006, 09:48 AM   #6 (permalink)
Stud Sausage
 
Join Date: Dec 2006
Location: England
Posts: 1,546
34.41 NP$ (Donate)

Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of

Adoption Breast Cancer Breast Cancer Cancer Survivorship
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 Code:
error_reporting(E_ALL ^ E_NOTICE);
FTW lol.
Matthew. is offline  
Old 12-31-2006, 10:39 AM   #7 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Quote:
Originally Posted by Matthew.
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 Code:
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.
Peter is offline  
Old 01-01-2007, 02:05 AM   #8 (permalink)
NamePros Regular
 
Join Date: Feb 2006
Posts: 588
1,620.95 NP$ (Donate)

jerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to all


Quote:
Originally Posted by filth@flexiwebhost
Quote:
Originally Posted by Matthew.
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 Code:
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.
jerometan is offline  
Old 01-01-2007, 02:14 AM   #9 (permalink)
NamePros Regular
 
beaver6813's Avatar
 
Join Date: May 2005
Location: England
Posts: 349
65.50 NP$ (Donate)

beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough


Showing all notices can be very annoying, for example if i did:
PHP Code:
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.
__________________
-Beaver6813.com V5 Soon!
beaver6813 is offline  
Old 01-01-2007, 04:41 AM   #10 (permalink)
Stud Sausage
 
Join Date: Dec 2006
Location: England
Posts: 1,546
34.41 NP$ (Donate)

Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of

Adoption Breast Cancer Breast Cancer Cancer Survivorship
Quote:
Originally Posted by beaver6813
Showing all notices can be very annoying, for example if i did:
PHP Code:
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
Matthew. is offline  
Old 01-01-2007, 04:59 AM   #11 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Quote:
Originally Posted by beaver6813
Showing all notices can be very annoying, for example if i did:
PHP Code:
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 Code:
if(isset($_GET['code']) && $_GET['code'] == 21) {
(also helps to put the right amount of quotes in)
Peter is offline  
Old 01-01-2007, 06:45 AM   #12 (permalink)
NamePros Regular
 
beaver6813's Avatar
 
Join Date: May 2005
Location: England
Posts: 349
65.50 NP$ (Donate)

beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough


Quote:
Originally Posted by filth@flexiwebhost
all it would take to fix that notice is using the isset function on $_GET['code'] so the code would end up:-

PHP Code:
if(isset($_GET['code']) && $_GET['code'] == 21) {
(also helps to put the right amount of quotes in)
Gimme a break :P
__________________
-Beaver6813.com V5 Soon!
beaver6813 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 05:18 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85