Web Design, Development & Marketing

Web Development Articles

PHP: Including Variables and Special Characters in Strings

Just a simple note here on how to include variables in strings in PHP. If you create or echo a string using double quotation marks you can simply embed variables in the string like so:

<?php
$num = 20;
echo "There are $num widgets";
// outputs: There are 20 widgets
?>

If you want to include double quotation marks within a string which you have enclosed with double quotation marks, you need to escape those quotation marks within the string with backslashes. See the PHP example below:

<?php
$num = 20;
echo "There are \"$num\" widgets";
// outputs: There are "20" widgets
?>

If you use single quotation marks to enclose a string, you can concetate variables together like so:

<?php
$num = 20;
echo 'There are '.$num.' widgets';
// outputs: There are 20 widgets
?>

Here, the quotation marks are closed and opened, and full stops used to concetate the variables.

Hope that has been enlightening. Bye for now!

Subscribe to RSS Feed Bookmark and Share

Related Links

Related Articles / Posts

Barclaycard ePDQ CPI Integrations (27/05/2009)

HSBC XML API / CPI E-commerce Integrations (02/04/2009)

Recommended Open Source Invoicing System (25/03/2009)

Recent Recommended Links - October 2008 (03/10/2008)

Controlling PHP Register Globals Using .htaccess File (04/03/2008)