PHP

PHP variables with a string literal

Share your learning

While working with PHP, most of the time we need to use PHP string literal variables. Then we use string concatenation techniques.

PHP String literal variable

PHP string literal variable is a technique to use the variable with text or string to make text preview dynamic. Every time your variable value will change, it will effect the text and then you will get the dynamic content.
But there are few more methods to render the PHP variable value inside the string literal.

  1. Use single quotes
  2. Use double quotes
  3. Use curly braces
  4. Use of sprintf
  5. Heredoc

Read also : Create, modify and update excel sheet with PHP library

When using single quotes

We can use single quotes for variable interpolation with string or PHP string literal variable. For this we need to use a concatenation operator to concat string with variable.

<?php

//phpinfo();

$name = "harry";

$subject = 5;
$total_marks = 500;
$obtained_marks = 423;
$exam_cycle = 'month';

// string concatenation with single quotes
echo 'Your name is '.$name.'!';

With double quotes

We can use double quotes for variable interpolation and this time we don’t need any concatenation operator. Double quotes allow us to render the value of the variable inside the string without using any other operator.

// dobule quotes
echo "<br>and your got the marks $obtained_marks";

When we need to use curly braces with double quotes

Sometimes, you heard about curly braces with double quotes to use PHP string literal variable. But I was surprised when I heard about it for the first time because I am easily able to use variables with double quotes. Then why do we need to use curly braces?

Then I found the case where we need to use curly braces. Let’s suppose we are going to print a fees cycle for students Like this monthly, quarterly or yearly.

And we have variables with value “month, quarter or year” then we need variable interpolation without space with string to get the desired result. 

This is the situation where we need to use curly braces. Curly braces separate the variable from the other string.

// use of curly braces with double quotes
echo "<br>Your exam cycle is {$exam_cycle}ly";

Use of Sprintf

Sprintf allows us to use complex variable expressions easily. For example we are calculating average marks here and print with the help of sprintf.

Sprintf simply uses the spots where the variables can be placed with %s syntax.

// sprintf
echo sprintf("<br>and your avg marks is %s", $obtained_marks/$total_marks*100);

echo sprintf("<br>You got %s out of %s", $obtained_marks, $total_marks);

Heredoc

One more way to use variables with string is heredoc. The syntax of heredoc is <<< then use the identifier and press enter to use the new line.  

Here we can use our string and then the same identifier will be used to close the heredoc.

Closing identifier must be in the new line. Identifiers must be alphanumeric and must start with a non-digit character or underscore.

Example of using heredoc. Hope you can relate with the example below.

echo <<<STR
    <div class="container">
    <p style="font-size:12px;line-height:1.5px;">
    <span class="first-item" style="color:red">Hello world</span>
    <span class="last-item" style="color:black">Lore ipsume</span>
    </p>
    </div>
STR;

You have learnt about the use of PHP string literal variable.

Hope you find this article useful. Keep learning, stay inspired

See you in the next learning chapter.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago