Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - Strings

PHP Strings are simply sequences of characters put together (especially meaningful dialogues or statement to be used as information), such as "Hello World" or "PHP is very much capable of storing string and not just strings but long and lengthy strings".

Topics to be covered

We will cover the following topics in this lesson

  • String Example
  • String Concatenation Operator
  • Using the strlen() function
  • Using the strpos() function
  • Summary

String Example

For example

$string_var1 = "This is an example of string stored in PHP variable in double quotes";

$string_var2 = 'And this particular string is an example of string written with single quotes';

$string_var3 = "space is also considered a character so if you ever wish to count how many characters are there make sure to count string";

$string_var4 = ""; /* example of a string with zero character stored which basically will return a FALSE boolean result */

 

The difference between strings stored in single quote and double quote is that with singly quoted strings the value is stored literally as it is no matter what character you used and what special symbol you used, but with double quotes strings are carefully evaluated if they have any variable or special character written and if found it will parse them.

<?php

$variable = "city";
$literally = 'In this example $variable is printed as it is with $ sign\n';
print($literally);
print "<br />";
$literally = "In this example the variable $variable is printed\n";
print($literally);

?>

Above code will return the following output

<?php

$variable = "city";
$literally = 'In this example $variable is printed as it is with $ sign\n';
print($literally);
print "<br />";
$literally = "In this example the variable $variable is printed\n";
print($literally);

?>

 

As we can observe that with single quotes not only the $variable is printed as it is but also EOL character \n was also not interpreted and parsed rather printed as it is as \n. Whereas with double quotes everything was interpreted and parsed as per $variable and EOL characters

As far as string limitations are concerned in terms of its length, there are no certain limitation other than the available memory of the system on which PHP is running (as PHP is a server-side language). You are usually able to store very long length of strings to a PHP variable.

As we learned that single quotes do not parse anything and prints or stores the values as it is, however with double quotes we can parse various symbolism and signals to the string such as:

  • $ will be interpreted as variable and any word followed by $ is taken as variable instead of string (i.e "This is a $variable and not string directly stored here")
  • \n is interpreted as EOL or gives signal of a new line
  • \r is interpreted as carriage-return character (i.e. just like pressing the Enter key on keyboard)
  • \t is interpreted as the tab character
  • \" is interpreted as opening or closing double quote)
  • \$ is interpreted as dollar sign itself (i.e. $55 only)
  • \\ is interpreted as a single blackslash (i.e Windows\System32)

String Concatenation Operator

The kind of operators are used when we need to combine string echoed live along side with the string stored previously in a variable. It is basically a dot (.) character called String Concatenation Operator

<?php

$varchar1 = "Hello Venus";

$varchar2 = "Hello World";

echo $varchar1 . " said to " . $varchar2;

?>

Output of the above code will be

Hello Venus said to Hello World

In the above scenario we used the dot (.) two times because we wanted the concatenation operator to echo two variables joined by a live echoed string.

Using the strlen() function

strlen() function is used to calculate the number of characters used in a string, so that is to say it will output the total length of a string

For example

<?php

echo strlen("Howdy partner!");

?>

 

In this example strlen() will calculate the length of string and return the following result

14

If we play around

<?php

echo strlen("");

?>

Will output

0

Furthermore

<?php

echo strlen("\n");

?>

Will return

1

and

<?php

echo strlen('\n');

?>

Will return

2

this is because although the characters written in double quote were 2 but because PHP parsed as New Line character omitting the \ in the way, it tricked the strlen to believe it's only 1 character (i.e. EOL char) as with the single quote it literally read the string hence given 2 as output

Using the strpos() function

We use the strpos() function when we are in need of searching for a string or a character within a string itself. Once found, this function will then return the position number of the first match. If it doesn't find it then it will return boolean FALSE

For Example

<?php

echo strpos("My Name is Fred","Fred");

?>

will return the following output

11

Which means that the word Fred is located after the 11th character but the F of Fred is the 12th character itself.

Summary

Strings are used to store alphanumeric data. Technically, there is no limit on the length of the strings in PHP but much depends on the available memory. String can be created using either single or double quotes. Single quotes are used for literally strings while double quote strings can contain things like variables, line new line feeds and PHP will process them first before outputting the result.

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP - Constants

PHP - Arrays

Leave A Reply