EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

String in PHP

Home » Software Development » Software Development Tutorials » PHP Tutorial » String in PHP

String-in-PHP

Introduction to String in PHP

Basically, we all know that a string is some collection of Characters. It is also one of the data types actually supported by the PHP programming language. Strings may contain alphanumeric characters. It can be created by the following ways listed:

  • You can declare a variable and then you can assign a string to a character
  • By using the echo statement
  • String/strings are language construct, it can help capture words.
  • Other than this learning how strings works in PHP programming language can make you manipulate how to use strings very effectively like a productive developer.

How to declare String in PHP using various methods?

There are four different types of declaration.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • By using Single quotes
  • By using Double Quotes
  • By using the Heredoc string creation method++
  • By using Nowdoc string creation method

1. PHP String creation method using Single Quotes

Creating string/strings using the Single Quotes is the easiest and the simplest of creating a string. Now let’s have a look at the below-listed PHP syntax which is creating a simple string.

Syntax:

<?php
echo 'Login to the Page to view this page template';
?>

Output:

using single quotes

If this PHP Single Quote string is a part of the string value then we make it escape using the character of backslash. Check that code syntax below.

<?php
echo 'I \'ll be in 10 minutes just wait here';
?>

Output:

string in php 2

2. PHP String creation method using the Double Quotes

Mostly in the String creation in PHP language, Double Quotes will be used. Double Quotes String declaration method can create complex strings rather than the strings created using the single quotes.

We can also use variable names inside the double quotes so that variable values will be displayed as we want. Just have a look at the example of a double-quote declaration, variable declaration so that you will understand the concept of the String Declaration using the Double Quotes.

Syntax:

<?php
$myname = 'Pavan Kumar Sake';
echo "$myname and Surendra Gandham is friends since from the College times";
?>

Output:

The above example will create a simple string variable “myname” with the value “Pavan Kumar Sake”. This string variable can now be used at any time in the PHP programming language as you want.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,731 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Double Quotes declared strings can also escape special characters. \n for line feed, \$ for the dollar sign, etc., can be used to get the outputs of empty line and dollar sign. Likewise, there are ways to escape special characters using Double Quotes.

Syntax:

<?php
echo "I want 100\$ now urgently";
echo "\n This content is appearing after providing linefeed";
?>

Output:

using double quotes

3. PHP String creation using Heredoc String creation method – PHP Heredoc

Here we will know what is Heredoc. The main methodology of Heredoc is to create the most complex strings in PHP when compared with the Double Quotes string declaration.

Heredoc will support all the features of the double-quotes. Along with that Heredoc will also allow you to create the string values without php string concatenation in more than one line. You can create as many lines with strings as you want. Multiple lines with strings can be created using the Heredoc method. One can even escape the Double Quotes inside the Heredoc method.

Let’s have an example of the PHP Heredoc method to create single/multiple string values.

Syntax:

<?php
$my_name = "Pavan Kumar Sake";
echo <<<EOT
When $my_name was a small boy,
He worked a lot and gained a lot
of knowledge yet he is simple like
the "small boy " in the childhood
EOT;
?>

Output:

using heredox string

<<<EOT is the string delimiter, EOT means End Of Text. EOT should be used/specified at the beginning of the string and also at the end. You can also use any variable declaration for <<<EOT.

Syntax:

<?php
$my_name = "PK Sake";
$eot_variable = <<<EOT
When $my_name was a small boy,
He worked a lot and gained a lot of knowledge yet he is simple like the "small boy " in the childhood
EOT;
echo $eot_variable;
?>

Output:

string in php 6 PNG

4. PHP String creation using Nowdoc String creation method – PHP Nowdoc

The PHP Nowdoc string creation method is very much similar to the PHP Heredoc string creation method but the Nowdoc method mainly works like how the single quotes work. Parsing will not take place inside the Nowdoc. Nowdoc method is ideal then we are working with the raw data which is not at all needed to be parsed.

Let us have a code example of Nowdoc method below:

Syntax:

<?php
$my_name = "Pavan Kumar Sake";
$my_variable = <<<'EOT'
When the big $my_name was a small boy,
He has done many funny things even this generation can’t imagine He is always like a "small boy "since from childhood
EOT;
echo $my_variable;
?>

Output:

creation using Nowdoc

Other than string declaration there are some string functions that will be very helpful in developing small web programs to the large projects. Kindly have a look at them. Also, know what is the output difference between the string double quote declaration and the single quote declaration.

strtolower, strtoupper, strlen, explode, substr, str_replace, strops, sha1, md5, str_word_count, ucfirst, lcfirst are the string functions. These string functions are very helpful in creating projects or Programs.

“strtolower” converts all the string characters to the lowercase letters whereas “strtoupper” will convert them to upper case letters. “strlen” will provide the string length(characters count). “explode” function will convert strings into array variables. “substr” will return part of the string by using 3 basic parameters. “str_replace” will replace some string content using 3 basic arguments. “strpos” function will return the position of the specific string. “sha1” function returns hash code of the string. “md5” function provides md5 hash value for the string. Likewise, other strings function too.

Syntax:

<?php
echo strtolower("1. PAVAN kumar is a Writer  ");
echo strtoupper("2. PAVAN kumar is a Writer  \n");
echo "3. ";
echo strlen("PAVAN kumar is a Writer");
echo substr(" 4. PAVAN kumar is a Writer  ",0,12);
echo str_replace('PAVAN','VASU'," 5. PAVAN kumar is a Writer  \n");
echo "6. ";
echo strpos("PAVAN kumar is a Writer  ",'kumar');
echo " 7. ";
echo sha1("PAVAN kumar is a Writer  ");
echo "\n8. ";
echo md5("PAVAN kumar is a Writer  ");
?>

Output:

string in php 8PNG

Conclusion

Here we understand the concept of “String in PHP” with the variety of string declaration methods along with the basic introduction of important string functions which are helping a lot when doing php programs/projects

Recommended Articles

This has been a guide to String in PHP. Here we discuss the introduction and how to declare String using various methods. You may also have a look at the following articles to learn more –

  1. PHP File Handling
  2. PHP Filters
  3. Arrays in PHP
  4. PHP Split Array

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More