Q695 Posted June 22, 2014 Share Posted June 22, 2014 Someone showed me a while ago this trick for loading variables, but it creates an error when you go to the page initially: $fname = $_POST['fname'] ? $_POST['fname'] : null; How do I write the short hand to run correctly? Quote Link to comment https://forums.phpfreaks.com/topic/289248-short-hand-variable-loading/ Share on other sites More sharing options...
Solution Jacques1 Posted June 22, 2014 Solution Share Posted June 22, 2014 You need to check if the parameter even exists: $fname = isset($_POST['fname']) ? $_POST['fname'] : null; However, this whole practice of putting external input into separate variables doesn't make a lot of sense. You already have a variable with the value: $_POST['fname']. What's the point of creating this extra $fname variable? Only to save 10 characters when writing it down? That's not really a valid reason in the times of auto-completion. Quote Link to comment https://forums.phpfreaks.com/topic/289248-short-hand-variable-loading/#findComment-1483045 Share on other sites More sharing options...
Q695 Posted June 23, 2014 Author Share Posted June 23, 2014 Someone points their form without data required for the PDO to the page the form is on. When it's not set, it automatically sets the value to null. Sorry about the redundant posting, it was a slow server that I'll blame ;) Quote Link to comment https://forums.phpfreaks.com/topic/289248-short-hand-variable-loading/#findComment-1483047 Share on other sites More sharing options...
Jacques1 Posted June 23, 2014 Share Posted June 23, 2014 Someone points their form without data required for the PDO to the page the form is on. When it's not set, it automatically sets the value to null. This doesn't require an extra variable. Just set the value to null when needed, e. g., $stmt = $database->prepare(' ... some query ... '); $stmt->execute(array( 'fname' => isset($_POST['fname']) ? $_POST['fname'] : null )); Quote Link to comment https://forums.phpfreaks.com/topic/289248-short-hand-variable-loading/#findComment-1483048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.