Interpolating Variables in Strings

The values in variables can be interpolated if you put them into double-quoted strings.

<html>
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$welcome = "Welcome to PHP class";
echo "$welcome. We have a lot of materials to learn";
?>
</p>
</body>
</html>

View the effect

If you want to add a word to the output of the variable, enclose the variable that you're interpolating in curly braces { and }.

<html>
<head>
<title>Interpolating Variables in Strings</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$meat = "meat";
echo "Do you like to eat spaghetty with $meatballs?<br /><br />";
// enclose the variable with curly braces
echo "Do you like to eat spaghetty with {$meat}balls?<br /><br />";
?>
</p>
</body>
</html>

View the effect


Variables | Identifiers | What are Variables? | Assigning Values to Variables | Data Types | Variable Scope | Interpolating Variables in Strings | Creating Variables Variables | Creating Constants
© 2008: Hann So
email: hso@voyager.deanza.edu