Passing an argument that may not be set in PHP
<p>This is a dodgy question...</p>

<p>When requesting a page, the output of the page depends on the parameters passed. Soo:</p>

<pre><code>http://localhost/test/php?key=value
</code></pre>

<p>AND</p>

<pre><code>http://localhost/test/php?key=other_value
</code></pre>

<p>will produce different outputs.</p>

<p>There are a lot of these parameters and sometimes they are set, sometimes they are not.</p>

<p>I have this code block, that I keep copying over and over:</p>

<pre><code>if ( !isset( $PARAMS["type"] ) ){
    $message = urlencode("No Type Parameter Defined In File {$_SERVER["PHP_SELF"]}");
    header( "Location: /admin/pages/{$PARAMS["page"]}?m=$message" );
    exit;
}
</code></pre>

<p>Now this code block is repeated in some cases about 6  times!</p>

<p>So I thought I could write a function:</p>

<pre><code>function redirect_on_fail( $var ){
    if ( !isset( $var ) ){
        $message = urlencode("No Type Parameter Defined In File {$_SERVER["PHP_SELF"]}");
        header( "Location: /admin/pages/index?m=$message" );
        exit;
    }
}
</code></pre>

<h2>OBVIOUSLY this will not work</h2>

<p>Because I will have to call <code>redirect_on_fail( $PARAMS["type"] );</code> and if it is not set, it wont be passed to the function... </p>

<p>Because I need to test whether the variable exists in the Pages Scope...</p>

<p>So I could just do this, I suppose:</p>

<pre><code>function redirect_on_fail( $message, $redirect_to ){
    header( "Location: /admin/pages/$redirect_to?m=".urlencode($message) );
    exit;
}
</code></pre>

<p>But then in the page I'm doing:</p>

<pre><code>if ( !isset( $PARAMS["type"] ) ){
    redirect_on_fail( "No Type Parameter", $redirect_to );
}
</code></pre>

<p>and that defeats the point...</p>

<p>So is there a way around this?</p>
