🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

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

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

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


Get Started Now!

How to creating and accessing string function in PHP?

What is String in PHP?

A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation marks (‘), like this:

Example: $my_string = ‘Hello World’;

You can also use double quotation marks (“). However, single and double quotation marks work in different ways. Strings enclosed in single-quotes are treated almost literally, whereas the strings delimited by the double quotes replaces variables with the string representations of their values as well as specially interpreting certain escape sequences.

The escape-sequence replacements are:

  • \n is replaced by the newline character
  • \r is replaced by the carriage-return character
  • \t is replaced by the tab character
  • \$ is replaced by the dollar sign itself ($)
  • \" is replaced by a single double-quote (")
  • \\ is replaced by a single backslash (\)

Here’s an example to clarify the differences between single and double quoted strings:

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Single and Double Quoted Strings</title>
</head>
<body>
<?php
$my_str = 'World';
echo "Hello, $my_str!<br>";
echo 'Hello, $my_str!<br>';
echo '<pre>Hello\tWorld!</pre>';
echo "<pre>Hello\tWorld!</pre>";
echo 'I\'ll be back';
?>
</body>
</html>
view raw String.php hosted with ❤ by GitHub

Manipulating PHP Strings

PHP provides many built-in functions for manipulating strings like calculating the length of a string, find substrings or characters, replacing part of a string with different characters, take a string apart, and many others. Here are the examples of some of these functions.

Calculating the Length of a String

The strlen() function is used to calculate the number of characters inside a string. It also includes the blank spaces inside the string.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Calculate String Length</title>
</head>
<body>
<?php
$my_str = 'Welcome to Devops School';
// Calculating and displaying string length
echo strlen($my_str);
?>
</body>
</html>
view raw length.php hosted with ❤ by GitHub

outputs 24

Counting Number of Words in a String

The str_word_count() function counts the number of words in a string.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Find the Number of Words in a String</title>
</head>
<body>
<?php
$my_str = 'The quick brown fox jumps over the lazy dog.';
// Calculating and displaying number of words
echo str_word_count($my_str);
?>
</body>
</html>

outputs 9

Replacing Text within Strings

The str_replace() replaces all occurrences of the search text within the target string.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Replacing Text within a String</title>
</head>
<body>
<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
// Display replaced string
echo str_replace("facts", "truth", $my_str);
?>
</body>
</html>
view raw Replacing.php hosted with ❤ by GitHub

outputs :- If the truth do not fit the theory, change the truth.

Reversing a String

The strrev() function reverses a string.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Reversing a String</title>
</head>
<body>
<?php
$my_str = 'You can do anything, but not everything.';
// Display reversed string
echo strrev($my_str);
?>
</body>
</html>
view raw Reversing.php hosted with ❤ by GitHub

outputs:- .gnihtyreve ton tub ,gnihtyna od nac uoY

Strpos() – Search For a Text Within a String

The PHP strpos() the function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

Example:

<!DOCTYPE html>
<html>
<body>
<?php
echo strpos("Hello world!", "world");
?>
</body>
</html>
view raw strpos() .php hosted with ❤ by GitHub

outputs:- 6

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.