===== PHP Survival Tips ===== created: 2025-04-28 last modified: 2025-04-28 ===== * it really helps, during debugging, to have included in your PHP: ini_set("display_errors", 1); error_reporting(E_ALL); ===== * when you get a completely blank screen as your PHP response, remember you can use the php command-line interface on nrs-projects to see if there's a parse error: $ php my-app.php ===== * I have found it useful to output p elements at intermediate points when I can't figure out *where* a bug is. (<p> before the while loop </p>, <p> DID connect </p>, <p> called function blah with argument <?= $param ?>, etc.) * (PHP's version of a debugging cout statement? 8-) ) ===== PHP SESSION-SANITY HELPERS ===== * draw a FINITE-STATE DIAGRAM to describe your desired web application's behavior! * (see posted photo of informal finite state diagram for try-quad.php) * have a helper function for each state in your application logic * have the first version of each be a STUB, a little working version of the function that just proclaims what you called * (and including an anchor element to your PHP to continue is also useful) * START the postback PHP for your application with an if-elseif that just lays out the order these will be called, setting up a $_SESSION key to keep track of the current state * INCLUDE an else at the end of if-elseif "driving" the application that you HOPE you never reach, that outputs a warning p element and destroys the current session if you DO reach it ===== REMINDER: a useful pattern in each state of a PHP application using sessions: ==== * as you are writing the function FOR a state in your application: * What information does this state need to GET (and SANITIZE/VALIDATE) from a just-submitted FORM? (typically, from the $_POST array, if its method="post") * What information from a PREVIOUS state does this state need to get FROM the $_SESSION array? * What information will a LATER state possibly need that this state should ADD to (or update in) the $_SESSION array? ===== * browsers CAN be a *pain* when you try to view the source for a postback PHP using sessions -- I'm finding that, lately, both Chrome and Firefox are fetching the NEXT state when I'm trying to see the PHP-generated source in my browser for debugging! * this MIGHT be a preference that can be set...? * Safari, currently, is NOT doing this to me, so I am finding myself using that more!