Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP variable default

PHP variable default
Thread Tools
Chris O'Brien
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Feb 23, 2004, 09:31 AM
 
This will be an easy question, but I'm having no luck figuring it out (after the whole 5 minutes I've spent - patience isn't one of my virtues ). Basically, if I have a user defined variable that isn't set, I just want to provide a default value. Make sense?

In javascript, I'd just use something like:
Code:
function whatever(arg1, arg2) { var something = arg1 || "chris"; var something_else = arg2 || "topher"; }
With this PHP, it's just $_GET variable i'm using, so, although it's a different scope, I wouldn't've imagined it to be difficult. I initially thought using
[php]
$al = $_GET['something'] || "hi";
[/php]

might work, but it doesn't. What does PHP default $_GET variables to if they aren't present? I had presumed something like
[php]
if (is_null($_GET['something'])) {
$al = "hi";
} else {
$al = $_GET['something'];
}
//or use !is_string()...
[/php]
would work, but even though it does to an extent, i still get the PHP error 'undefined index'.

Anyway, there must be an obvious thing I'm missing here, as this seems very easy.

Cheers for any help
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
Chris O'Brien  (op)
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Feb 23, 2004, 09:35 AM
 
Sorry! me == idiot.

Just remembered isset() and it works.

Anyway - is there a more elegant way of going about this?
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
Diggory Laycock
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Feb 23, 2004, 02:18 PM
 
if by more elegant - you mean smaller:

[php]$something = ( isset($GET["something"]) ? $GET["something"] : 'default something' );[/php]
     
Chris O'Brien  (op)
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Feb 23, 2004, 02:57 PM
 
Ah - I didn't know PHP supported that syntax. I use it all the time in javascript - I suppose i should've just given it a try.

Thanks very much.
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 10:04 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,