How Many Ways to Include Array Elements in Double-Quoted Strings using PHP?
Submitted by: AdministratorThere 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.
Submitted by: Administrator
► "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.
Submitted by: Administrator
Read Online PHP Job Interview Questions And Answers
Top PHP Questions
☺ | What is the difference between PHP4 and PHP5? |
☺ | What is the difference between characters \023 and \x23? |
☺ | How To Download and Install PHP on Windows? |
☺ | What are the difference between abstract class and interface? |
☺ | How many ways we can pass the variable through the navigation between the pages? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |