Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

PHP Program to replace the words with form and database

PHP Program to replace the words with form and database

In this tutorial, we will learn about the followings.

  1. PHP Program to replace the words 
  2. PHP Program to replace the words with form 
  3. PHP Program to replace the words with  database

PHP Program to replace the words. 

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.

<html>
<head>
	<title>Replace the words</title>
</head>
<body>
<?php
for ($i=1; $i <=100 ; $i++) { 
	if ($i%3==0 && $i%5==0) {
		echo $i."NO.1"."<br>";
	}elseif ($i%3==0) {
		echo $i."WEBSITE"."<br>"."<br>";
	}elseif ($i%5==0) {
		echo  $i."T4TURORIALS.COM"."<br>"."<br>";
	}else{
		echo $i."<br>";
	}
}
?>
</body>
</html>

PHP Program to replace the words with form.

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

<html>
<body>
	<form method="post">
		<input type="text" name="num">
 		<button name="sub">Clik_Me</button>
	</form>
</body>
</html>
<?php
if (isset($_POST['sub'])) {
	$num=$_POST['num'];

for ($i=1; $i <= $num; $i++) { 
	if ($i%3==0 && $i%5==0) {
		echo $i."NO.1"."<br>";
	}elseif ($i%3==0) {
		echo $i."WEBSITE"."<br>"."<br>";
	}elseif ($i%5==0) {
		echo  $i."T4TUTORIALS.COM"."<br>"."<br>";
	}else{
		echo $i."<br>";
	}
}
}
?>

PHP Program to replace the words with the database.

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>
<body>
<form method="post">
		<input type="text" name="num">
 		<button name="sub">Clik_Me</button>
	</form>
</body>
</html>
<?php
$conn=mysqli_connect('localhost','root','','t4tuto');
if (isset($_POST['sub'])) {
	$num=$_POST['num'];
$sql="INSERT INTO tutorials(num)VALUES('$num')";
$run=mysqli_query($conn,$sql);
for ($i=1; $i <= $num; $i++) { 
	if ($i%3==0 && $i%5==0) {
		echo $i."NO.1"."<br>";
	}elseif ($i%3==0) {
		echo $i."WEBSITE"."<br>"."<br>";
	}elseif ($i%5==0) {
		echo  $i."T4TUTORIALS.COM"."<br>"."<br>";
	}else{
		echo $i."<br>";
	}
}
}
?>

Database to replace the words 

Replace the words program in php form database

Figure: Replace the words program in PHP form database.

 

Prof. Fzal Rehman Shamil
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325