Arrays and Strings
Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block is referred to as an element of the array The format for creating an array in PHP is: $data = array("foo", "bar", "hello", "world"); $data now contains an array with 4 elements $data[0] contains “foo”, $data[1] contains “bar” and so on
Adding/Removing Elements $array[]=value; adds an element unset($array[5]); removes the 6 th element unset($array); deletes the whole array Note: there are other ways to add/remove elements i.e. array_splice can be used to remove elements and/or replace them with others
Parsing Arrays For loops are commonly used to iterate through the elements of an array The process of searching for an item in an array 1 element at a time is referred to a linear search, a very simple searching algorithm The foreach loop is a modified for loop that is made for this purpose: Example: foreach ($array as $i => $value) { if($value==“what we’re looking for”){ echo “We found it at position “.$i; } } $i is the index variable that stores the index of each element and $value is the actual value of the element We could also use a standard for loop: for ($i = 0; $i < count($array); $i++) { if($array[$i]==“what we’re looking for”){ echo “We found it at position “.$i; } } Note the use of the count() function which returns the number of elements in an array
PHP Arrays An array in PHP is actually an ordered map. A map is a type that associates values to keys.array $array = array( "foo" => "bar", "bar" => "foo", ); array[‘foo’] returns ‘bar’
Strings Strings are the common format for storing data in programs As a result its very important to understand how to parse out critical information from a string We do this by using functions The following slides will cover common PHP String functions
explode explode — Split a string by string Description ¶ array explode ( string $delimiter, string $string [, int $limit = PHP_INT_MAX ] ) Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. Parameters Delimiter The boundary string. String The input string. Limit If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. If the limit parameter is negative, all components except the last -limit are returned. If the limit parameter is zero, then this is treated as 1.
chop(chop(string,charlist ) ) Removes whitespace or other characters from the right end of a string (charlist is optional) str_ireplace()Replaces some characters in a string (case- insensitive) ord(string)Returns the ASCII value of the first character of a string str_split(string, length) Splits a string into an array (length is optional) chr(ascii)Returns a character from a specified ASCII value strlen(string)Returns the length of a string strpos(strpos(string,find,s tart)) Returns the position of the first occurrence of a string inside another string (case-sensitive)- start is optional
User-Defined Functions A function may be defined using syntax such as the following:
Example function sum($num1,$num2) { $answer=$num1+num2; return $answer; }
Exercises Modify your user registration script so that it now does the following: When a user registers you will read the current list of registered users from the file into a variable. Explode the string into an array using the end of line delimiter(\r\n). Iterate through each element of this array and store it in another variable. Explode each of these lines using the comma delimeter into its own array. Next determine if the user who just registered is already a registered user and if so do not add them to the list. Provide an appropriate message under either circumstance. Make sure you check the username by matching any possible variation i.e. Mike, mike, MIKE etc. Hint: use the strncasecmp() function (read the doc.’s to learn how to use this in this situation).strncasecmp()
Modification-Using a Function Modify your code from the previous example to include a function that takes a line from the file, a name and then returns true if the name is in the line and false if it isn’t.