Merge Two Strings in PHP Free Source code and Tutorial

Merge Two Strings in PHP Free Source code and Tutorial

Introduction

There are a few ways to merge two strings in PHP. One way is to use the concatenation operator, “.”. This operator joins two strings together and creates a new string. In this tutorial, we will learn how to create a PHP script that will ask the user to input two strings and merge it using the concatenation operator.  The PHP script presented in this tutorial is also integrated in Bootstrap. We will use two input type text and a button. The purpose of the text box is to capture the two strings for our PHP script to merge it into one using the concatenation operator.

Objectives

By the end of this tutorial, you will be able to:

  1. The objectives of creating this tutorial are to provide step-by-step instructions how to merge two strings in PHP.
  2. Use concatenation operator in PHP.
  3. To integrate and apply the source code in your projects.

Relevant Source code

The following are the requirements for this tutorial:

  • XAMPP
  • Text editor (VS Code, Sublime, Brackets), download and install a text editor of your choice
Merge Two String in PHP Free Source code and Tutorial - source code
Merge Two String in PHP Free Source code and Tutorial – source code

Line 19-25 – this is the scope of the HTML Form, we all know that the purpose of the form is to capture the user input. In this case, we want to capture two strings and let PHP process the merging of two strings into one.

Line 26-33 – this is the scope of our PHP script. It starts at line 26 and ends at line 33.

Line 28 – this is where we declare our first variable $str1. The variable will hold the value entered by the user in the first text box.

Line 29 – this is where we declare our second variable $str2. The variable will hold the value entered by the user in the second text box.

Line 30 – this is where we will merge the two strings using the concatenation operator. We will now declare a new variable $new_str after we have merge $str1 and $str2.

Line 31 – this line is responsible for displaying the value of $new_str variable that merges the two strings entered by the user in the text boxes.

Merge Two String in PHP Free Source code and Tutorial - output
Merge Two String in PHP Free Source code and Tutorial – output
<form method="post">
<label>String 1</label>
<input type="text" name="str1" class="form-control"><br>
<label>String 2</label>
<input type="text" name="str2" class="form-control"><br>
<input type="submit" name="submit" class="btn btn-primary" value="Merge String">
</form><br>
<?php
if($_POST){
$str1 = $_POST['str1'];
$str2 = $_POST['str2'];
$new_str = $str1.' '.$str2;
echo "<h4>Merge String is: ".$new_str."</h4>";
}
?>

Video Demo

Merge Two String in PHP Free Source code and Tutorial
Merge Two String in PHP Free Source code and Tutorial

FREE DOWNLOAD SOURCE CODE

FREE DOWNLOAD PDF TUTORIAL

Summary

To wrap things up, we have learned how to use the concatenation operator in PHP.  Concatenation is used when you want to merge 2 or more strings. The bonus part is that, we have used the HTML form in order for the users to enter two strings and merge it using the concatenation operator. Source code of the tutorial as well as the video explanation is also available on this content.

We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive  free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

Related Topics and Articles:

How to Generate Random Password in PHP Free Source code and Tutorial

Email Validation in PHP Free Source code and Tutorial

Get File size and File Extension in PHP Free Source code and Tutorial

Calculate Sum of Column in PHP and MySQL

Bar Graph in PHP and MySQL Free Source code and Tutorial

, , , , , , , , , , , , , ,

Post navigation