Jump to content

How do you define constants that have been posted from a form?


rocky48

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

@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 #8)

 

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 7

output
 

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 by Psycho
Link to comment
Share on other sites

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.