php Flashcards
php: To render a string to a template, type
-?php echo “string”; ?>
php: The built in function for returning the date year is
date(“Y”) e.g.
-?php echo date(“Y”); ?>
php: For the server to render a page as php,
the filename must be .php
php: Files that get included in other templates are called
include files, and go in “includes” folder
php: To include another template into your current template, type
-?php include(“file/path.php”); ?>
php: To create a php variable, type
-?php>
$varName = “string”
?>
note: Must start with dollar
php: Variables that are rendered before an include file
can be referenced from within the file
php: To return get params, type
$_GET[“key”]
php: To create an if, if else statement, type
just like js
if (100 == 100) { .... } else if (100 == 100) { ... } else { ... }
php: To check if there is a query string, type
isset($_GET[“key”])
e.g. if (isset($_GET[“key”])) {
…
}
php: If you reference a get param that does not exist
it shows an error on the page, so always use if isset($_GET[‘key’]) {}
php: To set a conditional class name, type
-?php if (100==100) { echo “ class-name”; } ?>
php: For variables used in conditionals, always set a
default value to avoid errors like
$varName = null;
php: an “and” conditional statement uses
double ampersand
php: To create a variable that holds an array, type
$arrayVar = array("value1", "value2"); $arrayVar = ["value1", "value2"];
php: The final semicolon for an if statement goes
inside the curly brackets
php: To concatenate a string with a var type
“string” . $varName
php: To render a loop through an array, type
foreach($arrayVar as $item) {
echo “-li>” . $item . “-/li>”;
}
php: To append an item to an existing array, type
$arrayVar[] = “string”;
php: To specify the key for an array item you are appending, type
$arrayVar[100] = “string”;
php: To return the length of an array, type
count($arrayVar);
php: in a foreach, the echo goes
inside the curly braces
php: A dictionary in php is called
an associative array
php: To create an associative array, type
$assArrayVar = [“key” => “value”, “key” => “value”]
php: Functions are usually
stored together in a functions.php file and included on all pages
php: To create a function, type
-?php function function_name($param1, $param2) { $output = $param1 . $param2; return $output; } ?>
php: To pull out the id of an $item in a foreach, type
foreach ($arrayVar as $id => $item) {
echo $id . $item
}
php: To make a ternary operator/one line if, type
($varName == 10 ? true : false);
php: To return a random item key from an array, type
array_rand($arrayVar, 1);
note: the second argument is the number of items will be returned
php: To print a variable’s data to the screen, type
var_dump($varName);
php: To convert a string to lower case, type
strtolower(“string”)
php: To cut a specific string from the left, type
ltrim(“string”)