Write a PHP script to display the strings as per the given sample strings

By Shahnail Khan Last updated : December 11, 2023

Problem statement

Write a PHP script to display the following strings.

Sample String :
'Tomorrow I \'ll learn PHP global variables.'
'This is a bad command : del c:\\*.*'
Expected Output :
Tomorrow I 'll learn PHP global variables.
This is a bad command : del c:\*.*

Prerequisites

To understand this solution better, you should have the basic knowledge of the following PHP topics:

Displaying the given string using PHP

To display the given string, we can use a simple echo statement. In PHP, a string is a sequence of characters. These characters can be letters, numbers, symbols, or spaces. Strings are essential for storing and manipulating text-based data in web applications. We can declare string using single or double quotes.

Escape characters in PHP strings help you include special characters in your text. For example, to display the $ sign, you need \$ escape character in a sentence.

The sample string given in this question includes the '\' character, which is used to include a single quote (') within a single-quoted string ("). We cannot use single or double quotes in the string without an escape character.

Click here to get more information about PHP Strings and escape characters.

PHP script to display the given strings

In the below PHP code, we are displaying the strings as per the question.

<?php
// Sample strings
$string1 = 'Tomorrow I \'ll learn PHP global variables.';
$string2 = "This is a bad command : del c:\\*.*";

// Expected output
echo $string1 . "<br>";
echo $string2;
?>

Output

The output of the above code is:

Tomorrow I 'll learn PHP global variables.
This is a bad command : del c:\*.*

Code Explanation

  • As you already know <?php and ?> tags indicate the beginning and end of PHP code in the script.
  • The backslash (\) is an escape character in PHP, so it's used before the single quote (') in the first string to escape it and display it correctly.
  • The <br> tag is used to add a line break between the two strings.
  • We have used the echo statement to display the two strings.

More PHP Programs »

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.