In this PHP tutorial, you shall learn how to concatenate a given string and number using String Concatenation Operator, with example programs.

PHP – Concatenate string and number

To concatenate string and number in PHP, convert the number to string using strval() function and then use string concatenation operator to concatenate both the values.

Syntax

The syntax to concatenate the string str and number n is

</>
Copy
$str . strval($n)

The function returns a string value.

Examples

1. Concatenate string and number

In this example, we take a string in str and a number in n. We concatenate these two values into a string.

PHP Program

</>
Copy
<?php
  $str = 'apple';
  $n = 542;
  $output = $str . strval($n);
  echo $output;
?>

Output

Conclusion

In this PHP Tutorial, we learned how to concatenate string and number, using strval() function and string concatenation.