Note that there are some explanatory texts on larger screens.

plurals
  1. POConstant, isset and empty evaluation
    primarykey
    data
    text
    <p>Does anyone know how isset and empty is interpreted by the translator, or how the translator treats undefined variables?</p> <p>To expand on my question, I have the below code in my include file:</p> <pre><code>define('USER_AUTH', !empty($_SESSION['username']) &amp;&amp; !empty($_SESSION['status'])); //user is verified define('ACC_IS_PENDING', $_SESSION['status'] == '0');//checks to see if user status is pending which has a status of 0 </code></pre> <p>USER_AUTH is used as a quick hand to check if user is authenticated. ACC_IS_PENDING is used as a quick hand for when the account status is pending. However, PHP gives me a notice to advise me that $_SESSION['status'] in my second line of code is undefined. I know that it is undefined, but I haven't used it yet! How dare you tell me what I already know. LoL</p> <p>However, when I trick the code with the below:</p> <pre><code>define('USER_AUTH', !isempty($_SESSION['username']) &amp;&amp; !isempty($_SESSION['status'])); define('ACC_IS_PENDING', $_SESSION['status'] == '0'); </code></pre> <p>Where isempty() is a custom made function that will always return FALSE. Then no notice! Alternatively, if I use the below code:</p> <pre><code>define('USER_AUTH', notempty($_SESSION['username']) &amp;&amp; notempty($_SESSION['status'])); define('ACC_IS_PENDING', $_SESSION['status'] == '0'); </code></pre> <p>Where notempty() always return TRUE, then again no notice.</p> <p>Am I right in saying that the translator checks that the variable has been tested once, and that if the test resulted in true, then the translator sees this as the variable has been defined?</p> <p>If this was the case, then what about isset and empty? They both seem to give me notices no matter if the evaluation is true or false.</p> <pre><code>define('USER_AUTH', isset($_SESSION['username']) &amp;&amp; isset($_SESSION['status'])); define('ACC_IS_PENDING', $_SESSION['status'] == '0'); </code></pre> <p>and</p> <pre><code>define('USER_AUTH', empty($_SESSION['username']) &amp;&amp; empty($_SESSION['status'])); define('ACC_IS_PENDING', $_SESSION['status'] == '0'); </code></pre> <p>Apologies for the long winded question. This seems trivial, but it would be nice to have a quick defined constant without having to get notices! Any help in explanation or a better solution for such trivial task would be appreciated, thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload