Section 1

Preview this deck

How popular is PHP?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (39)

Section 1

(39 cards)

How popular is PHP?

Front

Used on lots of sites (used on 78.9% of sites) but trend is away from PHP

Back

Where is PHP code executed?

Front

On the server to generate HTML that the client receives

Back

What is the MySQLi - Procedural way to connect to MySQL dd, check for errors, and close connection?

Front

$conn = mysqli_connect($servername, $username, $password); if(!$conn) { die("Connection failed: " . mysqli_connect_error()); } mysqli_close($conn);

Back

Describe PHP file content, where executed, and extension

Front

- contain text, HTML, CSS, JS, PHP code - executed on server and return result to browser as HTML - has extension .php

Back

What is basic syntax for a PHP function?

Front

function funcName($param=defaultVal) { ... }

Back

Concatenate the following strings together: $first_name $last_name

Front

$first_name . $last_name

Back

What are superglobals?

Front

predefined vars in PHP that are always accessible regardless of scope EX: $GLOBALS $_SERVER $_REQUEST $_SESSION $_COOKIE $_FILES $_ENV $_GET $_POST

Back

What are the array types in PHP?

Front

Numeric array - array with a numeric index > $arr = array("Volvo", "Subaru", "BMW"); Associative array - array where each ID key is associated with a value > $arr = array("Peter"=>32, "Joe"=>34); > the above is the same as: > $arr["Peter"] = "32"; $arr["Joe"] = "34"; Multidimensional array - array containing one or more arrays > $arr = array("Griffin"=>array("Joe", "Peter"),"Erbes"=>array("Courtney", "Casey")); echo $arr["Erbes"][0]; // prints Courtney

Back

What is the basic MySQL structure for inserting values into a table?

Front

INSERT INTO table_name VALUES (val1, val2, ...) OR INSERT INTO table_name (col1, col2, ...) VALUES (val1, val2,...)

Back

How do you do the following for a session? start/ resume session store/retrieve session var delete session data destroy session

Front

session_start(); $_SESSION[sessVarName]; unset($_SESSION[sessVarName]); session_destroy();

Back

Define PHP

Front

PHP: Hypertext Preprocessor - it's a recursive acronym!

Back

What is basic syntax for if else if else statement(s)?

Front

If (cond) { ... } elseif (cond2) { ... } else { ... }

Back

What is the basic MySQL structure for selecting values from a table?

Front

SELECT col_name(s) FROM table_name WHERE col_name operator val

Back

What is the MySQLi - Object Oriented way to connect to MySQL dd, check for errors, and close connection?

Front

$conn = new mysqli($servername, $username, $password); if($conn->connect_error) { die("Connection failed: " . conn->connect_error); } $conn->close();

Back

What are the two PHP output statements?

Front

echo - outputs one or more strings print - can only output one string and returns 1 Both can have output containing HTML markup

Back

What do PHP pages contain?

Front

HTML with embedded code; the embedded code is enclosed by <?php and ?>

Back

What can PHP do?

Front

- generate dynamic page content - create, open, read, write, delete, and close files on server - collect form data - send and receive cookies - add, delete, modify data in db - restrict users to access some pages on the site - encrypt data - output images, PDF files, Flash movies - output text like XML and XHTML

Back

What can be used to test that input is not empty? What can be used for pattern matching?

Front

empty($_POST["name"]) preg_match("/^[a-zA-z ]*$/", $name)

Back

How execute a MySQL select query in PHP?

Front

$sql = ".."; $colName = "..."; $res = mysqli_query($con, $sql); while($row = mysqli_fetch_array($res)) { echo $row[colName]; }

Back

How can you prepare SQL statements for later execution?

Front

$stmt = $conn->prepare("INSERT INTO MyGuests VALUES (?, ?, ?)"); $stmt->bind_param("sss", $first, $last, $email); // set params and execute // can do this in a loop $first = "John"; $last = "Doe"; $email = "john@doe.com"; $stmt->execute();

Back

What does var_dump() do?

Front

Returns the data type and value of the variables

Back

What is $_GET function used for?

Front

to collect values from a form sent with method="get" where info is visible to everyone in the address bar but can only send max 2000 chars - get inputs using their defined names EX: <form method="get"> <input name="fname"/> <input name="age"/> </form> in php file: echo $_GET["fname"]; echo $_GET["age"];

Back

What would the following code look like in a php file using echo to display the "Hello world" portion of the code? <html> <body> <p>Hello World</p> </body> </html>

Front

<html> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html>

Back

Are there switch statements in PHP?

Front

Yes

Back

What are the three scopes for a PHP variables?

Front

Local - only accessed within a function Global - only accessed outside a function > use global keyword to "declare" a previously created global var inside a function to be used EX: global $x; > use $GLOBALS[index] array to access global variables Static - lifetime is across the entire program > static $x=0;

Back

What is necessary to sanitize the form data?

Front

trim stripslashes htmlspecialchars can combine these into one function: function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); }

Back

How do PHP and AJAX work together?

Front

// js makes GET call to php and something's inner HTML is set to the response of the GET call in js: xmlhttp = new XMLHttpRequest(); if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { something.innerHTML = xmlhttp.responseText; } xmlhttp.open("GET","getuser.php?q="+str.q); xmlhttp.send(); in php file: $q = intval($_GET['q']);

Back

PHP data types

Front

String Integer (decimal, hexadecimal, octal) Floating point number (10.365, 2.4e3, 8E-5) Boolean (true, false) Array ($cars=array("Volvo","BMW","Toyota") Object (stores data and info; use class keyword to declare a structure to contain properties and methods) NULL value (

Back

What is $_POST used for?

Front

to collect values from a form sent with method="post" with info invisible to others though implicit 8Mb max size for the POST method (this can be changed in the php.ini file) EX: <form method="post"> <input name="fname"/> <input name="age"/> </form> in php file: echo htmlspecialchars($_POST["fname"]); echo (int)$_POST["age"]; // htmlspecialchars makes sure people can't inject HTML tags or JS into the page, and int gets rid of stray chars

Back

How can you retrieve and set cookie values in PHP? How check if a specific cookie is set?

Front

setcookie(cookieStrName, value, expire, path, domain) // must appear BEFORE the <html> tag EX: setcookie("user", "Alex Porter", time+3600); $_COOKIE[cookieStrName] isset($_COOKIE[cookieStrName])

Back

How do you make a PHP comment?

Front

// or /* */

Back

How execute a MySQL insertion query in PHP?

Front

$sql = ".."; if (!mysqli_query($con, $sql)) { die(); }

Back

What is the PDO way to connect to MySQL dd, check for errors, and close connection?

Front

try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } $conn = null;

Back

What is a session? What is sent to the client instead of a cookie?

Front

A PHP session var is used to store info about or change settings for a user session on the server - hold info on one user and available to all pages in an app - unique session id is returned to the client

Back

What is a cookie? When is a cookie sent?

Front

A cookie is often used to identify a user and is a small file stored on the user's computer - cookie is sent each time the same computer requests a page with a browser

Back

What are the basic loops for PHP?

Front

while do while for foreach ($arr as $val) { }

Back

When is a variable created? When is data type determined?

Front

Both: When a value is assigned to it

Back

What is the default location for Xampp document root? How do we use the browser to access a file named text.php that is at the document root?

Front

Root = xampp/htdocs Browser: localhost/text.php

Back

What are valid PHP variables?

Front

- start with $ sign - name: starts with letter or underscore, only contains alpha-numeric chars and underscores - variables are case sensitive

Back