php Flashcards

1
Q

php: To render a string to a template, type

A

-?php echo “string”; ?>

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

php: The built in function for returning the date year is

A

date(“Y”) e.g.

-?php echo date(“Y”); ?>

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

php: For the server to render a page as php,

A

the filename must be .php

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

php: Files that get included in other templates are called

A

include files, and go in “includes” folder

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

php: To include another template into your current template, type

A

-?php include(“file/path.php”); ?>

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

php: To create a php variable, type

A

-?php>
$varName = “string”
?>

note: Must start with dollar

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

php: Variables that are rendered before an include file

A

can be referenced from within the file

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

php: To return get params, type

A

$_GET[“key”]

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

php: To create an if, if else statement, type

A

just like js

if (100 == 100) {
  ....
} else if (100 == 100) {
  ...
} else {
  ...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

php: To check if there is a query string, type

A

isset($_GET[“key”])

e.g. if (isset($_GET[“key”])) {

}

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

php: If you reference a get param that does not exist

A

it shows an error on the page, so always use if isset($_GET[‘key’]) {}

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

php: To set a conditional class name, type

A

-?php if (100==100) { echo “ class-name”; } ?>

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

php: For variables used in conditionals, always set a

A

default value to avoid errors like

$varName = null;

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

php: an “and” conditional statement uses

A

double ampersand

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

php: To create a variable that holds an array, type

A
$arrayVar = array("value1", "value2");
$arrayVar = ["value1", "value2"];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

php: The final semicolon for an if statement goes

A

inside the curly brackets

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

php: To concatenate a string with a var type

A

“string” . $varName

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

php: To render a loop through an array, type

A

foreach($arrayVar as $item) {
echo “-li>” . $item . “-/li>”;
}

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

php: To append an item to an existing array, type

A

$arrayVar[] = “string”;

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

php: To specify the key for an array item you are appending, type

A

$arrayVar[100] = “string”;

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

php: To return the length of an array, type

A

count($arrayVar);

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

php: in a foreach, the echo goes

A

inside the curly braces

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

php: A dictionary in php is called

A

an associative array

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

php: To create an associative array, type

A

$assArrayVar = [“key” => “value”, “key” => “value”]

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

php: Functions are usually

A

stored together in a functions.php file and included on all pages

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

php: To create a function, type

A
-?php
function function_name($param1, $param2) {
  $output = $param1 . $param2;
  return $output;
}
?>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

php: To pull out the id of an $item in a foreach, type

A

foreach ($arrayVar as $id => $item) {
echo $id . $item
}

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

php: To make a ternary operator/one line if, type

A

($varName == 10 ? true : false);

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

php: To return a random item key from an array, type

A

array_rand($arrayVar, 1);

note: the second argument is the number of items will be returned

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

php: To print a variable’s data to the screen, type

A

var_dump($varName);

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

php: To convert a string to lower case, type

A

strtolower(“string”)

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

php: To cut a specific string from the left, type

A

ltrim(“string”)

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

php: To sort an array in place, type

A

asort($arrayVar)

34
Q

php: To make an “or” conditional statement, use

A

OR or ||

35
Q

php: To make true or false the opposite, type

A

!true !false

36
Q

php: To redirect, type

A

header(“location:page_name.php”);
exit;
note: Exit prevents other processes from occurring during redirect.

37
Q

php: header(“location:page_name.php”) will fail if

A

there is anything sent back to the browser, so header() must be called in a php block at the very top of the page requested, and no blank lines can be above.

38
Q

php: To convert an array to a string with spaces between each item, type

A

implode(“ “, $arrayVar);

39
Q

php: To access params that are posted to a php endpoint, type

A

$_POST[“key”]

40
Q

php: A short hand for appending something to an existing variable is

A

.=

41
Q

php: To write a comment, typ

A

//

42
Q

php: To make a route that accepts a form post

A

create a file and post the data to it (can be same as form). In target file, perform the actions you want on the data and then redirect to a thank you page in order to prevent the user from being able to refresh and resubmit a form.

43
Q

php: To conditionally handle only a post or get request, type

A

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {}

if ($_SERVER[“REQUEST_METHOD”] == “GET”) {}

44
Q

php: When creating a condition that depends on a value of a request param, use

A

if (isset($_GET[“key”]) dbl amp $_GET[“key”]==”value”) {

}

To check the presence and value in one line

45
Q

php: You can open an if else

A

in one area of the page and then close the if else inside of a php block that is not connected later in the page

46
Q

php: To eliminate white space from a string, type

A

trim($varName)

47
Q

php: To stop processing all code while handling a request, type

A
exit;
e.g. 
if ($varName == "") {
  echo "error"
  exit;
}

note: If there was any html above this code, it would have sent by now

48
Q

php: To clean up a user’s input for a form in the post request.

A

$name = trim(filter_input(INPUT_POST, “name”, FILTER_SANITIZE_STRING));

$email = trim(filter_input(INPUT_POST, “email”, FILTER_SANITIZE_EMAIL));

$message = trim(filter_input(INPUT_POST, “message”, FILTER_SANITIZE_SPECIAL_CHARS));

49
Q

php: A good php email library is called

A

phpmailer

50
Q

php: The difference between require “file.php” and include(“file.php”) is

A

if you include a file that doesn’t exist it will throw a warning and continue to execute code while require will error and quit.

51
Q

php: To prevent getting an error when you require or include a file containing functions more than once

A

use include_once(“”) or require_once “”

note: require or include are more common to use in all cases

52
Q

php: To create a class instance, type

A

@instanceName = new ClassName;

53
Q

php: To call a class method, type

A

@instanceName->methodName(param1);

54
Q

php: To call an object property, type

A

@instanceName->propertyName;

55
Q

wp: To redirect user that is not logged in to the login page, type

A

if ( !is_user_logged_in() ) {
auth_redirect();
}

56
Q

wp: To check if user is logged in, type

A

is_user_logged_in()

57
Q

wp: The two types of hooks are

A

actions and filters. Filters must return and action don’t.

58
Q

wp: An action is

A

a function you make that gets triggered by wp at a designated time.

59
Q

wp: A filter is a

A

function that receives params from wp and must return something

60
Q

wp: When wp runs its runtime, it

A

gets values of things by calling their hook which cause all of the functions associated with that hook to run and the return of the last hook that runs will be the value

61
Q

wp: To run your hooked filter functions, type

A

apply_filters(“hook_name”, “default_param”)

note: apply_filters requires a default argument to be sent into the functions

62
Q

wp: To create a filter that overwrites a default filter you must

A

add it to the same hook but make its priority a higher number so it runs after.

63
Q

wp: To hook my filter onto a hook, type

A

add_filter(“hook_name”, “my_function_name”, 10, 1)

the 10 is priority, and 1 is number of arguments to accept. You know the number of args by checking the apply_filters() for this hook and seeing how many args it has (after the default value which would be the first argument)

64
Q

wp: To create a filter function, type

A
function filter_function_name($arg) {
  return ...
}
note: Make sure to accept an argument for filters. The argument is whatever is sent from apply("hook_name", "argument")
65
Q

wp: To remove a filter from a hook to prevent it from running, type

A

remove_filter(“hook_name”, “function_to_remove_name”, 10)

note: 10 is the priority that the function has, so you must check for it’s priority in the docs

66
Q

wp: To check if a filter exists, type

A

has_filter(“filter_name”)

67
Q

wp: To check if a filter has a function in it, type

A

has_filter(“filter_name”, “function_name”)

68
Q

wp: To return true if you are in the main query, type

A

is_main_query()

69
Q

wp: To return the type of page you are on, type

A

get_post_type()

70
Q

wp: To remove an item from an array by its key, type

A

unset($arrayVar[“key”]);

71
Q

wp: Essentially, the process of modifying wordpress is

A

create a function, hook it to a the right hook,

72
Q

php: To cast a var to a type, type

A
(int)$varName
or
settype($foo, "int");
73
Q

php: To pass params to an anonymous function, type

A
function() use ($param1, $param2) {
  ...
}
74
Q

php: To remove an action, type

A

remove_action(“hook_name”, “function_to_remove_name”, 10)

note: 10 is the priority

75
Q

php: To add an action, type

A

add_filters(“hook_name”, “my_function_name”, 10, 1)

the 10 is priority, and 1 is number of arguments to accept. You know the number of args by checking the apply_actions() for this hook and seeing how many args it has (after the default value which would be the first argument to apply_actions())

76
Q

wp: To create an action hook

A
create a function that holds the hook
function function_name() {
  do_action("hook_name")
}

and call it from the page being loaded by the request
-?php echo function_name() ?>

77
Q

wp: To check if an action hook has a certain function, type

A

has_action(“hook_name”, “function_name”)

78
Q

wp: The hook that runs when widget get set up is called

A

widget_init

79
Q

wp: The hook that runs when wp menus get set up is called

A

init

80
Q

wp: It’s crucial to

A

attach to the correct hook for whatever you want to change

81
Q

wp: wp provides a function that allows you to create new users called

A

$user_id = wp_insert_user()

note: Takes in an associative array and returns the user id

82
Q

wp: To add a user meta to a user, type

A

update_user_meta( $user_id, ‘key’, ‘value’ )