click below
click below
Normal Size Small Size show me how
PHP - Week 2
WTWD 410 - PHP Vocabulary
| Term | Definition | Example |
|---|---|---|
| PHP Function | A block of code that performs a specific task, can be reused, and may take inputs and return outputs. | function add($a, $b) { return $a + $b; } echo add(5, 3); // Output: 8 |
| Error Handling | Techniques used to manage errors in PHP, including the use of try, catch, and finally blocks. | try { $result = riskyOperation(); } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } finally { echo 'This block executes regardless of an error.'; } |
| MySQL Basics | Fundamental operations and commands used to interact with a MySQL database, such as connecting to the database, querying data, and closing the connection. | $cnx = new mysqli("host", "user", "pass", "db"); if ($cnx->connect_error) die("Fail: " . $cnx->connect_error); $sql = "SELECT name FROM users"; $result = $cnx->query($sql); while($row = $result->fetch_assoc()) echo $row["name"]; $cnx->close(); |
| Form Method Attribute | Specifies the HTTP method to be used when submitting the form data, typically either GET or POST. | <form action="submit.php" method="POST"> <input type="text" name="username"> <input type="submit" value="Submit"> </form> |
| Form Validation | The process of ensuring that the user-provided data in a form is correct, complete, and meets certain criteria before it is submitted. | if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim($_POST["name"]); if (empty($name)) echo "Name is required"; elseif (!preg_match("/^[a-zA-Z-' ]*$/", $name)) echo "Only letters and spaces"; else echo "Valid name"; } |
| Regular Expression (RegEx) | A sequence of characters that forms a search pattern, often used for string matching and manipulation. | $pattern = "/^([a-zA-Z' ]+)$/"; $string = "John Doe"; if (preg_match($pattern, $string)) { echo "Valid name"; } else { echo "Invalid name"; } |
| Try-Catch Statement | A control structure used to handle exceptions in PHP. The try block contains code that might throw an error, and the catch block contains code to handle the error if it occurs. | try { $result = riskyOperation(); } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } |
| Database Connection | The process of establishing a connection between PHP and a database server. | $conn = new mysqli("localhost", "username", "password", "database"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } |
| Query Execution | Running a SQL query against a database and retrieving the results. | $sql = "SELECT id, name FROM users"; $result = $conn->query($sql); while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>"; } |
| MySQLi | An improved extension of PHP to interact with MySQL databases, offering both procedural and object-oriented interfaces. | $conn = new mysqli("localhost", "user", "pass", "db"); |
| PDO (PHP Data Objects) | A database access layer providing a uniform method of access to multiple databases, including MySQL, and supporting prepared statements. | $conn = new PDO("mysql=localhost;dbname=db", "user", "pass"); |
| Prepared Statement | A feature used to execute the same SQL statement repeatedly with high efficiency, and to prevent SQL injection. | $stmt = $conn->prepare("SELECT * FROM users WHERE email = ?"); $stmt->bind_param("s", $email); $stmt->execute(); |
| Bind Parameter | Associates variables with a prepared statement as parameters, allowing for safer and more efficient execution of SQL queries. | $stmt->bind_param("s", $email); |
| Fetch | Retrieves a row from a query result, typically used within a loop to process multiple rows. | while ($row = $result->fetch_assoc()) { echo $row['id'] . " " . $row['name']; } |
| Close Connection | Terminates the connection to the MySQL database, freeing up resources. | $conn->close(); |
| Escape String | A method used to escape special characters in a string for use in an SQL statement, preventing SQL injection. | $name = $conn->real_escape_string($name); |
| MySQLi Query | A method used to perform queries against a MySQL database using the MySQLi extension. | $result = $conn->query("SELECT * FROM users"); |