Re: Newbie: Do or Die ... howto in PHP???
| From: | Tom Rogers | Date: | Mon, 07 Apr 2003 05:29:33 +0000 |
| Subject: | Re: Newbie: Do or Die ... howto in PHP??? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-142635@lists.php.net to get a copy of this message | ||
Hi,
Monday, April 7, 2003, 2:16:59 PM, you wrote:
RB> Hi gang
RB> I've been scouraging both through my offline manual, and the online
RB> version, trying to find a way to use a Do or Die method (or try/catch as
RB> it's called in java) for checking if query variables are set, but have
RB> come up a blank if this is even possible in PHP, and then even more blank
RB> as how to do it, and how the syntax would look...
RB> Like I currently do this:
RB> if (isset($_GET['loc'])) {
RB> $loc = $_GET['loc'];
RB> $includefile("files/$loc.src.html");
RB> }
RB> else {
RB> $includefile('files/defaultfile.src.html');
RB> }
RB> or something similar, depending on the situation (and of course I
RB> occasionally use both post and get or either, which makes it more
RB> complex).
RB> Since some of the pages can take quite a few variables, this can get a bit
RB> teadious to code default values this way, and also pretty complex on the
RB> pages that have to check for the same variable in both get and post (I
RB> can't always use $_REQUEST).
RB> So is there some way to code a Do or Die procedure in PHP???
RB> Like in Java, you'd do something like:
RB> try {
RB> // some expressions
RB> }
RB> catch {
RB> // some fail-safe/error management expressions
RB> }
RB> finally {
RB> // always do this
RB> }
RB> Does PHP have this??? I tried looking under error management, but that
RB> didn't really fit what I'm looking for...
RB> TIA
RB> Rene
RB> --
RB> Rene Brehmer
RB> This message was written on 100% recycled spam.
RB> Come see! My brand new site is now online!
RB> http://www.metalbunny.net
To cut down typing I use this class in an auto prepend file.
class req {
function get($var,$sub=False){
return (isset($_GET[$var]))? $_GET[$var]:$sub;
}
function post($var,$sub=False){
return (isset($_POST[$var]))? $_POST[$var]:$sub;
}
function request($var,$sub=False){
return (isset($_REQUEST[$var]))? $_REQUEST[$var]:$sub;
}
function server($var,$sub=False){
return (isset($_SERVER[$var]))? $_SERVER[$var]:$sub;
}
function session($var,$sub=False){
return (isset($_SESSION[$var]))? $_SESSION[$var]:$sub;
}
}
No need to instantiate the class just use like this
if($loc = req::get('loc'){
$includefile("files/$loc.src.html");
}
else {
$includefile('files/defaultfile.src.html');
}
you can also supply a default value like this:
$file = req::get('loc','defaultfile').'.src.html';
incluse{$file);
If you get variables by both post or get use req::request('var')
Not exactly what you asked for but it should help
--
regards,
Tom