This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 3 years ago.
Why does the following code output 0?
It works with numbers instead of strings just fine. I have similar code in JavaScript that also works. Does PHP not like += with strings?
<?php
$selectBox = '<select name="number">';
for ($i=1; $i<=100; $i++)
{
$selectBox += '<option value="' . $i . '">' . $i . '</option>';
}
$selectBox += '</select>';
echo $selectBox;
?>
This is because PHP uses the period character . for string concatenation, not the plus character +. Therefore to append to a string you want to use the .= operator:
for ($i=1;$i<=100;$i++)
{
$selectBox .= '<option value="' . $i . '">' . $i . '</option>';
}
$selectBox .= '</select>';
In PHP use .= to append strings, and not +=.
Why does this output 0? [...] Does PHP not like += with strings?
+= is an arithmetic operator to add a number to another number. Using that operator with strings leads to an automatic type conversion. In the OP's case the strings have been converted to integers of the value 0.
More about operators in PHP:
Reference - What does this symbol mean in PHP?
PHP Manual – Operators
PHP syntax is little different in case of concatenation from JavaScript.
Instead of (+) plus a (.) period is used for string concatenation.
<?php
$selectBox = '<select name="number">';
for ($i=1;$i<=100;$i++)
{
$selectBox += '<option value="' . $i . '">' . $i . '</option>'; // <-- (Wrong) Replace + with .
$selectBox .= '<option value="' . $i . '">' . $i . '</option>'; // <-- (Correct) Here + is replaced .
}
$selectBox += '</select>'; // <-- (Wrong) Replace + with .
$selectBox .= '</select>'; // <-- (Correct) Here + is replaced .
echo $selectBox;
?>
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
i have been trying to name a form element to a PHP variable
Echo ('<br><br>' . $number1 . ' x ' . $number2 . ' = <input type="text" name="number $i ">' ); // Print out the sums
Do it the same way you did $number1 and $number2, with string concatenation.
Echo ('<br><br>' . $number1 . ' x ' . $number2 . ' = <input type="text" name="number' . $i . '">' ); // Print out the sums
For one of our projects we need a value input that supports number localization in IE, and as can be seen here (link) IE doesn't support comma separation. To work around this issue I figured to make a dropdown that has all values with commas.
I've run into a strange problem in automatically generating this range. It works for all values except 0. I hope there is a solution out there (or a logical explanation) to solve or help me understand this strange behaviour.
The code I use:
$select_number = "";
for ($i = 10; $i >= 0; $i -= 0.1){
$value = str_replace(".", ",", $i );
$select_number .= "<option value='" . $value . "' $selected>" . $value . "</option>";
}
this code is placed in the interface with
<select class="form-control" id="myname" data-live-search="true" name="myname">
<?php echo $select_number; ?>
</select>
This renders the the following dropdown
Notice that 0 became 1,87905246918E-14. If I change the order of the for loop to for($i = 0; $i <= 10: $i += 0.1) everything works fine... Anybody know a solution to this?
I use php version 5.6.28
Loop through the integers instead:
$select_number = "";
for ($i = 100; $i >= 0; $i -= 1){
$value = str_replace(".", ",", $i/10 );
$select_number .= "<option value='" . $value . "' $selected>" . $value . "</option>";
}
Floating-point numbers suffer from round-off error.
How to get a sequence like (50 difference)
100,150,200,250,.....,900.
using 2 values (100 & 900).
My try gets an increment with 50,
for($i=100;$i<900;$i++)
{
$ii=$i+50;
echo '<option value="'.$ii.'">'.$ii.'</option>';
}
Go simple like this:
for($i=100;$i<=900;$i+=50)
{
echo '<option value="'.$i.'">'.$i.'</option>';
}
Also if you wanted to include 900, use <= not just <
Your Eval
Another option: use range() (http://php.net/manual/en/function.range.php)
$sequence = range(100, 900, 50);
foreach ($sequence as $step) {
echo '<option value="' . $step . '">' . $step . '</option>';
}
This is less memory efficient than a for loop, but I believe it is more readable. In PHP 5.5 you can make it both efficient and readable by defining a generator version of range (a-la the Python xrange). See the first example here: http://php.net/manual/en/language.generators.overview.php
I need to include the - to my database whenever I select the year, but only, 2006 2008 are saving in my database. How is this guys?
('<option value="'.$year2. " " . $i .'" '.$selected.'> '.$year2.' - '.$i.'</option>'."\n");
Put it in the value attribute, like this:
('<option value="'.$year2. " - " . $i .'" '.$selected.'> '.$year2.' - '.$i.'</option>'."\n");
Then you should put the - in the value attribute and not just the label text.
use this
'<option value="'.$year2. " " . $i .'" '.$selected.'> '.$year2." - ".$i.'</option>'."\n";
I have a column(apps) in the database which has a list of words separated with commas like - AAA, BBB, CCC
I have another column(appsSelected) that will be populated in the same format with the words that the user checks with check boxes values.
On one page i list all of the words in apps with check boxes beside them, on submit it inserts those checked into appsSelected.
On another page it lists only the words in the appsSelected column, I have managed to grab appsSelected and explode it to list each word.
<?php
$appsString = $pi_row['appsSelected'];
// break $appsString using the comma as the delimiter
$appliances = explode(', ', $appsString);
// loop through and print all the words
echo '<ul>';
for ($i = 0; $i < count($appliances); $i++)
{
echo '<li>' . $appliances[$i] . '</li><br/>';
}
echo '</ul>';
?>
The problem i have is when i go back to the page with check boxes, i want them to stay checked on the words that are in appsSelected.
So im guessing i have to compare apps with appSelected and echo out either a checked box or un-checked depending on what words match.
for ($i = 0; $i < count($appliances); $i++)
{
$codes = array($pi_row['apps']);
$codesSelected = array($pi_row['appsSelected']);
//if (in_array($appliances[$i], $codesSelected)) {
if (array_intersect($ap, $aps)) {
echo '<li><input type="checkbox" checked="checked" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
} else{
echo '<li><input type="checkbox" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
}
}
The code above only works if appsSelected matches the entire array in apps:
for example in the apps column - AAA, BBB, CCC
if i tick AAA and BBB or any other combonation that doesnt involve matching every word in apps no check boxes get ticked but if i tick AAA, BBB, CCC the check boxes will return all ticked
I need to find a way to see if any word in apps matches any word in appSelected.
Use in_array like you did in your comment, you have to pass it the correct array though:
$codesSelected = explode(', ', $pi_row['appsSelected']);
for ($i = 0; $i < count($appliances); $i++)
{
if (in_array($appliances[$i], $codesSelected)) {
echo '<li><input type="checkbox" checked="checked" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
} else {
echo '<li><input type="checkbox" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
}
}