Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In PHP, a variable or array element which has never been set is different from one whose value is <code>null</code>; attempting to access such an <em>unset</em> value is a runtime error. </p> <p>That's what you're running into: the array <code>$_POST</code> does not have any element at the index <code>"username"</code>, so the interpreter aborts your program before it ever gets to the nullity test.</p> <p>Fortunately, you can test for the existence of a variable or array element without actually trying to access it; that's what the special operator <code>isset</code> does:</p> <pre><code>if (isset($_POST["username"])) { $user = $_POST["username"]; echo $user; echo " is your username"; } else { $user = null; echo "no username supplied"; } </code></pre> <p>This looks like it will blow up in exactly the same way as your code, when PHP tries to get the value of <code>$_POST["username"]</code> to pass as an argument to the function <code>isset()</code>. However, <code>isset()</code> is not really a function at all, but special syntax recognized before the evaluation stage, so the PHP interpreter checks for the existence of the value without actually trying to retrieve it. </p> <p>It's also worth mentioning that as runtime errors go, a missing array index is considered a minor one (assigned the <code>E_NOTICE</code> level). If you change the <code>error_reporting</code> level so that notices are ignored, your original code will actually work as written, with the attempted array access returning <code>null</code>. But that's considered bad practice, especially for production code.</p> <p>Side note: PHP does string interpolation, so the <code>echo</code> statements in the <code>if</code> block can be combined into one:</p> <pre><code>echo "$user is your username"; </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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