PHP Tutorials: String & String Operations

When working in combination with PHP and HTML you will really find this tutorial very helpful. Because in PHP you can display PHP variable value inside HTML tags. No doubt you can use simple PHP markups and echo the variable inside the HTML. But, don’t you think it will become frustrating at a point when you have to maintain and expand the code. Let us see an example of what I am saying.

<!DOCTYPE html>
<html>
<head>
	<title>My HTML and PHP Code</title>
</head>
<body>
<?php 
     $my_name = "Naresh Kumr!";
     $qualificaiton = "Masters in Computer application.";
?>
     <h1>Hello guys! My name is <?php echo $my_name; ?></h1>
     <p>I am a <?php echo $qualificaiton; ?></p>

</body>
</html>

Did you notice how many time we used the PHP start and end markups? If you get a basic understand of PHP strings and it’s operations then you can save a lot of time writing unusual code.

First we need to understand basics about Strings.

What is a String?

A explained earlier in the topic PHP: Data type a String is a sequence of characters. These characters can be

  1. Alphabets
  2. Numbers
  3. Special signs
  4. Or any combination these 1 to 3

In PHP you can define a string variable inside single quotation mark ‘Your string’.

<?php
     $my_first_string = 'Here is the string!';
?>

It’s it easy. But, there is lot more to know about PHP strings. The string can also be defined by using double quotation marks. For example

<?php
     $my_first_string = "Here is the string!";
?>

There is a very special difference between single quotation and double quotation strings.

Single Quotation String

A single quotation string is treated as literally. So whatever you type within the single quotes will display as it is. Either it is variable name or any escape sequences. Let us have an example

<?php
     $my_name = "John Doe!"
     echo 'My name is $my_name';   
?>
OUTPUT:  
My name is $my_name

Double Quotation String

A double quote string display the string which it contains as it is plus it also display the value of variable and special escape sequences. For example

<?php
     $my_name = "John Doe!"
     echo "My name is $my_name"; 
?>
OUTPUT:  
My name is John Doe!

Now you might be thinking what are escape sequences in String.

Escape Sequence

It is a sequence of characters that does not represent itself when used inside a string literal, but it is translated into an another character that may be difficult or impossible to represent directly. Sounds complicated! Right? Let me give you example of escape sequence character then you will get an idea of it.

  1. \n is used for newline
  2. \r is used for carriage-return
  3. \t is used for the tab
  4. \$ is replaced by the dollar sign itself ($)
  5. \” is replaced by a single double-quote (“)
  6. \ is replaced by a single backslash (\)

Let take an example for better understand.

<?php
     echo "This is a string.\n An another line starting from here.\t Its a tab.";
?>
OUTPUT:  
This is a string.
An another line starting from here.    Its a tab.

String Manipulation

PHP comes with a lot of inbuilt string functions which will help you coding complex applications. I am going to tell you some of String functions which get used very frequently.

Concatenation

Most importation and very frequently used is concatenation. Concatenation is basically combining 2 or more strings. It will be required which you will work with database queries on the go. Concatenation can be done using Full stop sign “.”

<?php
     $first_name = "John";
     $last_name = "Doe";
     echo "Full name is ".$first_name." ".$last_name;
?>
OUTPUT: 
Full name is John Doe
String Replace

str_replace() is used to replace a specific string or character with another character or string.

Syntax: str_replace( 'find text', 'Replace text', 'Source' );
<?php
     $source = "I read a book everyday.";
     echo $source."\n";
     
     $new_string = str_replace( 'read', 'don\'t read', $source );
     echo $new_string;
?>
OUTPUT: 
I read a book everyday.
I don't read a book everyday.
String Length

strlen() is used to find the number of character in a string.

Syntax: strlen( string );
<?php
     $source = "Hello World!";
     echo strlen($source);
?>
OUTPUT: 
12
Trim

trim() function is used to strip extra space from start and end of a string.

Syntax: trim(string)
<?php
     $source = "    This string has spaces    ";
     echo trim( $source );
?>
OUTPUT:
This string has spaces

I hope you liked the tutorial and will implement. Try some example and share your comments here.

One thought on “PHP Tutorials: String & String Operations

Leave a Reply

Your email address will not be published. Required fields are marked *