All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Creating And Accessing String In PHP

Last Updated : Mar 11, 2024

Creating And Accessing String In PHP

In this tutorial we will show you the solution of creating and accessing string in PHP, a string is generally a sequence of letters, numbers, special characters and arithmetic values. It can be a combination of all of them also.

Now, let us understand our today’s articles using following example.

Step By Step Guide On Creating And Accessing String In PHP :-

Here, the easiest way to create a string is to enclose the string characters in single quotation marks i.e. ‘.

For example:

$string = ‘Hello to talkers code’;

One thing to note here that we can use single quotation marks and also double quotation marks here.

Now, here in php are some escape sequences that are used in string for their special purposes. Let us see them first.

  • \n is replaced by the new line character
  • \r is replaced by carriage-return character
  • \t is replaced by the tab character
  • \$ is replaced by the dollar sign($)
  • \” is replaced by the single double quotes
  • \\ is replaced by the single backslash

Now, here are string function used in php with help of which we can manipulate strings. These functions are:

  1. strlen() used to return the length of string
  2. str_word_count() used to count the words in string
  3. strrev() used to reverse a string
  4. strpos() used to search for a text within a string
  5. str_replace() used to replaces text within a string
<?php
echo strlen("Welcome to talkers code!");
echo str_word_count("Welcome to talkers code!");
echo strrev("Welcome to talkers code!");
echo strpos("Welcome to talkers code!", "talkers code");
echo str_replace("talkers code", "this world", "Welcome to talkers code!");
?>
  1. Here, as we see we give an example in which we use various function that helps use to manipulate string in php.
  2. Whereas creation and accessing of strings in php is very easy. As we specify in first example that we have to enclose the string in single quotes or either in double quotes. And we can store them in any variable in php using $ sign.
  3. We hope you understand about variable in php, if you don’t know about them you can visit to our article in which we understand about variables in php.
  4. To accessing them, we only have to use that variables in which we store our string. We hope you understand creation and access of strings in php because it is the same concept of variables.
  5. Now, at next steps we define some escape sequences that are used for special purposes inside string. You have to use them for proper understating that how they work in php.
  6. After that there are some inbuilt functions in php which helps to manipulate string. These are functions are discussed in brief in our next sessions. We hope you understand all the codes easily.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to create and access string in php.

I hope this tutorial on creating and accessing string in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪