CodingBison

PHP stores strings as an array of character (char) types; each char type typically requires one byte of storage. It is worth nothing that unlike C language, a PHP string does not use a NULL termination character ('\0') to mark the end of the string.

Assigning a string to a variable is simple -- we can keep the string in double-quotes or single quotes and assign that to the variable. For example, the statement, "$var_string = "Mona Lisa";", assigns string "Mona Lisa" to the variable $var_string.

String indexing is 0-based; the first character of the string has an index of 0, the second character has an index of 1, and so on. Thus, $var_string[0] equals the character 'M', $var_string[1] equals the character 'o', etc.

Printing a string variable is equally simple. We can put the string variable (must not forget the dollar sign before the variable name!) in a double-quoted string and print the string; this method of passing a variable by putting it inside a string is called interpolation.

Another way to print a string variable is to put the variable name under braces. In fact, using braces for passing a variable name is a good workaround for cases, where the variable name can be altered otherwise.

We use a simple program (provided below) to understand both assignment and printing of string variables. We keep the variable, $var_str, inside a larger string and print it using the PHP's echo function. In the same string, we also keep the variable $adjective under braces followed by "st" so that we can print the adjective "largest" when describing Facebook. Note that it is okay to have a comma immediately after the variable name and that is why "$var_str," works in the above example.

 <?php
 $var_str = "Social Network";
 $adjective = "large";
 echo "The movie, $var_str, is about Facebook, which is the {$adjective}st social network";
 ?>

When run, the output of the following code would be "The movie, Social Network, is about Facebook, which is the largest social network."

In the above code, it is important to keep $adjective variable in braces; if we were to remove the braces, then PHP would look for variable $adjectivest! Since, this variable is not defined, PHP would print NULL in place of "largest".

Before proceeding further, we would like to mention that interpolation does not work within single-quotation. If we were to replace the double-quotes in the above snippet with single quotes, then the output would be: "The movie, $var_str is about Facebook, which is the {$adjective}st social network", which is the same as that of the string passed to the echo!

Sometimes, for longer PHP strings, it is more convenient to specify them across multiple lines, instead of a single line. For such cases, PHP provides the "heredoc" method. In this method, we begin the string with the "<<<" tag followed by an identifier, then the multi-lined string, and in the end, complete the string by the earlier identifier. Here is a sample code, that accepts multi-line input and uses "SocialNetwork" as the heredoc identifier.

 <?php
 $var_string = <<< SocialNetwork
 The movie, "Social Network", is about Facebook,
 which is the largest social network.
 In this movie, Jesse Eisenberg portrayed Mark Zuckerberg.
 He won an Oscar nomination for his role!
 SocialNetwork;

 echo $var_string;
 ?>

The output (provided below) shows that heredoc preserves the nature of multi-line input.

 The movie, "Social Network", is about Facebook,
 which is the largest social network.
 In this movie, Jesse Eisenberg portrayed Mark Zuckerberg.
 He won an Oscar nomination for his role!

Yet another fundamental requirement of strings is to join two or more strings. PHP provides a convenient way to join (concatenate) two or more strings using the dot "." operator. Thus, "$var_string1 . $var_string2" joins the two string variables and then returns them as a new string.

In the example provided below, we concatenate three string variables and its output is: "The movie: Social Networking and length is 28". We also use the strlen() function to print the number of characters present in a string.

 <?php
 $var_str1 = "The movie: ";
 $var_str2 = "The Social ";
 $var_str3 = "Network";
 $var_str_new = $var_str1 . $var_str2 . $var_str3;

 echo $var_str_new; 
 echo " and length is " . strlen($var_str_new); 
 ?>




comments powered by Disqus