Program to show the table of a number in PHP

Program to show a table of a number till 10

In this tutorial, we will learn that how to print the table of a number and number will be input by the user.

For this purpose, we need to make two text boxes in a form and then assign names to each of the form element. For example, here we assign the following names.

  • Input type text is named as “value”.
  • Input type submit is names as “btn“.

Now we can put the values of form elements “value” and “btn” in some variable by POST method and further necessary actions will be carried out.

<form Method="post">
<br><br>
Enter number:<input type="text" name="value"/><br><br>
<input type="submit" name="btn" value="print table" />

</form>
<?php
if(isset($_POST['btn']))
{
	$n=$_POST['value'];
	$res=0;
	for($i=1;$i<=10;$i++)
	{
		$res=$n*$i;
		echo $n;
		echo"*";
		echo $i;
		echo "=";
		echo $res;
		echo "<br>";
	}
}
?>

Output.

show table of a number in php

Figure: table of a number in php.

Line No CodeExplanation
8if(isset($_POST[‘btn’]))It will check that values are successfuly POSTED from form or not.
10$n=$_POST[‘value’];Right side takes the values from the form element (named as “value”) and this value is the value which is posted by user with the help of form.

Now we can put the value of right side into left side into the variable $n.

Program to show a table of a number till 20.

In this example, we will take the inputs from the user and store them in variables.

<form Method="post">
<br><br>
Enter number:<input type="text" name="value"/><br><br>
<input type="submit" name="btn" value="print table" />

</form>
<?php
if(isset($_POST['btn']))
{
	$n=$_POST['value'];
	$res=0;
	for($i=1;$i<=20;$i++)
	{
		$res=$n*$i;
		echo $n;
		echo"*";
		echo $i;
		echo "=";
		echo $res;
		echo "<br>";
	}
}
?>

 

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

Your email address will not be published. Required fields are marked *