In a form I created a loop to show a list of files and next to each file a check box. To give each check biox a unique name, I wanted to add the number of the file to it. So first I did:
<input type="checkbox" name="<?php echo 'upload' . $a-1; ?>">
I made the calculation because I already increased $a before the HTML code came. The result was:
<input type="checkbox" name="-1">
The trick is to put the calculation between brackets, like this:
<input type="checkbox" name="<?php echo 'upload' . ($a-1;) ?>">
And this results in the 'name' being: "upload1", "upload2" etc.
My website: www.dejongfotografie.nl
Showing posts with label string. Show all posts
Showing posts with label string. Show all posts
Saturday, 21 April 2007
Sunday, 18 March 2007
PHP: comparison of integer with string
PHP does not have explicit declarations of variable types. Whatever you put in a variable or array defines what the variable's type is. String, integer, boolean, float etc.
I have two variables, both containing a value. However somehow one of the variables is seen as a string, so when doing a comparison between the two strings it did not give a result. The variable was not explicitely loaded as a string, a value from the url is put in there using the GET method.
$var1=33 (integer)
$var2="33" (string)
if($var1 == $var2) {echo 'equal';}
This does not give a match. A quick although not very neat resolution is to make sure that var2 is seen as an integer. This can be done by using an arithmatic operator on the variable.
$var2++; //var2=34
$var2--; //var2=33
if($var1 == $var2) {echo 'equal';}
Results in the if-statement being true.
I have two variables, both containing a value. However somehow one of the variables is seen as a string, so when doing a comparison between the two strings it did not give a result. The variable was not explicitely loaded as a string, a value from the url is put in there using the GET method.
$var1=33 (integer)
$var2="33" (string)
if($var1 == $var2) {echo 'equal';}
This does not give a match. A quick although not very neat resolution is to make sure that var2 is seen as an integer. This can be done by using an arithmatic operator on the variable.
$var2++; //var2=34
$var2--; //var2=33
if($var1 == $var2) {echo 'equal';}
Results in the if-statement being true.
PHP: escaping double quotes
In PHP you sometimes have double quotes (this one: " ) in a string. Because double quotes also indicate the start and end of the string you have to escape them when they are within the string. Today I had the following example. I needed to put a piece of HTML to show an image into a string. So I tried:
$prev = "<img src="leftgr.GIF" width="13" height="13" border="0" >";
ofcourse this did notwork as PHP thinks the string ends with the second double quote "<img src=". It should be:
$prev = "<img src=\"leftgr.GIF\" width=\"13\" height=\"13\" border=\"0\" >";
The backslash (this one: \) escapes characters. PHP knows then then the character does not belong to the PHP code but is just a character in a string.
My website: www.dejongfotografie.nl
$prev = "<img src="leftgr.GIF" width="13" height="13" border="0" >";
ofcourse this did notwork as PHP thinks the string ends with the second double quote "<img src=". It should be:
$prev = "<img src=\"leftgr.GIF\" width=\"13\" height=\"13\" border=\"0\" >";
The backslash (this one: \) escapes characters. PHP knows then then the character does not belong to the PHP code but is just a character in a string.
My website: www.dejongfotografie.nl
Subscribe to:
Posts (Atom)