Web Tech PHP Array and String Functions PDF

Document Details

Uploaded by Deleted User

Tags

PHP programming array functions string functions web development

Summary

These notes provide an overview of PHP array and string functions, with explanations and examples for each function. Topics include array manipulation (push, pop, merge, etc.) and string operations (length, conversion, etc.).

Full Transcript

# Web tech ## Function of array in PHP - **`array_push`**: Add one or more elements to the end of the array. - **Syntax** ```php <?PHP $Fruits = ["Apple", "banana"]; array_push($fruit, "Orange", "grape"); print_r($fruits); ?> ``` - **`array_pop`**: Removes the last...

# Web tech ## Function of array in PHP - **`array_push`**: Add one or more elements to the end of the array. - **Syntax** ```php <?PHP $Fruits = ["Apple", "banana"]; array_push($fruit, "Orange", "grape"); print_r($fruits); ?> ``` - **`array_pop`**: Removes the last element of the array. ```php $fruits = ["Apple", "banana", "Orange"]; array_pop($fruits); print_r($fruits); ``` - **`array_merge`**: Merges one or more arrays. ```php $arr1 = ["a", "b"]; $arr2 = ["c", "d"]; $result = array_merge($arr1, $arr2); print_r($result); ``` - **`in_array`**: Check if a value exists in an array. ```php echo in_array("Banana", $fruits)? "Yes" : "No"; ``` - **`array_keys`**: Return all the keys of an array. ```php $arr = ["name" => "John", "age" => 25]; print_r(array_keys($arr)); ``` - **`array_shift`**: Removes the first element from an array. ```php $arr = [1, 2, 3]; array_shift($arr); print_r($arr); //Output: 2, 3 ``` - **`array_unshift`**: Adds one or more elements to the beginning of an array. ```php $arr = [1, 2, 3]; array_unshift($arr, 0); print_r($arr); //Output: 0, 1, 2, 3 ``` - **`count`**: Return the number of elements in an array. - **`array_slice`**: Extracts a portion of an array. ```php $arr = [1, 2, 3, 4, 5]; $slice = array_slice($arr, 2, 2); print_r($slice); ``` - **`array_splice`**: Replaces elements in an array. - **`array_key_exists`**: Check if a key exists in an array. - **`array_flip`**: Flip all the key and value of an array. - **`array_diff`**: Compute the difference of arrays. - **`array_intersect`**: Intersection of arrays. - **`array_key`**: Return all the keys of an array. ## String functions - **`strlen`**: Returns the length of the string. - **`strtoupper`**: Converts a string to uppercase. - **`strtolower`**: Converts a string to lowercase. - **`strpos`**: Finds the position of the first occurrence of a substring in a string. - **`str_replace`**: Replaces all occurrences of a search string with a replacement string. - **`substr`**: Returns a portion of a string. - **`trim`**: Removes whitespace or other predefined characters from the beginning and end of a string. - **`explode`**: Splits a string of an array using a delimiter. - **`implode`**: Join a string of an array using a delimiter. - **`mysqli_connect`**: Connects to a database with the details provided. - **`mysqli_query`**: Executes a query on a database. - **`mysqli_fetch_assoc`**: Fetches a row from a result set as an associative array. - **`json_decode`**: Decodes a JSON string into a PHP variable. - **`isset`**: Checks if a variable is set and not null. - **`htmlspecialchars`**: Converts special characters to HTML entities. - **`$_GET`**: Superglobal variable that contains data submitted through the URL. - **`$_POST`**: Superglobal variable that contains data submitted through a form. - **`$_SERVER`**: Superglobal variable that contains information about headers, paths, and script locations. - **`$_REQUEST`**: Superglobal variable that contains data submitted through the URL, form, or cookies. ## Roles of Manager Cookie data ### Describe GET and POST methods in PHP, which should be used? **GET** method is used to submit the HTML form data. This data is collected by the predefined `$_GET` variable for processing. The information sent from an HTML form using the GET method is visible to everyone in the browser's address bar, which means that all variable names and variables their values will be displayed in the URL. Therefore, the GET method is not secure. We can bookmark the page with specific query strings. GET requests can be cached, but requests are always in browser history. **POST** method is also used to submit the html form data. But the data submitted by this method is collected by the predefined superglobal variable `$_POST`. It does not have a limit on the amount of information to be sent. The information sent from an HTML form is visible to everyone. POST method is used to send sensitive information. GET method is used for actions that do not modify the server state. When data needs to be shared via URLs. **Example of using GET method in PHP:** ```php <?php if (isset($_GET['name'])) { echo "Hello"; htmlspecialchars($_GET['name']); } else{ echo "Name not provided"; } ?> ``` **Example of using POST method in PHP:** ```php <?php if($_SERVER["REQUEST_METHOD"] == "POST"){ if(isset($_POST['name'])){ echo "Hello"; htmlspecialchars($_POST['name']); } else{ echo "Please fill name"; } } else{ echo "Wrong Request Method"; } ?> ``` POST method is used for actions that modify server state and for transmitting sensitive data like passwords.

Use Quizgecko on...
Browser
Browser