Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.

Get Started Now!

What is String in PHP ?

In PHP, A string is a sequence of characters, such as letters, numbers, and symbols, that is used to represent text. Strings in PHP are enclosed in quotes, either single quotes (‘) or double quotes (“).

There are two ways to specify a string literal in PHP.

  1. single quoted
  2. double quoted

Single quotes:

In PHP, single quoted strings are one way to represent a string literal. Single quoted strings are denoted by enclosing the string within single quotes (‘).

For Example:

<?php  
       $str='Hello I'm single quote Example'; 
       echo $str;  
?>  

Output:

Hello I'm single quote Example

We can store multiple line text, special characters, and escape sequences in a single-quoted PHP string.

Example 2

<?php  
$str1='Hello text   
multiple line  
text within single quoted string';  
$str2='Using double "quote" directly inside single quoted string';  
$str3='Using escape sequences \n in single quoted string';  
echo "$str1 <br/> $str2 <br/> $str3";  
?>  

Output:

Hello text multiple line text within single quoted string 
Using double "quote" directly inside single quoted string 
Using escape sequences \n in single quoted string

Example3:

<?php  
$num1=10;   
$str1='trying variable $num1';  
$str2='trying backslash n and backslash t inside single quoted string \n \t';  
$str3='Using single quote \'my quote\' and \\backslash';  
echo "$str1 <br/> $str2 <br/> $str3";  
?>  

Output:

trying variable $num1 
trying backslash n and backslash t inside single quoted string \n \t 
Using single quote 'my quote' and \backslash

Double Quoted:

In PHP, double quoted strings are one way to represent a string literal. Double quoted strings are denoted by enclosing the string within double quotes (“).

For Example:

<?php  
$str="Hello I'm double quote Example";  
echo $str;  
?>  

Output:

Hello I'm double quote Example

Now, you can’t use double quote directly inside double quoted string.

Example 2

<?php  
$str1="Using double "quote" directly inside double quoted string";  
echo $str1;  
?>  

Output:

Parse error: syntax error, unexpected 'quote' (T_STRING) in C:\wamp\www\string1.php on line 2

We can store multiple line text, special characters and escape sequences in a double quoted PHP string.

Example 3

<?php  
$str1="Hello text   
multiple line  
text within double quoted string";  
$str2="Using double \"quote\" with backslash inside double quoted string";  
$str3="Using escape sequences \n in double quoted string";  
echo "$str1 <br/> $str2 <br/> $str3";  
?>  

Output:

Hello text multiple line text within double quoted string 
Using double "quote" with backslash inside double quoted string 
Using escape sequences in double quoted string

In double quoted strings, variable will be interpreted.

Example 4

<?php  
$num1=10;   
echo "Number is: $num1";  
?>  

Output:

Number is: 10

Related Posts

How to Install Node.js and npm (Step-by-Step Guide)

How to Install Node.js and npm (Step-by-Step Guide) Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, and npm (Node Package Manager) is the package Read More

Read More

Creating a WordPress Post with Google Image Search Results in PHP

In this blog post, we will explore a PHP script that utilizes the Google Custom Search API to search for images based on user-defined keywords. We’ll also Read More

Read More

How to Automatically Generate Image Search Queries Based on Keywords Using Google API

To automatically generate image search queries using Google API, you can use the Custom Search JSON API. Here’s a step-by-step guide: Step 1: Create a Google Cloud Read More

Read More

How to Build an Image Search Engine with Google Custom Search API and PHP

Building an image search engine using the Google Custom Search API and PHP involves several steps. Here is a detailed guide: Prerequisites Step 1: Create a Custom Read More

Read More

What is Composer, and why is it used in PHP development?

Composer is a dependency management tool specifically designed for PHP. It streamlines the process of managing libraries and packages required for PHP projects, making development more efficient Read More

Read More

Understanding PHP Data Types: A Comprehensive Guide with Examples

In PHP, data types refer to the various kinds of data that can be stored and manipulated within the language. PHP is a loosely typed language, meaning Read More

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x