Tuesday, July 3, 2018

Arrays in PHP

An array stores multiple values in one single variable:

Example

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Run example »

Concatenation of two strings in PHP


How to combine two strings in PHP

Answer: Use the PHP Concatenation Operator

You can use the PHP concatenation operator (.) to combine or join two strings together in PHP. This operator is specifically designed for strings.
  • <?php
  • $str1 = 'Hello';
  • $str2 = 'World!';
  • $new_str = $str1 . ' ' . $str2; 
    // variable . string . another varable
  • echo $new_str; // Outputs: Hello World!
  • ?>
Code #1:
<?php
 
// First String
$a = 'Hello';
 
// Second String
$b = 'World!';
 
// Concatenation Of String
$c = $a.$b;
 
// print Concatenate String
echo " $c \n";
?>
Output :
 HelloWorld!

Code #2 :


<?php
 
// First String
$fname = 'John';
 
// Second String
$lname = 'Carter!';
 
// Concatenation Of String
$c = $fname." ".$lname;
 
// print Concatenate String
echo " $c \n";
?>
Output :
John Carter!

Code #3 :
<?php
 
// First String
$a = 'Hello';
 
// now $a contains "HelloWorld!"
$a. = "World!";
 
// Print The String $a
echo " $a \n";
?>
Output :
 HelloWorld!