How to append two strings in PHP
In this post we will discuss about append two strings in PHP. PHP (Hypertext Pre processor) is an interpreter and open source script language that is used on Linux Web Servers. It is mainly used for web development and can be embedded into HTML.It can also be used as a programming language. The best feature of PHP is it is very simple and easy to learn. Compared to other programming languages like C or Perl, PHP contains HTML with embedded codes.
Requirements to run PHP
PHP is a server side scripting language where the PHP codes can be handled directly on the web server. To run PHP code, you must just to install the following in your machine.
- Install a Web Server (e.g., Apache)
- Install a PHP (Interpreter)
- Install a MySQL Database (optional)
Note: If your server supports PHP, then create the .php files and place them on your web directory. You do not need to compile them. After discussing what is php lets go through the actual code to append two strings using php language. PHP is considered as the most used language on web for websites specially wordpress websites.
Basic PHP Syntax
A PHP script can be placed anywhere in the code. The PHP code is enclosed with a beginning as <?php and end as ?> .
Example:
<?php //PHP Code ?>
The file extension for PHP file is “.php”.
PHP Operators
The PHP operators are used to perform operations on variables and values. PHP supports more than 50 types of operators, starting from arithmetical operations to the logical comparison. Few operators that are generally used are as follows:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Concatenation Operators
Let us discuss more on PHP concatenation operators.
PHP Concatenation Operators
There are two operators that are used for strings in the concatenation.
Operator | Description |
. | Concatenation operator to combine two strings to create one ($txt1 . $txt2) |
.= | Concatenation assignment. |
Appending two or more strings is string concatenation. You can easily concatenate the strings in PHP. To perform a string concatenation, use the concatenation operator “.” (dot). Echo is used to output the data to the screen. It can be used with parentheses or without parentheses i.e echo ( ) or echo.
Append two strings in php Code – 1
<?php $str1 = 'This ‘; $str2 = 'is a ‘; $str3 = 'test string‘; $full= $str1.$str2.$str3; echo$full; ?>
The output for the above code looks like:
“This is a test string”
Further, use the operator “.=” to append a string to an existing code as follows:
Append two strings in php Code – 2
<!--?php $str = 'This is the base string'; $str .= ' and this is added; echo$str; ?-->
The output for the above code looks like:
“This is the base string and this is added”
Appending Strings and Variables
We can combine variables with strings, but cannot append with complex data types (i.e arrays or objects into strings).
Examples of concatenating integers looks like:
Code
<?php $i = 90; $str = $i.' is a number'; echo$str; ?>
The output for the above code looks like:
90 is a number
Examples of concatenating floats looks like:
Code <?php $percentage = 15.5 + 12 + 35.17; echo 'Your final score: '.$percentage.' out of 100%.'; ?>
The output for the above code looks like:
Your final score: 62.67 out of 100%.
Though PHP is focused on server side scripting, you can play more with PHP to make it interesting. Other areas where PHP is used are command line scripting and while writing desktop applications. PHP supports almost all the web servers and used in all operating systems. One of the significant features of PHP is it supports a wide range of databases. It also works as a module or as a CGI processor.
For more PHP projects and codes please visit.