phppup Posted December 2, 2020 Share Posted December 2, 2020 While playing with some code, I wrote these few lines of script. $_SESSION["favcolor"] = green; echo "Session variable is ". $_SESSION["favcolor"] ; I tested the code and everything worked fine. But then, I noticed an error in my coding: I had accidentally forgotten the QUOTES around the word GREEN. I corrected the code $_SESSION["favcolor"] = "green"; But when I tested it, the page would not open/load. Then I removed the quote marks and restored the line to it's original state $_SESSION["favcolor"] = green; And things worked fine. Shouldn't the quote marks be required? Why am I having this issue? Is it really an issue, or should I just accept that it works WITHOUT the quotes and be satisfied with that result? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 2, 2020 Share Posted December 2, 2020 Quotes are normally required, however, see https://www.php.net/manual/en/language.types.string.php for details. The only way I would expect things to work find without them is if your code was something like this: define("green", "Some important stuff"); $_SESSION["favcolor"] = green; Quote Link to comment Share on other sites More sharing options...
requinix Posted December 2, 2020 Share Posted December 2, 2020 If you didn't have a constant named "green" then PHP was converting that to a string for you. But as of PHP 8, that will no longer happen and your script will die. PHP raised a warning when it did that. If you didn't see the warning then your PHP is not set up correctly for development. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 2, 2020 Share Posted December 2, 2020 If you had error reporting tuned on, you would see a message like this Quote Notice: Use of undefined constant green - assumed 'green' in C:\inetpub\wwwroot\test\test.php on line xx If the PHP processor comes across an unquoted string it assumes it is a defined constant. It then tries to find the value that was defined for that constant. If it cannot find one it then assumes it must be a string value 'green'. All this is a waste of time making your code less efficient, so always quote string literals. Quote Link to comment Share on other sites More sharing options...
phppup Posted December 2, 2020 Author Share Posted December 2, 2020 As I said, Quote I corrected the code $_SESSION["favcolor"] = "green"; But when I tested it, the page would not open/load. It seems that the CORRECT coding is NOT being allowed to function. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 2, 2020 Share Posted December 2, 2020 1 minute ago, phppup said: It seems that the CORRECT coding is NOT being allowed to function. If it doesn't function then it isn't correct. But it does need quotes. Quote Link to comment Share on other sites More sharing options...
phppup Posted December 3, 2020 Author Share Posted December 3, 2020 And while we're discussing anomalies, I am trying to determine when a SUBMIT button with a value = 'submit' is clicked. $A = $_POST['click1']; echo "<br>1 ".$A."<br>";echo "<br>2 ".$_POST['click1']."<br>"; if(isset($A)) { echo "YES, the value is: ". $A; } else { echo "NO the value is: " . $A; } if(isset($A)) { echo "YES, the value is:". $_POST['click1']; } else { echo "NO the value is: " . $_POST['click1']; } if(isset($_POST['click1'])) { echo "YES, the value is: ". $A; } else { echo "NO the value is: " . $A; } if(isset($_POST['click1'])) { echo "YES, the value is:". $_POST['click1']; } else { echo "NO the value is: " . $_POST['click1']; } None of my echo messages are grabbing $A, although I seem to have it declared. What did I do wrong here? Is there a better way to determine if a specific input='submit' is selected? Quote Link to comment Share on other sites More sharing options...
requinix Posted December 3, 2020 Share Posted December 3, 2020 What's the markup for the button? Quote Link to comment Share on other sites More sharing options...
phppup Posted December 3, 2020 Author Share Posted December 3, 2020 <input type="submit" id="click1" name="click1" value="SUBMIT"> Quote Link to comment Share on other sites More sharing options...
requinix Posted December 3, 2020 Share Posted December 3, 2020 Then the code you posted isn't wrong. Something else is. Quote Link to comment Share on other sites More sharing options...
phppup Posted December 3, 2020 Author Share Posted December 3, 2020 @Barand (and others) YES, I implemented error reporting and saw the warning [described above]. This brings me back to my initial question: Why isn't the correct coding be accepted? Quote Link to comment Share on other sites More sharing options...
phppup Posted December 3, 2020 Author Share Posted December 3, 2020 @requinix YES, I am beginning to think that low grade server fees come with a high price. I suppose I should be happy that I've excelled to the point of catching the errors and seeking [something closer to] perfection. Although it may have been easier when I didn't notice these mistakes and was simply happy to have a result generated. LOL Quote Link to comment 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.