Learn PHP from Scratch Day -8

Learn PHP from Scratch Day -8
Learn PHP from Scratch Day -8

PHP String Functions:

What is a string?

A string is a collection of characters. String is one of the data types supported by PHP

1) PHP Create strings

Let’s look at an example that creates a simple string in PHP.

Output:

string(42) "You need to be logged in to view this page"

If the single quote is part of the string value, it can be escaped using the backslash.

The code below illustrates how to escape a single quote.

Output:

I'll be back after 20 minutes

2) PHP Create Strings Using Double quotes

The double quotes are used to create relatively complex strings compared to single quotes.

Variable names can be used inside double quotes and their values will be displayed.

Let’s look at an example.

HERE,

  • The above example creates a simple string with the value of Alicia.
  • The variable name is then used in the string created using double quotes and its value is interpolated at run time.

Output:

Alicia is friends with kalinda

n addition to variable interpolations, the double quote string can also escape more special characters such as"\n for a linefeed, \$ dollar for the dollar sign" etc.

More examples Let’s suppose that we have the following code

Output:

NOTICE : Undefined variable pas

executing the above codes issues a notice"Notice: Undefined variable".

This is because $word is treated as a variable.

If we want the dollar sign to be treated as a literal value, we have to escape it.

Output:

pas$word

PHP Heredoc

This heredoc methodology is used to create fairly complex strings as compared to double quotes.

The heredoc supports all the features of double quotes and allows creating string values with more than one line without php string concatenation.

Using double quotes to create strings that have multiple lines generates an error.

You can also use double quotes inside without escaping them.

The example below illustrates how the Heredoc method is used to create string values.

HERE,

<<

EOT is the acronym for end of text.

It should be defined in its on line at the beginning of the string and at the end.

Note: you can use anything you like in place of EOT

beginning and end of string

Output:

When Shalon was a baby, She used to look like a "boy".

PHP Nowdoc

The Nowdoc string creation method is similar to the heredoc method but works like the way single quotes work.

No parsing takes place inside the Nowdoc.

Nowdoc is ideal when working with raw data that do not need to be parsed.

The code below shows the Nowdoc implementation

Output:

When $baby_name was a baby, She used to look like a "boy".

PHP string functions

PHP string functions are used to manipulate string values.

We are now going to look at some of the commonly used string functions in PHP

PHP string functions

PHP string functions are used to manipulate string values.

We are now going to look at some of the commonly used string functions in PHP

string functions in php

More tring function are available @ official document link below..

Resource : https://www.php.net/manual/en/ref.strings.php

Resource : https://www.php.net/manual/en/ref.strings.php