PHP Strings and the utilities

PHP Strings and the utilities

PHP strings are a vital part of the scripting language. Strings are an integral part of any programming language. String concatenation, character traversal, and several different other operations are very vital while coding a project. Any input from the keyboard comes in the form of a String. PHP Strings also have some very important utilities.

PHP does not treat PHP Strings to be a character array. However, if you wish to operate on specific characters, you might do that with the library functions specified. Strings are declared or initialized in the same manner as other data-types.

Some basic utilities of PHP Strings

One of the prime utilities offered by PHP strings is that they collect data from the user. Whether its and application or a website, PHP collects data in the form of String. Forms are one of the best examples. Until the advent of HTML5, forms implemented input type text, password, textarea, select menus, etc. All of these are basically String inputs. Through the GET or POST methods we collect the data and do the needful. If some calculation needs to be done with some numeric String, we cast it using intVal or floatVal to a numeric value.

Other than inputs, Strings play an important role in internal coding techniques. Suppose you want to send a mail. The body message of the email must be a String. Concatenation of several Strings makes up the message.

Encryption of password, generation of random text, etc. are some other very important utilities of PHP Strings.

PHP Strings commonly used techniques

Concatenation of PHP Strings
// $a = "Kaustav" $b = "Banerjee" you must get $c = "Kaustav Banerjee"
$a = "Kaustav";
$b = "Banerjee";
$c = $a.$b; // Produces - Kaustav Banerjee
$d = "Entrepreneur";
$e = $a.$b."is an".$d;
Typecasting of PHP Strings (Changing values to numbers)
// Changing Strings to Integer and Float
$a = "12345";
$b = intVal($a); // Gives integer equivalent of $a
$c = "5.12";
$d = floatVal($c); // Gives float equivalent of $a
Generation of a random PHP string
// Generating a random PHP string
$str = "", $i = 1;
while($i<=10){  // Iterating ten times. So string will contain 10 characters
$r = rand(65,90);  // Generates a random number between 65 and 90
$c = chr($r); // Converting number to character according to ASCII value 
$str = $str.$c; // Concatenating to present string

/*
You can also write the above 3 lines as 
$str .= chr(rand(65,90));

I have detailed the process to help beginners understand better
*/
$i++;
}
echo $str; // Final random string
PHP Strings special stripping and special joining
// PHP string special join and break
$a = "Kaustav Banerjee";

/* The following function breaks the string from all places where a space is encountered.
The broken strings are stored as an array */

$array = explode(" ", $a); // array1[0] contains "Kaustav" and array1[1] contains "Banerjee"

/* The following function joins the strings from the array elements of an array.
The joined string is stored as a new string. The character sent as first parameter to this 
function is present at the joining points of all the array elements */

$str = implode(",", $array); // Produces the string Kaustav,Banerjee

The above mentioned procedures are some of the most commonly used techniques. Other than this there are several library functions that offer a variety of functionalities. PHP Strings may be declared in three different syntaxes altogether. That is a very interesting section and I would love to share it with you all in my next article on PHP Strings. Till then, just enjoy the above tips and I hope this tutorial helps you in some way.

Comments are closed.