rocky48 Posted August 27, 2015 Share Posted August 27, 2015 I am getting as series of errors all of the same type concerning constants that are argument of a posted value. Viz: Notice: Use of undefined constant color - assumed 'color' in C:\wamp\www\1066mk4\Prnpdf.php on line 6 Here is a snippet of the code: $color = $_POST[color]; $image=$_POST[image]; $pdf->SetLeftMargin(float0); $pdf->SetFont($_POST[fontface],'',$_POST[font]); $pdf->SetXY($BoxX, $_POST[Top]); $pdf->Image($image,$floatx,$floaty,$floatw,$floath,jpg,''); The last line shown is a different error in as much as it is an argument of the image type as used in fpdf. Your help will be appreciated. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 27, 2015 Share Posted August 27, 2015 You get that error because the string index "color" is not in quotes $color = $_POST[color]; should be $color = $_POST['color']; Because you haven't used quotes, php thinks it is a defined constant. When it cannot find the definition it throws the error. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 27, 2015 Share Posted August 27, 2015 As in your last post you continue to use non-standard array notation. The index of an array element if not that of a numeric array needs to be in quotes. $ar = array(); $ar[0]= 'first element'; ar[1]= 'second element'; echo $ar[0],'<br>'; echo $ar[1],'<br>'; produces and outputs a proper numeric array. $ar = array(); $ar['first'] = 'first element'; $ar['second'] = 'second element'; echo $ar['first'],'<br>'; echo $ar['second'],'<br>'; produces and outputs an associative array. If you read up on arrays in the manual you will see references to how PHP tries to interpret what you are writing. Indices that are non numeric and not in quotes will first be checked as a constant which is what the messages are telling you. I don't think that is what you are trying to do. Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 27, 2015 Share Posted August 27, 2015 These are 'Notices'. A notice is not an error per-se. Your code will work without the use of a string index: name vs. 'name' However, you are causing PHP to do extra work, because it generates a notice (if the error level includes notices) has to first check to see if there is a constant with that name Obviously this is not best practice. Frequently I find that people have gotten into the bad practice of omitting single quotes around the string constants they use, because they are having parsing problems with interpolation. This doesn't work: $name['first'] = 'Fred'; $name['last'] = 'Stone'; $output = "Hi $name['first']! I hope that you and the rest of the $name['last'] family have a great visit with us at our first class resort!"; echo $output; Run this and it generates a parsing error: PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) The secret to getting around this issue is to wrap the array variables in a php block - { }. This code works: $name['first'] = 'Fred'; $name['last'] = 'Stone'; $output = "Hi {$name['first']}! I hope that you and the rest of the {$name['last']} family have a great visit with us at our first class resort!"; echo $output; Output: Hi Fred! I hope that you and the rest of the Stone family have a great visit with us at our first class resort! Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 27, 2015 Share Posted August 27, 2015 @gizmola, I (almost) always wrap my variables in curly braces when used in a string. However, I think the problem with people not using quotes around strings used as array keys is specifically because there is a specific exception with regard to the use of array variables in double-quoted strings. As you point out, this will not work: $name['first'] = 'Fred'; $name['last'] = 'Stone'; $output = "Hi $name['first']! I hope that you and the rest of the $name['last'] family have a great visit with us at our first class resort!"; echo $output; However, this will work and is actually 'proper' per the manual (see third echo in example # $name['first'] = 'Fred'; $name['last'] = 'Stone'; $output = "Hi $name[first]! I hope that you and the rest of the $name[last] family have a great visit with us at our first class resort!"; echo $output; Note: single quotes around the keys when defining, but no quotes around the keys when used in double-quoted text. Personally, I don't like it and never use it. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 27, 2015 Share Posted August 27, 2015 There's also this from another page in the manual: http://php.net/manual/en/language.types.array.php // The following is okay, as it's inside a string. Constants are not looked for// within strings, so no E_NOTICE occurs here print "Hello $arr[fruit]"; // Hello apple Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 28, 2015 Share Posted August 28, 2015 Psycho, Agreed 100%. I stated this, but without the examples you provided, when I said that it would treat them first as constants, generate a notice, but still resolve them as string constant keys in the array. As you state the manual indicates that you can leave off the single quotes, but I think that's bad form, and as it happens causes the notice to fire. If we're trying to be notice -free, then the way to go is to use the blocks around the array variables, and I think that is what people should be taught, and become comfortable with, and adopt as a best practice. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 29, 2015 Share Posted August 29, 2015 (edited) gizmola, My point is that it does not generate a notice when used within a double quoted string. The manual does discourage leaving the quotes off non-numeric keys in most circumstances - as you stated. But, within a double quoted string it is proper and valid to leave them off per the manual. error_reporting(-1); $foo['bar'] = 'output'; echo $foo[bar]; echo "<br><br>"; echo "$foo[bar]"; The output of the above is Notice: Use of undefined constant bar - assumed 'bar' in E:\xampp\htdocs\index.php on line 7output output No notice for the instance where the array key has no quote marks when used within a double quoted string. It is my opinion that making one instance valid and another not is a bad idea. I would suggest the second instance should generate a notice. Edited August 29, 2015 by Psycho 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.