PHP String Basics: Complete Beginner’s Guide with Examples
PHP String Basics Strings are nothing but collection of alphabets,letters.By using this letter we can form a words and sentences.These words or sentences are called as string.In php we are writing the these words or sentences in single or double quotation marks then only php threated as string data type.single quotation means(‘ ‘) and double quotation means (” “) .
i.In php string data type uses special syntax for declaration.
ii. There are four types of declaration for strings.
iii.php used to store strings in single quotations and double quotation marks.
iv.php uses string operators for concate two or more strings.
Declaration of strings
There are four types of string declaration
-
1.single quotation
-
2.double quotation
-
3.heredoc syntax
-
4.nowdoc syntax
1.single quotation syntax
single quotation in php is used to declare a simple strings.simple strings in the sence we can write message on single quotations but we cannot use any special symbols and variables to print message.
That’s why we are using this single quotations for simple messages
We cannot use any numbers on the string.single quotation do not allows these variable names, number and special characters.
Example program for single quotations:
<? php
$a=’ welcome to Siddharthone channel’;
$b=’This is php program’;
echo $a;
?>
Output:
welcome to Siddharthone channel
This is php program
2.double quotation syntax:.
In string double quotation marks are used to print statement.in double quotation it allows special symbols with the messages.single quotes for simple messages and double quotes for alla variable and special symbols, messages.
Program for double quotes:
<?php
$str=”say hello guys”.”\n;
echo “welcome to the college and $str”;
?>
Output:
say hello guys
Welcome to the college and say hello guys
In the above program str is the variable name.it store the string value as say hello guys.
Here we are using echo for printing statement in the screen.In that sentence we are adding that variable as str.
For the above program we are getting output as line by line because we are using \n for next new line.
3.Heredoc syntax:
Heredoc syntax is used for sentence without limitation.symbol for heredoc is <<< .in heredoc syntax we should write identifier after <<< heredoc symbol,then write sentence we want to print in line by line.we will get output same as you written in the program.
If you want close the heredoc syntax we just write identifier name and close phph code with greater than symbol.
Program for heredoc:
<?php
$str=<<<heredoc
Hello guys
Today we are learning strings in php
I would like to tell you is php is a programming language
It is very easy subject
heredoc;
$echo $str;
?>
Output:
Hello guys
Today we are learning strings in php
I would like to tell you is php is a programming language
It is very easy subject
Explanation:
In above program we are using heredoc syntax.heredoc syntax for multiple line to print on the console screen with declaring in quotation marks.in heredoc syntax takes sentence with variable values.
4.nowdoc syntax:
Nowdoc is same as heredoc syntax.thus is also used to print multiple line strings on screen.but the difference is nowdoc is enclosed with <<< with single quotations followed by identifier.only different is it does not take any variable value it prints same as output.
Program for nowdoc syntax:.
<?php
$str=”php”;
<<<‘nowdoc’
Welcome to the php program
Php is written with java and MySQL
We are calling $str
This is important concept in php
It is the strings concept
We can declare multiple line with quotation and \n for the next line
nowdoc;
?>
Output:
Welcome to the php program
Php is written with java and MySQL
We are calling $str
This is important concept in php
It is the strings concept
We can declare multiple line with quotation and \n for the next line
Explanation:
In the above program we are using variables as str.in nowdoc it does not take any variable value it only prints the statement written under the nowdoc program.program start with php we are using nowdoc enclosed in single quotation before we are using <<< symbols for starting program.after completion of multiple line we just write that identifier with semicolon.we are getting that output on console screen.