PHP String Variables

PHP String Variables tutorial explain you What is PHP String variables and How to find string length in PHP? with simple examples.

  • PHP String Variables takes a list of strings (or words) as its value.
  • There are four different ways to declare string:
    1. single quoted
    2. double quoted
    3. PHP heredoc syntax
    4. PHP nowdoc syntax

Single quoted PHP string

String is to enclose it in single quotes.

Example on Single quoted PHP string:

If you want to use a single-quote within the string you have to escape the Single-quote with a backslash \ . Like this: \’

Example Single quoted PHP string:

Double quoted PHP string:

It is an essential method for forming string. Double-quote allow for many special escaped characters to be utilized that you can’t do with a Single-quote string.
Once again, a backslash is utilized to escape a character.

Example on Double quoted string in  PHP :

PHP Heredoc:

PHP heredoc string behaves just like a Double-quote string, without the double quotes.

Example on PHP Heredoc:

Nowdoc syntax in PHP:

nowdocs are to Single-quote strings.
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes.

Example on nowdoc in PHP:

PHP Concatenation Operator:

In PHP, (.) operator is used for concatenation of two Strings.

Example PHP Concatenation Operator:

Output will be:

The strlen() function in PHP:

The strlen ( ) function is used to return the length of the string on success, and 0 if the string is empty.

Example on strlen in PHP:

The output of the code wll be:

The strpos() function in PHP:

The strpos( ) funtion is used to search for character/string within a string.

If a match is found, this funcion will return the position of the first match, else it will return FALSE for no match.

Example on strpos in PHP:

The output of the code will be:

Previous articlePHP Variables
Next articlePHP Operators
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!