payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


String Data Type   in Category: PHP   by amit

🕙 Posted on 2023-05-08 at 21:36:39     Read in Hindi ...


String and String Functions

    In many programming languages, string is a data-type which can hold zero or more characters, including digits and symbols within a pair of single quotes or a pair of double quotes. See the following picture, in which many letters are stringed with a thread. Similarly, a string is a series or sequence of characters. Remember that you cannot mix single quotes with double quotes, which will be explained later.


<?php
  $var_name = "PARTY"; // $var_name is user-defined custom variable name, and should not be confused with var_dump() function
  var_dump( $var_name ); // outputs string(5) "PARTY"
  $another_var = " P A R T Y "; // To know how you should name a variable or a constant, visit previous page.
  var_dump( $another_var ); // outputs string(11) " P A R T Y "
  $empty_var = ""; // there is no blank space within a pair of double quotes
  var_dump( $empty_var ); // outputs string(0) ""
?>

    In the above example, the word 'PARTY' is enclosed within a pair of double quotes. The output is string(5) "PARTY", which means that the value is of string data-type, and it consists of 5 characters. The second word " P A R T Y " contains 6 blank spaces (generally, called whitespaces in programming world, when dark mode feature was not available in TEXT Editors).

    The last line contains only two double quotes, without any space and without any character. This is also a string data-type, but it is called empty string. It is evaluated as false in boolean type-casting. Any non-numeric string (which does not start with a number and/or prefixed with + or - symbol to that number) is evaluated as zero.

    Caution: You should be careful, when writing codes into PHP or other programming languages, because " " (two double quotes, with a blank space) is not empty string, and therefore, it is not evaluated as false. You have also seen in previous page, various examples of type-casting from (1) boolean to integer, (2) integer to boolean, (3) string to integer, (4) string to boolean, and (5) null to other data-types.

<?php
  var_dump( (int) "" ); // int(0)
  echo '<br />';   var_dump( (bool) "" ); // bool(false)
  echo '<br />';   var_dump( (bool) " " ); // bool(true)
  echo '<br />';   var_dump( (array) "" ); // array(1) { [0]=> string(0) "" }
?>

    Various data-types (except array, and other compound data-type) can also be type-casted into string data-type. An array can be type-casted into other data-types, but when you try to type-cast array into a string, ERROR will be shown. However, PHP tries to convert the array into a string, similarly as echo function tries to display an array. The word 'Array' (last line) is converted into string data-type, and last line shows: string(5) "Array"

<?php
  var_dump( (string) true ); // string(1) "1"
  echo '<br />';   var_dump( (string) false ); // string(0) ""
  echo '<br />';   var_dump( (string) -1.232 ); // string(6) "-1.232"
  echo '<br />';   var_dump( (string) 0 ); // string(1) "0"
  echo '<br />';   var_dump( (string) array( "" ) ); // outputs ERROR as shown below, whether there is any item inside the parentheses or not.
?>

string(1) "1"
string(0) ""
string(6) "-1.232"
string(1) "0"

Warning: Array to string conversion in C:\xampp\htdocs\php_project\phptest.php on line 6
string(5) "Array"

A string is a series or sequence of characters.

    You can, therefore, access each character within a pair of single quotes or a pair of double quotes. You can either get any character from the string, either positively (from left to right) or negatively (from right to left) by placing integer value within brackets just after the variable name. These integer values inside the brackets just after the variable name are known as index number, and refer to the position of the character. You should remember that a blank space is also a character within the string.

<?php
  $var_name = "PARTY"; // 'PARTY' is a string of five character
  echo $var_name[0]; // 0 (zero) is the first positive (whole) number
  echo '<br />';   echo $var_name[4]; // you can place upto one less positive number within brackets
  echo '<br />';   echo $var_name[-5]; // you can place negative number upto length of string as of PHP 7.1.0
?>

P
Y
P

    You must remember that any number which is equal to or greater than length of the string, if placed inside brackets, will show ERROR. Similarly, any negative number which is less than (that is with - minus symbol) length of the string, if placed inside brackets, will also show ERROR. This second method to access character from right to left is applicable only to string, and you cannot access an item within an array, by placing negative number inside the brackets for the array.

String Functions

    These string functions are pre-defined (built-in) and enables PROGRAMMERS and WEB-DEVELOPERS to display or get the desired results. All built-in (pre-defined) functions in PHP or any programming languages make that programming language more powerful, robust, and scalable.

    Beginners will find it difficult to remember these built-in functions, but with time and practice, all persons (including myself) have learned to use them frequently. When any programmer (even the expert one) want to apply any newly defined (built-in) function, he/she will refer the official documentation of that programming language.

To find length of a string

<?php
  $var_name = "PARTY"; // a blank space is also calculated
  echo strlen( $var_name ); // outputs 5
  echo '<br />';
  echo $var_name[ strlen( $var_name ) -1 ]; // outputs Y
  echo '<br />';
  echo $var_name[ - strlen( $var_name ) ]; // outputs P
?>

    In the above example, there are blank spaces within brackets and parentheses. These blank spaces are only to look the code better. You can write above example as below, without blank spaces. Blank spaces are required to separate codes from opening PHP tag, and functions which do not need parentheses, for example, echo, etc.

    In following three lines, when <br> HTML element is not output by echo function within PHP tags, output will be 5YP, or you have to put <br> HTML element outside PHP tags to display the result human-readable. (I have added the <br> HTML element outside the PHP tag twice, for your illustration how to use them.)

<?php echo strlen($var_name); ?><br />

<?php echo $var_name[strlen($var_name)-1]; ?><br />

<?php echo $var_name[-strlen($var_name)]; ?>

    You have also seen in above examples that even a single blank space matters within a pair of single quotes or a pair of double quotes. Do not use special characters for writing a single quote or double quote, otherwise ERROR will be displayed. String and empty string are very important. You will learn more about empty string in advance chapters. More string functions are described in next page.


Leave a Comment: