Learn Php Flashcards
- what are the Applications of PHP?
PHP is a server scripting language used for making dynamic web pages. That means PHP allows you to use scripts on a web server to produce a response customized for each client’s (user’s) request.
PHP is the foundation of:
- Many CMS (WordPress, Drupal, Joomla)
- E-Commerce Platforms (WooCommerce, Magento)
- Web Development Frameworks (Laravel, CakePHP, Symfony)
Dynamic Webpages using PHP and PHP Script Execution
PHP is a server scripting language. This server side code can be used to fill out HTML templates in order to create a complete HTML document for a visitor. This finished document is called a dynamic webpage.
Dynamic PHP webpages can deliver a custom set of assets to each visitor, unlike static pages which deliver the same set of assets to everyone.
PHP can be served such that any browser making a connection to the server can execute the PHP file. This is called server-side scripting.
From the terminal, PHP scripts can be executed on demand and the output of the script is logged to the terminal. This is called command-line scripting.
Embedding PHP Code in HTML
PHP can generate HTML when saved as a file with a .php extension. These files must always start with the tag
PHP can also be embedded into HTML. In this case, both opening tag are used.
For example, in the given code, the PHP code has been embedded into the HTML by enclosing it within the tags.
Parsing Variables within PHP Strings
In PHP, variables can be parsed within strings specified with double quotes (“).
This means that within the string, the computer will replace an occurence of a variable with that variable’s value.
When additional valid identifier characters (ie. characters that could be included in a variable name) are intended to appear adjacent to the variable’s value, the variable name can be wrapped in curly braces {}, thus avoiding confusion as to the variable’s name.
Concatenating Strings and Appending a String in PHP
In PHP, if you want to join two strings together, you need to use the . operator.
This process is called concatenation. Put the . operator between the two strings in order to join them.
Note that strings are joined as-is, without inserting a whitespace character. So if you need to put spaces, you need to incorporate the whitespace manually within the string.
In PHP, there is a shortcut for appending a new string to the end of another string. This can be easily done with the string concatenation assignment operator (.=).
This operator will append the value on its right to the value on its left and then reassign the result to the variable on its left.
PHP String Escape Sequences
In PHP, sometimes special characters must be escaped in order to include them in a string. Escape sequences start with a backslash character ().
There are a variety of escape sequences that can be used to accomplish different tasks. For example, to include a new line within a string, the sequence \n can be used. To include double quotation marks, the sequence " can be used. Similarly, to include single quotes, the sequence ' can be used.
PHP Reference Assignment Operator =&
In PHP, the reference assignment operator (=&) is used to create a new variable as an alias to an existing spot in memory.
In other words, the reference assignment operator (=&) creates two variable names which point to the same value. So, changes to one variable will affect the other, without having to copy the existing data.
Define PHP Function
A function contains a set of instructions to be executed. It is defined by using the keyword function followed by the name of the function, the parentheses which contain the parameters, and finally the curly braces which contain the code block.
PHP built-in functions
PHP comes standard with many built-in functions. They give us an easier way to implement and repeat popular tasks throughout a program. A popular example is the echo function.
Many more are documented at https://www.php.net/docs.php. In addition to a description of what the function does, the documentation for each function denotes:
The name of the function
Required parameters and their types
Optional parameters (in square brackets, []), their types, and default values
The return type of the function (after the final colon, :)
PHP Apending Arrays
In PHP, elements can be added to the end of an array by attaching square brackets ([]) at the end of the array’s name, followed by typing the assignment operator (=), and then finally by typing the element to be added to the array.
PHP Ordered Arrays
In PHP, an ordered array is a data structure representing a list of ordered, stored data. The location of an element in the array is known as its index. The elements in an ordered array are arranged in ascending numerical order starting with zero.
PHP print_r function
The built-in print_r() function outputs arrays in a human readable format.
To use the function, place an array or a variable with an array as its value in between the parentheses.
PHP array_push function
The PHP built-in array_push() function takes an array as its first argument. The arguments that follow are elements to be added to the array.
The function adds each of the elements to the end of the array and returns the new number of elements in the array.
To use the function place an array or a variable with an array as its value in between the parentheses.
PHP array_pop function
The PHP built-in array_pop() function takes an array as its argument. It permanently removes the last element of an array and returns the removed element.
To use the function place an array or a variable with an array as its value in between the parentheses.
PHP unshift function
The PHP built-in array_unshift() function takes an array as its first argument. The arguments that follow are elements to be added to the array.
The function adds each of the elements to the beginning of the array and returns the new number of elements in the array.
To use the function, place an array or a variable with an array as its value in between the parentheses.
PHP implode function
The built-in implode() function makes a string from an array. The function takes two arguments: a string to place between each element of an array and the array to be joined together.
The function returns a single string with the string argument, known as the $glue, inserted between each element in the array, known as the $pieces.
PHP array_shift function
The PHP built-in array_shift() function takes an array as its argument, permanently removes the first element from the array, and returns the removed element. All the elements in the array will be shifted to an index one smaller than their previous index.
To use the function, place an array or a variable with an array as its value in between the parentheses.
Accessing and Adding Elements
In PHP, we can access the value that a given key points to using square brackets ([]) and the key. To add new elements to an associative array, we can use the assignment operator (=). We can also change existing elements using the same syntax that adds new array elements.
Assign by Value or by Reference
PHP arrays can be assigned or passed by value or by reference. PHP arrays that are passed by reference use the reference sigil (&). Here are some of the important differences between the two:
- Passed by value: this creates two variables which hold copies of the same value but remain independent entities.
- Passed by reference: this creates two variable names (aliases) which point to the same space in memory. They cannot be modified separately!
Joining Arrays in PHP
In PHP, the union (+) operator takes two array operands and returns a new array with any unique keys from the second array appended to the first array.
Removing Elements in Associative Array
A key=>value pair in a PHP associative array can be removed entirely using the PHP unset() function.
If the key used does not exist in the array, then nothing happens.