Hi,

Friday, October 31, 2003, 5:31:01 AM, you wrote:
wlcn> I'm sure I'm not the first person to find strict checking of whether
wlcn> or not variable (or array index) is set painful. I've considered
wlcn> just setting error_reporting() to some lax level on every script I
wlcn> write for the rest of my life, but have been thinking there might
wlcn> be a better way.

wlcn> What I'd like is a "sureset()" function ...there's probably a better 
wlcn> name, but what it would do is more important:

wlcn> function sureset($var) {

wlcn>   if(!isset($var) || empty($var))
wlcn>           return '';
wlcn>   else
wlcn>           return $var;
wlcn> }

wlcn> Of course, when you've got strict checking on, the above doesn't 
wlcn> work, because if the variable is unset you get caught on the fact 
wlcn> before the function call happens.

wlcn> Is there something like this already? Is there a way to make this 
wlcn> work? Or should I just go back to the idea of nuking error_reporting
wlcn> in all my scripts?

wlcn> Thanks,
wlcn>   Weston


wlcn> Thanks,

As an experiment i made an internal php function which I called if_isset() which
returned false if it was not set, but if it was it returned the value. It
replaced this construct which drives me crazy

$newvar = (isset($var))?$var:'';

it became

$newvar = if_isset($var);

It acually saved a few micro seconds too.

You could pester php internals to implement something like that but a plastic
antenna would probably be more receptive.

I also have a class to cope with this problems big brother:

if(isset($_POST['value']) && $_POST['value'] != ''){
  $name = $_POST['value'];
}else{
  $name = 'No Supplied';
}

I now type

$name = req::post('value','Not Supplied');

The class is very small and just needs including (I have it in an auto prepend
file) and you don't need to create an instance as it has no internal variables
to worry about.

<?php
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;
        }
        function getEQ($var,$eq){
                return (req::get($var) == $eq)?True:False;
        }
        function postEQ($var,$eq){
                return (req::post($var) == $eq)?True:False;
        }
        function requestEQ($var,$eq){
                return (req::request($var) == $eq)?True:False;
        }
        function serverEQ($var,$eq){
                return (req::server($var) == $eq)?True:False;
        }
        function sessionEQ($var,$eq){
                return (req::session($var) == $eq)?True:False;
        }
}
?>

You may find it useful..
-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to