Questions and Answers
What type of scope does the variable $x have when declared outside a function?
Which of the following is NOT a superglobal variable in PHP?
What will be the output of the following code: if ($t < '20') { echo 'Have a good day!'; } else { echo 'Have a good night!'; } if $t is set to 18?
What happens when a variable is declared inside a function in PHP?
Signup and view all the answers
What value will the variable $x hold after executing the following code snippet: $x = 'Hello world!'; $x = null;
Signup and view all the answers
What is the purpose of the var_dump() function in PHP?
Signup and view all the answers
Which of the following statements is true regarding PHP variables?
Signup and view all the answers
What is the correct way to create a constant in PHP?
Signup and view all the answers
Which of the following actions can PHP perform on the server?
Signup and view all the answers
How is PHP code executed and presented to the user?
Signup and view all the answers
Study Notes
PHP Basics
- PHP generates dynamic page content, allowing for real-time updates and interactions.
- Capable of file management: create, open, read, write, delete, and close files on the server.
- Collects form data and sends/receives cookies for user sessions.
- Manages database operations, including adding, deleting, and modifying data.
- Controls user access and can encrypt data for security purposes.
PHP File Characteristics
- PHP files include a mix of text, HTML, CSS, JavaScript, and PHP code.
- PHP code executes on the server, returning results as plain HTML to the browser.
- PHP files have the extension ".php".
Variable Declaration in PHP
- No explicit command to declare a variable; created on first value assignment.
- Single dollar sign ($var) denotes a normal variable.
- Double dollar sign ($$var) indicates a reference variable, storing the value of another variable.
Data Types and Functions
- The
var_dump()
function returns data type and value:- Examples include integers (5), strings ("John"), floats (3.14), booleans (true), arrays ([2, 3, 56]), and NULL values.
Constants in PHP
- Define constants using
define(name, value, case-insensitive)
. - Example:
define("GREETING", "Welcome to PHP!");
allows use in functions. - Constants are globally scoped and can also be created using the
const
keyword:const MYCAR = "Volvo";
.
PHP Data Types
- Strings: Defined with double quotes (") or single quotes (').
-
Integer: Whole numbers, e.g.,
$x = 5985;
. -
Float: Decimal numbers, e.g.,
$x = 10.365;
. -
Boolean: Represents truth values, e.g.,
$x = true;
. -
Array: Collection of values, e.g.,
$cars = array("Volvo", "BMW", "Toyota");
. -
NULL: Represents a variable without a value, e.g.,
$x = null;
.
Variable Scope
- Global Scope: Variables outside functions can only be accessed externally. Example shows access error if accessed internally.
- Local Scope: Variables declared inside functions are local and inaccessible from outside.
Conditional Statements
- PHP supports
if
,else
, andswitch
statements for decision-making. -
if
statement example:if (5 > 3) { echo "Have a good day!"; }
-
switch
statement allows multi-case checks, default executing if no cases match.
Superglobals
- Superglobals are automatically accessible throughout the PHP code, requiring no declarations.
- Common superglobals include:
-
$_GET
: Retrieves variables from the HTTP GET method. -
$_POST
: Retrieves variables from the HTTP POST method. -
$_SERVER
,$_REQUEST
, and$_GLOBALS
access server variables and global values.
-
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the fundamental aspects of PHP in web technology, including dynamic content generation, file handling, form data collection, and database interaction. Test your knowledge on how PHP functions and its role in server-side programming.