Web Tech Unit 2: PHP Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What type of scope does the variable $x have when declared outside a function?

  • Global Scope (correct)
  • Local Scope
  • Static Scope
  • Super Global Scope

Which of the following is NOT a superglobal variable in PHP?

  • $_COOKIE
  • $_SERVER
  • $_POST
  • $_SESSION (correct)

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?

  • Nothing will be output
  • Have a good night!
  • Error due to invalid condition
  • Have a good day! (correct)

What happens when a variable is declared inside a function in PHP?

<p>It has local scope (B)</p> Signup and view all the answers

What value will the variable $x hold after executing the following code snippet: $x = 'Hello world!'; $x = null;

<p>null (B)</p> Signup and view all the answers

What is the purpose of the var_dump() function in PHP?

<p>To return the data type and value of a variable (B)</p> Signup and view all the answers

Which of the following statements is true regarding PHP variables?

<p>Double dollar variables reference another variable's value. (D)</p> Signup and view all the answers

What is the correct way to create a constant in PHP?

<p>define('MY_CONSTANT', 'Value'); (D)</p> Signup and view all the answers

Which of the following actions can PHP perform on the server?

<p>Open, read, and write files (B)</p> Signup and view all the answers

How is PHP code executed and presented to the user?

<p>On the server-side and returned as HTML (C)</p> Signup and view all the answers

Flashcards are hidden until you start studying

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, and switch 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.

Quiz Team

Related Documents

Web2 PDF - Web Tech

More Like This

Server-Side Scripting with PHP Overview
40 questions
Introduction to PHP Programming
31 questions
Introduction to PHP Programming
10 questions
Use Quizgecko on...
Browser
Browser