Home > PHP, PHP Tutorials > Tutorial 3 – PHP Strings

Tutorial 3 – PHP Strings

October 16th, 2007 admin Leave a comment Go to comments

In the last post I covered PHP variables and how to use them. In this tutorial we will understand how to use strings in PHP. If you are set to make a career in development then you would need to use a lot of strings to make your program / script work.

What is a String?

A string is nothing but a series of characters in a sequence. E.g: “Sunil”, “Programming”, “PHP”, “Tutorial” are all examples of Strings.

Creating a String in PHP

As discussed in PHP Tutorial 2 – Variables, PHP is a loosely typed language. Therefore you don’t have to declare a variable as String to use it (as in other programming languages like Java).

To declare a String you need to use double quotes (“) or single quotes (‘). Both these approaches have some differences that we will cover later. See the example below to understand how to create strings:

<?php
$string = “This is a string”;
$name = “Sunil Bhatia”;
$data = “My name is $name”;
echo $data;
?>


In the above example $string, $name and $data are all string variables. $string holds the value “This is a string”, $name holds the value “Sunil Bhatia” and $data holds the value “My name is $name”.

Therefore you see, that a string is nothing but a series of characters in sequence.

The last example of $data is important to note and understand. $data contains value “My name is $name”. Here $name will get expanded automatically and be replaced with the value of $name i.e. Sunil Bhatia. Therefore when you echo or store this variable in a file, the output will be “My name is Sunil Bhatia”. This is called variable substitution.

Though in a real world scenario you would be using strings to store value from a database or store user inputs, but nonetheless, this example shows how to create strings.




Difference between a double quote and single quote String

When you enclose a string within double quotes, string variables and special characters like \r \n get expanded automatically. Whereas, in a single quote string data expansion does not happen. Lets look at an example below:

Creating String using Double Quotes

<?php

$name = “Sunil Bhatia”;

$data = “My name is $name”; //using double quotes
echo $data;

?>

Output:
My name is Sunil Bhatia

Creating String using Single Quotes

<?php

$name = “Sunil Bhatia”;

$data = ‘My name is $name’; //using single quotes
echo $data;

?>

Output:
My name is $name

Do you see the difference above. Data expansion of $name does not happen while using single quotes.

String Concatenation

Lets start by understanding what String Concatenation is all about. When you join two separate strings to become one its called Concatenation. To concatenate two strings in PHP you should use the dot (.) operator.

Lets look at an example below:

<?php

$first_name = “Sunil”;
$last_name = “Bhatia”;

$name = $first_name . ” ” . $last_name;

?>

In the above example I have two string variables $first_name and $last_name. I then create a third variable $name that is a concatenated string derived by concatenating $first_name and $last_name. Observe that I have also concatenated a space ” ” in between the first and the last name. So internally to derive $name, string concatenation takes place on three variables i.e. $first_name, ” ” and $last_name.




Important PHP String Functions

PHP offers a huge list of String functions to play with that you can check here http://in2.php.net/manual/en/ref.strings.php. I will cover some important functions that you could start with.

a. addslashes($string)
This function takes a string parameter and escapes all special characters in the string i.e. prepends a slash (\) before the special character. Special characters could be a quote or a double quote.

e.g.

<?php

$data = “Sunil’s hobby is writing utility software and magic”;

echo addslashes($data);

?>

Output:
Sunil\’s hobby is writing utility software and magic

Observe the added slash (\) in the output above.

You can get more information on addslashes() here http://in2.php.net/manual/en/function.addslashes.php

b. stripslashes($string)
stripslashes() function reverses the effect of addslashes() function.

e.g.

<?php

$data = “Sunil’s hobby is writing utility software and magic”;
$data2 = addslashes($data);
$data3 = stripslashes($data2);
echo $data3;

?>

Output:
Sunil’s hobby is writing utility software and magic

You can get more information on addslashes() here http://in2.php.net/manual/en/function.stripslashes.php

c. strlen($string)
This function is used to get the length of a string. This is useful if you want to find the number of characters contained in a string.

e.g.

<?php

$name = “Sunil Bhatia”;
echo “Length of string is : ” . strlen($name);

?>

Output:
Length of string is : 12

Length is 12 because 5 characters of Sunil + 6 characters of Bhatia + 1 character of space

You can get more information on strlen() here http://in2.php.net/manual/en/function.strlen.php

d. substr($string, $start, $length)

substr() is used to get a small portion of the big string i.e. it is used to get a chuck of characters from the string.

The substr() function takes three parameters i.e. $string, $start and $length. $string is used to pass data to the string function, $start informs the substr() function where to begin its operation from and $length informs the substr() function the length of characters required.

e.g.

<?php

// print ’12′
echo substr(’123456789′, 0, 2);

// print ’56789′
echo substr(’123456789′, 4);

// print ’89′
echo substr(’123456789′, -2);

// print ’456′
echo substr(’123456789′, 3, -4);
?>

You can get more information on substr() here http://in2.php.net/manual/en/function.substr.php

e. trim($string)

The trim() function is used to remove the leading and trailing spaces from a string i.e. it is used to remove the spaces in the end of the string and beginning of the string respectively.

Lets look at and example:

<?php

$name = ” Sunil Bhatia “;
echo “My name is $name, what is yours?”;

$name = trim(name);
echo “My name is $name, what is yours?”;

?>

In the first example, the output will be
My name is Sunil Bhatia , what is yours?

In the second example, the output will be
My name is Sunil Bhatia, what is yours?

Please note that if there were many spaces between Sunil and Bhatia that would not have been removed. Look at the example below:

<?php

$name = ” Sunil Bhatia “;

$name = trim(name);
echo “My name is $name, what is yours?”;

?>

Output:
My name is Sunil Bhatia, what is yours?

You can get more information on trim() here http://in2.php.net/manual/en/function.trim.php

These are some of the functions that I have covered, if you would like to study more functions I would recommend visiting this URL: http://in2.php.net/manual/en/ref.strings.php

This is all from my side on strings, in the next post I will cover PHP Arrays. Subscribe to my newsletter to receive updates as the next set of articles go live.

Previous Posts

Tutorial 2 – PHP Variables
Introduction to PHP Web Programming
PHP 5 Tutorials – Introduction

Categories: PHP, PHP Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. November 13th, 2007 at 09:11 | #1

    Very extensive but an informative read nevertheless! I always use the single quote to surround a literal string. If I include variables then I always concatenate – apparently it’s the fastest way to go about concatenation. If you’re also using a syntax highlight application, such as Zend, then you can clearly identify the variables when they’re not in quotes.

  1. October 22nd, 2007 at 23:19 | #1
Enter this code to leave comment (Sorry, but bots get me crazy :) )