spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Introduction to PHP Programming

By PHP Catalyst
2007-11-19


Printing Strings in PHP

In this section, we will learn to print strings. Also, see how to print values of variables along with strings, with help of some examples.

Simple Printing

Simple printing in PHP means that we will assign a string to a variable and print it's value in the output. Let us begin with this simple example.

<?php
echo "This is a string";
//Output- This is a string

echo "String
in
multiple lines
also work"
;
//Output- String in multiple lines also work

echo "Let's rock";

//writing the above using single quote
//will not work without a escape character
//in this case it is a backslash \

echo 'Let\'s Rock';
//Try the above without a \ and see the difference!

echo 'First line \n Second line'; //will not work - single quote

echo "New line \n second line"; //will work - double quotes
?>

In the above example, we used a backslash when we enclosed the string 'Let's Rock' in single quote. This is called escaping. This is essential when you want to avoid PHP from processing certain portion of code. For example, if you can put \ in front of variable $string in your echo statements to avoid printing the value of the variable, such as \$string. Sometimes, you may have to put more backslashes, like \\\$string

Printing Variables

We will now look at assigning a string to a variable and printing it's value as an output. See this example:

<?php
$string
= '100 pages'; //Assigning a string to a variable

echo 'I want to print $string';
//Output: I want to print $string

echo "I want to print $string";
//Output: I want to print 100 pages

echo "I will print $string's";
//this will work as a variable name can not have
//the ' character!

echo "I will print $strings";
//will not work, it will look for a variable name $variables

echo "I will print {$string}";
//will work, {} are not considered as part of variable names

echo "I will print ${string}";
//will work, {} are not considered as part of variable names
?>

You must try to experiment with the above code and modify it to see how it works. That will be a great learning experiencing for you!

Let's also take a look at some built-in constructs of PHP that we can make use of to print one or more strings. SOme of these are:

  • print and
  • printf

These built-in constructs can be used in the same way we use 'echo' to print strings.

We will now move on to our next section on String Comparison



Tutorial Pages:
» What can I do with PHP?
» Popoular Features of PHP
» Basics of PHP
» Variables in PHP
» Data Types in PHP
» Expressions and Operators
» Control Structures in PHP
» Functions in PHP
» Declaring Functions in PHP
» Scope of a Variable
» Built-in Functions in PHP
» Handling Strings in PHP
» Printing Strings in PHP
» String Comparisons in PHP
» Manipulating Strings in PHP
» Arrays in PHP
» Types of Arrays in PHP
» Creating Arrays in PHP
» Array Operations in PHP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: