Logo F2FInterview

PHP Interview Questions

Q   |   QA

Variables included in double-quoted strings will be interpolated. Their values will be concatenated into the enclosing strings. For example, two statements in the following PHP script will print out the same string:

<?php
$variable = "and";
echo "part 1 $variable part 2 ";
echo "part 1 ".$variable." part 2 ";
?>

This script will print:
part 1 and part 2
part 1 and part 2

There are 3 formats to include variables in double-quoted strings:

  • "part 1 $variable part 2" - This is the simplest format to include a variable in a string. The variable name starts with the dollar sign and ends at the first character that can not be used in variable name. Space is good character to end a variable name.
  • "part 1${variable}part 2" - This format helps you to clearly end the variable name. The variable name starts at dollar sign before the open brace (${) and ends at the close brace (}).
  • "part 1{$variable}part 2" - This format is also called complex format. You use this format to specify any complex variable expression in the same way as in a normal statement. The variable expression starts at ({$) followed by a variable name and ends at (}).


Here is a PHP script example of different ways to include variables in double-quoted strings:

<?php
$beer = 'Heineken';
echo "$beer's taste is great. ";
echo "He drank some ${beer}s and water. ";
echo "She drank some {$beer}s and water. ";
?>

This script will print:

Heineken's taste is great.
He drank some Heinekens and water.
She drank some Heinekens and water.

There are 2 formats to include array elements in double-quoted strings in PHP:

  • "part 1 $array[key] part 2" - This is called simple format. In this format, you can not specify the element key in quotes.
  • "part 1 {$array['key']} part 2" - This is called complex format. In this format, the array element expression is specified in the same way as in a normal statement.

Here is a PHP script example of different ways to include variables in double-quoted strings:

<?php
$fruits = array('strawberry' => 'red', 'banana' => 'yellow');
echo "A banana is $fruits[banana]. ";
echo "A banana is {$fruits['banana']}. ";
?>

This script will print:

A banana is yellow.
A banana is yellow.

"A banana is $fruits['banana']. " will give you a syntax error.

Any character in a string can be accessed by a special string element expression:

$string{index} - The index is the position of the character counted from left and starting from 0.

Here is a PHP script example:

<?php
$string = 'It's Friday!';
echo "The first character is $string{0} ";
echo "The first character is {$string{0}} ";
?>

This script will print:

The first character is It's Friday!{0}
The first character is I

The string element expression, $string{index}, can also be used at the left side of an assignment statement. This allows you to assign a new character to any position in a string. Here is a PHP script example:

<?php
$string = 'It's Friday?';
echo "$string ";
$string{11} = '!';
echo "$string ";
?>

This script will print:

It's Friday?
It's Friday!

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: