Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/311794-are-quote-marks-required/
Share on other sites

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;

 

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.

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.

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?

@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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.