php Flashcards
Q1: What does PHP stand for?
A: PHP stands for Hypertext Preprocessor.
Q2: Is PHP a client-side or server-side language?
A: PHP is a server-side scripting language.
Q3: What are PHP files saved as?
A: PHP files have a .php extension.
Q4: What platforms does PHP support?
A: PHP runs on Windows, Linux, Mac, etc.
Q5: Name two popular web servers PHP supports.
A: Apache and IIS.
Q6: Can PHP work with databases?
A: Yes, PHP supports databases like MySQL.
Q7: What can PHP do with files?
A: PHP can create, open, read, write, and delete files.
Q8: How is PHP used in forms?
A: It collects form data and processes it.
Q9: How is PHP code executed?
A: On the server, and HTML is sent to the browser.
Q10: What is PHP mainly used for?
A: Building dynamic web applications.
Q1: What is the basic syntax for PHP?
A: <?php to start, ?> to end.
Q2: Are PHP keywords case-sensitive?
A: No, but variables are case-sensitive.
Q3: How do you write a single-line comment?
A: Using // or #.
Q4: How do you write multi-line comments?
A: Using /* */.
Q5: How should a PHP variable start?
A: With a $ followed by a letter or underscore.
Q6: Can a PHP variable start with a number?
A: No, it must start with a letter or underscore.
Q7: What are valid variable names in PHP?
A: $name, $_age, $user123.
Q8: What happens if PHP code contains an error?
A: It may show a blank page or error message.
Q9: What does echo do in PHP?
A: Outputs data to the screen.
Q10: What symbol is used for variables in PHP?
A: The $ sign.
Q1: What are the main data types in PHP?
A: String, Integer, Float, Boolean, NULL, Array, Object.
Q2: What is a string in PHP?
A: A sequence of characters (e.g., “Hello”).
Q7: What is an array in PHP?
A: A collection of values stored under one name.
Q8: What are the types of arrays in PHP?
A: Indexed, Associative, Multidimensional.
Q9: What is an object in PHP?
A: An instance of a class containing data and functions.
Q10: How do you check a variable’s data type in PHP?
A: Using the var_dump() function.
Q1: What are the types of conditional statements in PHP?
A: if, if-else, if-elseif-else, switch.
Q2: How does an if statement work in PHP?
A: Executes code if a condition is true.
Q3: What does an else statement do?
A: Runs code if the if condition is false.
Q4: When do we use elseif in PHP?
A: To check multiple conditions after an if.
Q5: What is the purpose of a switch statement?
A: To select one of many code blocks to execute.
Q6: What keyword is used to exit a switch case?
A: break.
Q7: What happens if you don’t use break in a switch?
A: It will continue executing the next case.
Q8: Can you use default in a switch statement?
A: Yes, it runs if no cases match.
Q9: What operator is used to compare values in conditions?
A: == (equal to).
Q10: What operator checks both value and type?
A: === (identical).
Q1: What are the loop types in PHP?
A: for, foreach, while, do-while.
Q2: How does a for loop work?
A: Loops a set number of times.
Q3: What does a foreach loop do?
A: Loops through each element in an array.
Q4: What is the syntax of a while loop?
A: Executes as long as a condition is true.
Q5: What is unique about a do-while loop?
A: Executes at least once before checking the condition.
Q6: What keyword is used to exit a loop?
A: break.
Q7: What keyword skips the current iteration?
A: continue.
Q8: What is an infinite loop?
A: A loop that never ends due to a missing exit condition.
Q9: Can loops be nested in PHP?
A: Yes, loops can be placed inside other loops.
Q10: How do you loop through an associative array?
A: Using the foreach loop.
Q1: What is a function in PHP?
A: A block of code that performs a specific task.
Q2: How do you define a function in PHP?
A: Using the function keyword.
Q3: What is the syntax for defining a function?
A: function functionName() { //code }
Q4: How do you call a function?
A: By using its name followed by parentheses functionName();
Q5: What are PHP function types?
A: Built-in and user-defined functions.
Q6: How do you pass arguments to a function?
A: Inside parentheses, e.g. function greet($name).
Q7: Can a function return a value?
A: Yes, using the return keyword.
Q8: What is a default function argument?
A: A preset value if no argument is passed.
Q9: Are function names case-sensitive?
A: No, function names are not case-sensitive.
Q10: What does declare(strict_types=1); do?
A: Enforces strict type checking in PHP 7+.
Q1: What is an array in PHP?
A: A collection of values stored under a single name.
Q2: What are the types of arrays in PHP?
A: Indexed, Associative, Multidimensional.
Q3: How do you create an indexed array?
A: $arr = array(“apple”, “banana”, “cherry”);
Q4: How do you access an indexed array element?
A: Using its index, e.g. $arr[0].
Q5: How do you create an associative array?
A: $arr = array(“name” => “Salam”, “age” => 22);
Q6: How do you access an associative array value?
A: By its key, e.g. $arr[“name”].
Q7: What is a multidimensional array?
A: An array containing other arrays.
Q8: How do you loop through an array?
A: Using a foreach loop.
Q9: Name two functions to sort an array?
A: sort() and rsort().
Q10: How do you count elements in an array?
A: Using count($array);.
Q1: What are PHP superglobals?
A: Predefined variables accessible from anywhere in code.
Q2: What does $_GET do?
A: Collects form data sent via URL.
Q3: What does $_POST do?
A: Collects form data submitted via POST method.
Q4: What does $_SERVER do?
A: Holds server and execution environment information.
Q5: What does $_SESSION store?
A: User session data across pages.
Q6: What does $_COOKIE store?
A: Stores small pieces of data on the user’s device.
Q7: What does $_FILES handle?
A: File uploads in forms.
Q8: What does $_REQUEST do?
A: Collects data from both GET and POST requests.
Q9: How do you check if a form field is set?
A: Using isset($_POST[‘fieldname’]);.
Q10: What does $_GLOBALS store?
A: All global variables in an array.
Q1: Why is form validation important?
A: To protect against spam and security threats.
Q2: What function removes whitespace from input?
A: trim().
Q3: What function removes backslashes from input?
A: trim().
Q3: What function removes backslashes from input?
A: stripslashes().
Q5: How do you check if a field is empty?
A: Using empty().
Q4: What function converts special characters?
A: htmlspecialchars().
Q6: How do you validate an email in PHP?
A: Using filter_var($email, FILTER_VALIDATE_EMAIL).
Q7: What is cross-site scripting (XSS)?
A: A security attack where attackers inject scripts into web forms.
Q8: How can PHP prevent XSS attacks?
A: By sanitizing input with htmlspecialchars().
Q9: What function filters input data?
A: filter_input().
Q10: How do you handle errors in form validation?
A: By checking conditions and showing error messages.
Q1: What is a cookie in PHP?
A: A small file stored on the user’s computer.
Q2: How do you set a cookie in PHP?
A: Using setcookie(name, value, expiry);.
Q3: How do you access a cookie?
A: Using $_COOKIE[‘name’];.
Q4: How do you delete a cookie?
A: Set an expired time with setcookie(name, “”, time() - 3600);.
Q5: What is a session in PHP?
A: A way to store user data across multiple pages.
Q6: How do you start a session?
A: Using session_start();.
Q7: How do you store session data?
A: Using $_SESSION[‘key’] = ‘value’;.
Q8: How do you remove session data?
A: Using unset($_SESSION[‘key’]);.
Q9: How do you destroy an entire session?
A: Using session_destroy();.
Q10: What is the difference between a cookie and a session?
A: Cookies are stored on the client, sessions on the server.
Q1: What is MySQL?
A: A free, fast, and reliable database system.
Q2: How do you connect PHP to MySQL?
A: Using MySQLi or PDO extensions.
Q3: What does SQL stand for?
A: Structured Query Language.
Q4: How do you create a MySQL connection in PHP?
A: Using mysqli_connect(server, user, password, dbname);.
Q5: What function is used to close a MySQL connection?
A: mysqli_close($conn);.
Q6: How do you insert data into a MySQL table?
A: Using INSERT INTO table (columns) VALUES (values);.
Q7: How do you retrieve data from a MySQL table?
A: Using SELECT * FROM table;.
Q8: How do you update records in MySQL?
A: Using UPDATE table SET column=value WHERE condition;.
Q9: How do you delete records from MySQL?
A: Using DELETE FROM table WHERE condition;.
Q10: What function is used to execute SQL queries in PHP?
A: mysqli_query($conn, $query);.
Q1: What does MySQLi stand for?
A: MySQL Improved.
Q2: What is PDO?
A: PHP Data Objects, a database access layer.
Q3: Which extension works only with MySQL?
A: MySQLi.
Q4: Which extension supports multiple databases?
A: PDO.
Q5: Which is better for prepared statements, MySQLi or PDO?
A: Both support prepared statements.
Q6: What is an advantage of MySQLi?
A: Supports both procedural and object-oriented programming.
Q7: How do you connect to MySQL using PDO?
A: new PDO(“mysql:host=host;dbname=db”, “user”, “password”);.
Q8: Can you switch databases easily with PDO?
A: Yes, it supports 12+ databases.
Q9: Does MySQLi support exception handling?
A: No, but PDO does.
Q10: Which one is more secure, MySQLi or PDO?
A: Both are secure when using prepared statements.
Q1: What is a prepared statement?
A: A precompiled SQL statement to prevent SQL injection.
Q2: Why are prepared statements important?
A: They protect against SQL injection attacks.
Q3: How do you create a prepared statement in MySQLi?
A: Using $stmt = $conn->prepare(“SQL query”);.
Q4: How do you bind parameters in a prepared statement?
A: Using $stmt->bind_param(“s”, $var);.
Q5: What does the s in bind_param mean?
A: String; it defines the data type.
Q6: How do you execute a prepared statement?
A: Using $stmt->execute();.
Q7: How do you fetch results from a prepared statement?
A: Using $stmt->get_result();.
Q8: How do you close a prepared statement?
A: Using $stmt->close();.
Q9: What data types can be bound in prepared statements?
A: i (integer), d (double), s (string), b (blob).
Q10: What is an advantage of using prepared statements?
A: Faster execution and enhanced security.