How to show the integer of multiplier of the number using loop?

Hi there, I just started learning PHP a few weeks ago and I’m still having troubles with the basics, can someone help me out? How do I show the amount (starting from 1) of multiplier of the number(float) using loop?

For example I would like it to be something like this:

If the user typed in ‘7.1’ under 'Multiple of: ', and ‘5’ under 'Amount: ', the output will be:


Here’s a picture of my code:

It doesn’t need to be ‘While’, ‘For’ is totally okay too! Apologies if this is considered as a dumb question

First thing! Edit your question to include the actual text of your code - you can use markdown to format it correctly. This will make it easier for other people to read and copy / paste your code, which makes it easier for them to help you.

Second thing! Use variable names that describe the purpose of a value, not just it’s type. $Float is no good as it doesn’t tell you what the variable is for. $multiples_of would be better.

Third thing! Use a consistent coding style. You’ve capitalised $Float but left $integer all lower case; this will trip you up in larger scripts, so it’s better to get into a habit now. $snake_case and $camelCase are two popular options. You should also make sure your indentations are consistent; try and vertically match up the lines that open and close blocks in your code:

// This looks weird - it's difficult to easily see what's doing what
    while ($x == 0) {
    if ($integer <= 0){

// This is more clear
while ($x == 0) {
    if ($integer <= 0) {

Fourth thing! When echoing a variable on its own, you don’t need to surround it with quotes.

// Will work, but unneeded
echo "$result";

// Will work, and cleaner
echo $result;

As for your actual problem… For each number between one and the user’s submitted multiplier, you want to print the product of that number and the user’s submitted base. You can do this like so:

$end_multiplier = (float) $_POST['num'];
$base = (int) $_POST['amt'];

for ($multiplier = 1; $multiplier <= $end_multiplier; $multiplier ++) {
    echo $multiplier * $base, '<br>';
1 Like

Hi, Next time I will try to include the actual text of my code, thank you for reminding me to add that :’) As for the actual code, It now works perfectly fine, thank you so much!

Sponsor our Newsletter | Privacy Policy | Terms of Service