Learn Php Flashcards

1
Q
  1. what are the Applications of PHP?
A

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:

  1. Many CMS (WordPress, Drupal, Joomla)
  2. E-Commerce Platforms (WooCommerce, Magento)
  3. Web Development Frameworks (Laravel, CakePHP, Symfony)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Dynamic Webpages using PHP and PHP Script Execution

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Embedding PHP Code in HTML

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Parsing Variables within PHP Strings

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Concatenating Strings and Appending a String in PHP

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

PHP String Escape Sequences

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

PHP Reference Assignment Operator =&

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define PHP Function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

PHP built-in functions

A

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, :)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

PHP Apending Arrays

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

PHP Ordered Arrays

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

PHP print_r function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

PHP array_push function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

PHP array_pop function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

PHP unshift function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

PHP implode function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

PHP array_shift function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Accessing and Adding Elements

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Assign by Value or by Reference

A

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!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Joining Arrays in PHP

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Removing Elements in Associative Array

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Numerical Keys in Associative Arrays

A

Associative arrays can use integers or strings as keys.

In PHP, ordered arrays are just arrays in which integer keys have been assigned to the values automatically. For example, the first element is associated with key 0, the second with 1, etc. Even though ordered arrays are the same structure as associative arrays, it’s recommended that you treat the two separately.

When adding an element to an array without specifying a key, PHP associates it with the “next” key. If no integer keys have been used, it will associate it with the key 0, otherwise it will associate it one more than the largest integer used so far.

23
Q

Create Associative Array

A
24
Q

$_REQUEST Superglobal Variable

A

The $_REQUEST superglobal variable is an array that contains data from the clients cookies, GET and POST request.

25
Q

echo Shorthand Syntax

A

Instead of using the regular echo syntax like we can use a shorthand syntax such as = “Hello World”; ?>.

26
Q

PHP comparison operators

A
27
Q

PHP switch statement

A

PHP switch statements provide a clear syntax for a series of comparisons in which a value or expression is compared to many possible matches and code blocks are executed based on the matching case.

In PHP, once a matched case is encountered, the code blocks of all subsequent cases (regardless of match) will be executed until a return, break, or the end of the statement is reached. This is known as fall through.

28
Q

PHP readline()

A

The PHP built-in readline() function takes a string with which to prompt the user. It waits for the user to enter text into the terminal and returns that value as a string.

29
Q

PHP elseif statements

A

PHP elseif statements must be paired with an if statement, but many elseifs can be chained from a single if.

elseifs provide an additional condition to check (and corresponding code to execute) if the conditional statements of the if block and any preceding elseifs are not met.

30
Q

PHP ternary operator

A

In PHP, the ternary operator allows for a compact syntax in the case of binary (if/else) decisions. It evaluates a single condition and executes one expression and returns its value if the condition is met and the second expression otherwise.

The syntax for the ternary operator looks like the following:

condition ? expression1 : expression2

31
Q

Multi-File Programs: include

A

A way to improve our code and separate concerns is with modularity, separating a program into distinct, manageable chunks where each provides a piece of the overall functionality. Instead of having an entire program located in a single file, code is organized into separate files.

In PHP, files can be included in another file with the keyword include. An include statement is followed by a string with a path to the file to be included. The code from the file will be executed.

32
Q

PHP break keyword

A

In PHP, break can be used to terminate execution of a for, foreach, while or do…while loop.

One downside of heavy usage of break statements is that code can become less readable.

33
Q

PHP do while loops

A

In PHP, do…while loops are very similar to while loops. The main difference is that do…while loops always execute their code block at least once and continue execution as long as their conditional statement is true. Even if the conditional is false, the code block will execute one time.

The syntax for a do…while loop is:

34
Q

PHP for loop

A
35
Q

PHP while loops

A
36
Q

PHP foreach loop

A
37
Q

PHP while loop shorthand

A
38
Q

PHP for loop shorthand

A
39
Q

PHP foreach loop shorthand

A
40
Q

PHP filter_var function

A

In PHP, filter_var is a function that filters a variable with a specified filter.

As its first argument, filter_var() takes a variable. As its second, it takes an ID representing the type of filtering that should be performed. The third argument is optional and can be used to specify options for the filter.

There are several filters for sanitizing common input types, including FILTER_SANITIZE_EMAIL, which is a common filter that removes illegal characters for an email address.

Filters can also be used to validate that text matches a pattern. For example, FILTER_VALIDATE_EMAIL returns the variable if it contains only valid email characters. Otherwise, it returns false.

41
Q

PHP preg_match function

A
42
Q

PHP strlen function

A

echo strlen(“Codecademy”);

prints “10”

43
Q

PHP preg_replace function

A

In PHP, preg_replace is a function that can replace portions of an input string based on a regular expression.

The first argument is the pattern to search for. The second argument is the new text to replace the pattern with. The third argument is the string to operate on.

44
Q

PHP header function

A

The PHP header() function can be used to perform redirects.

We call the header() function on a string that begins with “Location: “, followed by the URL we want to redirect the user to. For example: “Location: https://www.codecademy.com/learn/learn-php/”. After invoking the header() function we’ll want to use the language construct exit to terminate the current script.

45
Q

PHP htmlspecialchars function

A
46
Q

PHP trim function

A
47
Q

PHP extends keyword

A
48
Q

PHP Constructor Method

A

A constructor method is one of several magic methods provided by PHP. This method is automatically called when an object is instantiated. A constructor method is defined with the special method name __construct. The constructor can be used to initialize an object’s properties.

49
Q

PHP class

A

In PHP, classes are defined using the class keyword.

Functions defined within a class become methods and variables within the class are considered properties.

There are three levels of visibility for class members:

  • public (default) - accessible from outside of the class
  • protected - only accessible within the class or its descendants
  • private - only accessible within the defining class

Members can be defined to be static:

  • Static members are accessed using the Scope Resolution Operator (::)

Classes are instantiated into objects using the new keyword. Members of an object are accessed using the Object Operator (->).

50
Q

PHP extends keyword

A

In PHP, to define a class that inherits from another, we use the keyword extends:

class ChildClass extends ParentClass {

}

The newly defined class can access members with public and protected visibility from the base class, but cannot access private members.

The newly defined class can also redefine or override class members.

51
Q

PHP Constructor Method

A

A constructor method is one of several magic methods provided by PHP. This method is automatically called when an object is instantiated. A constructor method is defined with the special method name __construct. The constructor can be used to initialize an object’s properties.

52
Q

PHP class

A

In PHP, classes are defined using the class keyword.

Functions defined within a class become methods and variables within the class are considered properties.

There are three levels of visibility for class members:

  • public (default) - accessible from outside of the class
  • protected - only accessible within the class or its descendants
  • private - only accessible within the defining class

Members can be defined to be static:

  • Static members are accessed using the Scope Resolution Operator (::)

Classes are instantiated into objects using the new keyword. Members of an object are accessed using the Object Operator (->).

53
Q

Overriding Methods

We can call the parent’s definition of the method within the subclass using parent:: followed by the method name

A
54
Q
A