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
- Special signs
- 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.
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.
- \n is used for newline
- \r is used for carriage-return
- \t is used for the tab
- \$ is replaced by the dollar sign itself ($)
- \” is replaced by a single double-quote (“)
- \ 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.
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.
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
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.
strlen() is used to find the number of character in a string.
Syntax: strlen( string );
<?php $source = "Hello World!"; echo strlen($source); ?>
trim() function is used to strip extra space from start and end of a 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.