how to get PHP variable in HTML form element [duplicate] - php

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

Related

Convert SQL query with COALESCE and LEFT JOIN to PHP [duplicate]

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;
?>

Why addition can't be performed while echoing concatenation in PHP [duplicate]

This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
PHP concatenation of strings and arithmetic operations
(3 answers)
php concatenating string math operation
(4 answers)
Why do you have to add parentheses to + - operations when concatenating?
(1 answer)
Closed 4 years ago.
Edit: Sorry, wasn't aware that this question was already answered.
Consider this php code:
<?php
$var1 = 10;
$var2 = 12;
echo "The addition of " . $var1 . " and " . $var2 . " is " . $var1 + $var2 . ".";
?>
The output of the above code is:
12.
But, while doing any other mathematical operation like multiplication or division like done below
<?php
$var1 = 10;
$var2 = 12;
echo "The addition of " . $var1 . " and " . $var2 . " is " . $var1 * $var2 . ".";
echo "<br>";
echo "The addition of " . $var1 . " and " . $var2 . " is " . $var1 / $var2 . ".";
?>
This time, the output comes like this:
The addition of 10 and 12 is 120.
The addition of 10 and 12 is 0.83333333333333.
Of course, keeping the operation in brackets solves this, but I'm curious to why it showed only "12."

Sum in echo php [duplicate]

This question already has answers here:
Concatenation with addition in it doesn't work as expected
(2 answers)
Closed 7 years ago.
I'm a little bit discouraged because of PHP behavior.
Why does PHP interprets my code in this way:
echo "We has over " . 2500 + 500 . " employees over the world"; // 500 employees over the world
You can also change the above code into:
<?php
$sum=2500 + 500;
echo "We has over " . $sum . " employees over the world";
?>

Can't Sort PHP Array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anybody tell me why I can't get this to work?
I am simply trying to sort my arrays from highest to lowest.
$stuff[] = "100";
$stuff[] = "104";
$stuff[] = "102";
$stuff[] = "103";
$stuff[] = "101";
$stuff[] = "99";
echo "Largest: " . max($stuff) . " <br> \n";
arsort($stuff);
echo "0 : " . $stuff[0] . " <br> \n";
echo "1 : " . $stuff[1] . " <br> \n";
echo "2 : " . $stuff[2] . " <br> \n";
echo "3 : " . $stuff[3] . " <br> \n";
echo "4 : " . $stuff[4] . " <br> \n";
echo "5 : " . $stuff[5] . " <br> \n";
arsort() is sorting your array, but it also conserve the keys of each value. Witch mean the key 0 will still answer with 100, and not the first element !
With a foreach, you would see them in correct order.
foreach ($stuff as $value) {
echo $value . "<br>\n";
}
OR
You can also use rsort() that will not be associative, which mean the keys won't have the same values after it.

A very basic php query related to string concatenation and sum

<?php
$number1 = 1;
$number2 = 2;
echo $number1.' + ' . $number2. ' = '.$number1+$number2;
?>
See the above program. It is giving output as 3. Why not it is giving output as:
1 + 2 = 3
+ and . have the same precedence.
echo $number1.' + ' . $number2. ' = '.($number1+$number2);
The operations are applied in order. I.e:
echo $number1.' + ' . $number2. ' = '.$number1+$number2;
Becomes:
echo '1 + 2 = '.$number1+$number2;
Becomes:
echo '1 + 2 = 1'+$number2;
Since this is addition PHP will convert the string to an int which gives 1.
So the final expression is:
echo 1 + 2;//Prints 3
You can indicate which operations to perform together using brackets:
echo $number1.' + ' . $number2. ' = '.($number1+$number2);
your desired output :-
<?php
$number1 = 1;
$number2 = 2;
echo $number1.'+ ' . $number2.'='.($number1+$number2);
?>

Categories