PHP Template String

Sheeraz Gul Feb 08, 2022
  1. Use the echo Keyword to Generate a Template String in PHP
  2. Use PHP strstr() to Generate a Template String
  3. Use str_replace() to Generate Template Strings in PHP
  4. Creating Our Function to Generate Template Strings in PHP
PHP Template String

The template strings are the dynamic strings. Suppose you have a list of names and tasks, and you want to print each name and task name in a string one by one.

You can set a template string and then print the list one by one.

Python has a built-in function Template(), which converts a string with variables into one string.

Let’s see how it can be done in PHP.

Use the echo Keyword to Generate a Template String in PHP

The most simple way to generate a template string is the php keyword echo, and we can put different variables anywhere in the string and print it.

<?php
$name = "Jack";
$last_name= "Enfield";
$job = "Software Engineer";
$company = "Microsoft";
echo "Employee name is {$name} {$last_name}, he is a {$job} at {$company}.";
?>

The code above generates a template string with the given variable’s name, last name, job, and company.

Output:

Employee name is Jack Enfield, he is a Software Engineer at Microsoft.

We can put the variables in an array with multiple values and set foreach loops and conditions to print multiple template strings with different values.

Use PHP strstr() to Generate a Template String

PHP has a built-in function strstr() which generates a template string. It takes two parameters, one is the template string, and the other is the array with values.

<?php
$output_string = 'Employee name is $name $last_name, he is a $job at $company.';

$values = array( '$name' => 'Jack', '$last_name' => 'Enfield', '$job' => 'Software Engineer', '$company' => 'Microsoft');

echo strtr($output_string, $values);
?>

The output for the code above will be the string with the values from the array.

Output:

Employee name is Jack Enfield, he is a Software Engineer at Microsoft.

What if there is information of multiple employees? In that case, we can use a multidimensional array and foreach loop to generate multiple strings.

<?php
$output_string = 'Employee name is $name $last_name, he is a $job at $company.';

$values = array(
    array( '$name' => 'Jack' , '$last_name' => 'Enfield' , '$job' => 'Software Engineer', '$company' => 'Microsoft'),
	array( '$name' => 'Samuel', '$last_name' => 'Stevens', '$job' => 'Software Tester', '$company' => 'Apple'),
	array( '$name' => 'Mike', '$last_name' => 'Geller', '$job' => 'Salesman', '$company' => 'Amazon'),
	array( '$name' => 'John', '$last_name' => 'Clay', '$job' => 'Manager', '$company' => 'Merriot Hotels')
	);

foreach($values as $val){
    echo strtr($output_string, $val);
	echo "<br>";
}
?>

Output:

Employee name is Jack Enfield, he is a Software Engineer at Microsoft.
Employee name is Samuel Stevens, he is a Software Tester at Apple.
Employee name is Mike Geller, he is a Salesman at Amazon.
Employee name is John Clay, he is a Manager at Merriot Hotels.

Use str_replace() to Generate Template Strings in PHP

The str_replace() is another PHP built-in function to generate string.

It takes three parameters, first is the array of values to be replaced, second the values, and third the template string.

<?php
$pass = array('%name%', '%last_name%', '%job%', '%company%' );
$value = array('Jack', 'Enfield', 'Software Engineer', 'Microsoft');
echo str_replace($pass, $value, "Employee name is %name% %last_name%, he is a %job% at %company%.");
?>

The str_replace() will replace the values with %% to the given values and generate a string from the given template string.

Output:

Employee name is Jack Enfield, he is a Software Engineer at Microsoft.

Creating Our Function to Generate Template Strings in PHP

We can also create our function for generating template strings.

<?php
$template = function ($values) {
    extract($values);
    return "Employee name is $name $last_name, he is a $job at $company.";
};
$values = array( 'name' => 'Jack', 'last_name' => 'Enfield', 'job' => 'Software Engineer', 'company' => 'Microsoft');
//No $ in the values array because extract function will itself detect values for variables.
echo $template($values);
?>

The template function will first extract the values from the array and then return a string.

Output:

Employee name is Jack Enfield, he is a Software Engineer at Microsoft.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP String