PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev. Читать онлайн. Newlib. NEWLIB.NET

Автор: Sergey D Skudaev
Издательство: Издательские решения
Серия:
Жанр произведения: Компьютеры: прочее
Год издания: 0
isbn: 9785449090157
Скачать книгу
($lastname);

      echo “Length=”. $len.”<br>”;

      Output: Length=11

      The trim () function trims space characters from the beginning and the end of a string. This function is useful because a user may inadvertently enter space characters while typing in a login or password. To prevent this from occurring, trim every user input string as in the following example:

      $lastname =' Shakespeare”;

      echo “Before trim:”. strlen ($lastname).”<br>”;

      $lastname=trim ($lastname);

      echo “After trim:”. strlen ($lastname).”<br>”;

      Output:

      Before trim 13

      After trim 11

      As you can see, I’ve intentionally entered a space at the beginning and end of the last name and its length became longer by two characters. After trimming these spaces, the length was restored to 11 characters.

      The string position function strpos () returns the position of the first occurrence of a substring in the string.

      For example, echo strpos ($lastname, ‘sp’) will return 5.

      You might expect it to return 6 because sp is in the 6th position; however, it returns 5

      because the count starts at 0.

      echo strpos ($lastname, “Shake’); will return 0.

      After execution, echo strpos ($lastname, ‘shake’); nothing will be displayed in the browser because it returns Boolean false. Lower case ‘shake’ has not occurred in the “Shakespeare’. To confirm this, execute the following code:

      if (strpos ($lastname, ‘shake’) ===false)

      echo “return false <br>”;

      Output: return false

      The strpos is a case sensitive function. The stripos is a case insensitive function.

      echo stripos ($lastname, ‘shake’); will return 5.

      The strrpos () – Find the position of the last occurrence of a substring in a string.

      echo strrpos ($lastname, ‘e’) will return 10.

      The substr () function will return a substring of a string.

      echo substr (“William Shakespeare’, 8); will return Shakespeare

      echo substr (“William Shakespeare’, 8, 5); will return Shake.

      It starts counting from 0 and returns that piece of substring starting from the 8th character.

      The second parameter, if it is present, determines the substring length. This explains why

      echo substr (“William Shakespeare’, 8, 5); will return Shake.

      If the second parameter is not present, all that portion of string is returned.

      This is why echo substr (“William Shakespeare’, 8); will return Shakespeare

      The str_replace () is another important string function. This function replaces all occurrences of the search string with a replacement string.

      For example, if a user entering text containing single or double quotation marks into a form, it can create problems with the database. To prevent this from happening, using the str_replace function, we can replace all occurrences of single quotation marks in the input text.

      $comments=“this is comments with a ‘single quotation’ marks”;

      echo “before str_replace:”. $comments.”<br>”;

      $comments=str_replace (” ‘”, "&acute;”, $comments);

      echo “after str_replace:”. $comments.”<br>”;

      Output:

      before str_replace: this is comments with a ‘single quotation’ marks after str_replace: this is comments with a “single quotation’ marks

      When viewing the web page source, you will see that each single quotation mark has been replaced by the &acute; code.

      PHP Array Functions

      Arrays are used in any programming language. You can imagine an array as a long box with many identical compartments. Visualize it like this: |___|___|___|___|___|.

      Whatever you place in a compartment is that particular compartment’s value. Let’s place the characters a, b, c, d and e in the following array of compartments: |_a_|_b_|_c_|_d_|_e_|

      Now, to access any of these values, you’ll need to know which compartment the value is stored in. For example, ‘b’ is stored in the second compartment.

      In most computer languages, array index counting starts from 0, not 1, meaning that the index of the first element of the array is 0, the index of the second element of the array is 1 and so on. In the following array of names, you see the indexes and values:

      //declare an array of names

      $names=array ();

      $names [0] =“John”;

      $names [1] =“George”;

      $names [2] =“James”;

      $names [3] =“Anna”;

      $names [4] =“Robert”;

      $names [5] =“John”;

      $names [6] =“James”;

      $names [7] =“George”;

      $names [8] =“Maria”;

      $names [9] =“Peter”;

      $names [10] =“James”;

      To display “Anna” you have to access the element (compartment) with the index 3. If you use the print command,

      print ($names [3]);

      The built-in function sizeof (array) returns the number of elements in an array. In this case,

      $asize=sizeof ($names);

      Or you can use the count function

      $asize=count ($names);

      To display all array values, we can use the for loop.

      The for loop looks like this:

      for ($i=0; $i <$asize; $i++)

      print ($names [$i].”<br>”);

      In the for loop, the $i variable is incremented from 0 to the value one less than the $asize value. If the array has 11 elements, the $sizeof () function will return 11. The $i variable will be incremented to 10 and then stops. You might assume that if $i stops at 10, and we have 11 elements, the last array element would not be displayed.

      However, that assumption would be wrong because the first array element index is 0, the eleventh element will have index 10, and so our code will display the element index 10, which is the 11th element of the array.

      There are many useful built-in functions for arrays. The code below demonstrates some of these:

      The in_array () function returns true if the value exists in an array.

      So, in the above array of names, in_array (“Anna’) will return True, but in_array (“Lidia’) will return False.

      The sort function sorts an array in ascending order.

      The rsort function sorts an array in reverse order.

      The array_pop function